@devbro/neko-cache 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CacheProviderInterface.d.mts +30 -0
- package/dist/cache.d.mts +53 -3
- package/dist/cache.mjs +45 -3
- package/dist/cache.mjs.map +1 -1
- package/dist/index.js +235 -3
- package/dist/index.js.map +1 -1
- package/dist/providers/DisabledCacheProvider.d.mts +34 -0
- package/dist/providers/DisabledCacheProvider.mjs +30 -0
- package/dist/providers/DisabledCacheProvider.mjs.map +1 -1
- package/dist/providers/FileCacheProvider.d.mts +59 -0
- package/dist/providers/FileCacheProvider.mjs +49 -0
- package/dist/providers/FileCacheProvider.mjs.map +1 -1
- package/dist/providers/MemcacheCacheProvider.d.mts +40 -0
- package/dist/providers/MemcacheCacheProvider.mjs +31 -0
- package/dist/providers/MemcacheCacheProvider.mjs.map +1 -1
- package/dist/providers/MemoryCacheProvider.d.mts +53 -0
- package/dist/providers/MemoryCacheProvider.mjs +43 -0
- package/dist/providers/MemoryCacheProvider.mjs.map +1 -1
- package/dist/providers/RedisCacheProvider.d.mts +41 -0
- package/dist/providers/RedisCacheProvider.mjs +37 -0
- package/dist/providers/RedisCacheProvider.mjs.map +1 -1
- package/package.json +2 -2
|
@@ -1,10 +1,40 @@
|
|
|
1
1
|
import { JSONValue, JSONObject } from '@devbro/neko-helper';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Interface that all cache providers must implement.
|
|
5
|
+
* Defines the contract for cache operations across different storage backends.
|
|
6
|
+
*/
|
|
3
7
|
interface CacheProviderInterface {
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves a value from the cache.
|
|
10
|
+
* @param key - The cache key
|
|
11
|
+
* @returns The cached value or undefined if not found
|
|
12
|
+
*/
|
|
4
13
|
get(key: string): Promise<JSONValue | JSONObject | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Stores a value in the cache.
|
|
16
|
+
* @param key - The cache key
|
|
17
|
+
* @param value - The value to cache
|
|
18
|
+
* @param ttl - Time to live in seconds (optional)
|
|
19
|
+
*/
|
|
5
20
|
put(key: string, value: JSONObject | JSONValue, ttl?: number): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Deletes a value from the cache.
|
|
23
|
+
* @param key - The cache key to delete
|
|
24
|
+
*/
|
|
6
25
|
delete(key: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a key exists in the cache.
|
|
28
|
+
* @param key - The cache key to check
|
|
29
|
+
* @returns True if the key exists, false otherwise
|
|
30
|
+
*/
|
|
7
31
|
has(key: string): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Increments a numeric value in the cache atomically.
|
|
34
|
+
* @param key - The cache key to increment
|
|
35
|
+
* @param amount - The amount to increment by (default: 1)
|
|
36
|
+
* @returns The new value after incrementing
|
|
37
|
+
*/
|
|
8
38
|
increment(key: string, amount?: number): Promise<number>;
|
|
9
39
|
}
|
|
10
40
|
|
package/dist/cache.d.mts
CHANGED
|
@@ -1,22 +1,72 @@
|
|
|
1
1
|
import { JSONValue } from '@devbro/neko-helper';
|
|
2
2
|
import { CacheProviderInterface } from './CacheProviderInterface.mjs';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Options for cache operations.
|
|
6
|
+
*/
|
|
4
7
|
type cacheOptions = {
|
|
8
|
+
/** Time to live in seconds */
|
|
5
9
|
ttl?: number;
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Cache class providing a unified interface for various cache providers.
|
|
13
|
+
* Handles key generation, serialization, and common cache operations.
|
|
14
|
+
*/
|
|
7
15
|
declare class Cache {
|
|
8
16
|
private provider;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new Cache instance.
|
|
19
|
+
* @param provider - The cache provider implementation to use
|
|
20
|
+
*/
|
|
9
21
|
constructor(provider: CacheProviderInterface);
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves a value from the cache.
|
|
24
|
+
* @template T - The expected type of the cached value
|
|
25
|
+
* @param key - The cache key (can be string, number, object, or array)
|
|
26
|
+
* @returns The cached value or undefined if not found or expired
|
|
27
|
+
*/
|
|
10
28
|
get<T>(key: JSONValue): Promise<T | undefined>;
|
|
29
|
+
/**
|
|
30
|
+
* Stores a value in the cache.
|
|
31
|
+
* @param key - The cache key (can be string, number, object, or array)
|
|
32
|
+
* @param value - The value to cache
|
|
33
|
+
* @param ttl - Time to live in seconds (optional)
|
|
34
|
+
*/
|
|
11
35
|
put(key: JSONValue, value: any, ttl?: number): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Deletes a value from the cache.
|
|
38
|
+
* @param key - The cache key to delete
|
|
39
|
+
*/
|
|
12
40
|
delete(key: JSONValue): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a key exists in the cache.
|
|
43
|
+
* @param key - The cache key to check
|
|
44
|
+
* @returns True if the key exists and has not expired, false otherwise
|
|
45
|
+
*/
|
|
13
46
|
has(key: JSONValue): Promise<boolean>;
|
|
47
|
+
/**
|
|
48
|
+
* Increments a numeric value in the cache atomically.
|
|
49
|
+
* @param key - The cache key to increment
|
|
50
|
+
* @param amount - The amount to increment by (default: 1)
|
|
51
|
+
* @returns The new value after incrementing
|
|
52
|
+
*/
|
|
14
53
|
increment(key: JSONValue, amount?: number): Promise<number>;
|
|
54
|
+
/**
|
|
55
|
+
* Gets a value from cache or executes callback and caches the result.
|
|
56
|
+
* This is useful for caching expensive operations.
|
|
57
|
+
* @template T - The expected type of the value
|
|
58
|
+
* @param key - The cache key
|
|
59
|
+
* @param callback - Function to execute if cache miss occurs
|
|
60
|
+
* @param options - Cache options including TTL (default: 3600 seconds)
|
|
61
|
+
* @returns The cached value or the result of the callback
|
|
62
|
+
*/
|
|
15
63
|
remember<T>(key: JSONValue, callback: () => Promise<T>, options?: cacheOptions): Promise<T>;
|
|
16
64
|
/**
|
|
17
|
-
* Generates a cache key by serializing and
|
|
18
|
-
*
|
|
19
|
-
*
|
|
65
|
+
* Generates a cache key by serializing and hashing complex keys.
|
|
66
|
+
* Simple string keys under 250 characters are returned as-is.
|
|
67
|
+
* Complex keys (objects, arrays, long strings) are MD5 hashed.
|
|
68
|
+
* @param key - The key to generate a cache key from
|
|
69
|
+
* @returns A string suitable for use as a cache key
|
|
20
70
|
*/
|
|
21
71
|
generateKey(key: JSONValue): string;
|
|
22
72
|
}
|
package/dist/cache.mjs
CHANGED
|
@@ -2,27 +2,67 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
import { createHash } from "crypto";
|
|
4
4
|
class Cache {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a new Cache instance.
|
|
7
|
+
* @param provider - The cache provider implementation to use
|
|
8
|
+
*/
|
|
5
9
|
constructor(provider) {
|
|
6
10
|
this.provider = provider;
|
|
7
11
|
}
|
|
8
12
|
static {
|
|
9
13
|
__name(this, "Cache");
|
|
10
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves a value from the cache.
|
|
17
|
+
* @template T - The expected type of the cached value
|
|
18
|
+
* @param key - The cache key (can be string, number, object, or array)
|
|
19
|
+
* @returns The cached value or undefined if not found or expired
|
|
20
|
+
*/
|
|
11
21
|
async get(key) {
|
|
12
22
|
return this.provider.get(this.generateKey(key));
|
|
13
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Stores a value in the cache.
|
|
26
|
+
* @param key - The cache key (can be string, number, object, or array)
|
|
27
|
+
* @param value - The value to cache
|
|
28
|
+
* @param ttl - Time to live in seconds (optional)
|
|
29
|
+
*/
|
|
14
30
|
async put(key, value, ttl) {
|
|
15
31
|
return this.provider.put(this.generateKey(key), value, ttl);
|
|
16
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Deletes a value from the cache.
|
|
35
|
+
* @param key - The cache key to delete
|
|
36
|
+
*/
|
|
17
37
|
async delete(key) {
|
|
18
38
|
return this.provider.delete(this.generateKey(key));
|
|
19
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Checks if a key exists in the cache.
|
|
42
|
+
* @param key - The cache key to check
|
|
43
|
+
* @returns True if the key exists and has not expired, false otherwise
|
|
44
|
+
*/
|
|
20
45
|
async has(key) {
|
|
21
46
|
return this.provider.has(this.generateKey(key));
|
|
22
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Increments a numeric value in the cache atomically.
|
|
50
|
+
* @param key - The cache key to increment
|
|
51
|
+
* @param amount - The amount to increment by (default: 1)
|
|
52
|
+
* @returns The new value after incrementing
|
|
53
|
+
*/
|
|
23
54
|
async increment(key, amount = 1) {
|
|
24
55
|
return this.provider.increment(this.generateKey(key), amount);
|
|
25
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets a value from cache or executes callback and caches the result.
|
|
59
|
+
* This is useful for caching expensive operations.
|
|
60
|
+
* @template T - The expected type of the value
|
|
61
|
+
* @param key - The cache key
|
|
62
|
+
* @param callback - Function to execute if cache miss occurs
|
|
63
|
+
* @param options - Cache options including TTL (default: 3600 seconds)
|
|
64
|
+
* @returns The cached value or the result of the callback
|
|
65
|
+
*/
|
|
26
66
|
async remember(key, callback, options = {}) {
|
|
27
67
|
options.ttl = options.ttl ?? 3600;
|
|
28
68
|
const cached = await this.get(key);
|
|
@@ -34,9 +74,11 @@ class Cache {
|
|
|
34
74
|
return result;
|
|
35
75
|
}
|
|
36
76
|
/**
|
|
37
|
-
* Generates a cache key by serializing and
|
|
38
|
-
*
|
|
39
|
-
*
|
|
77
|
+
* Generates a cache key by serializing and hashing complex keys.
|
|
78
|
+
* Simple string keys under 250 characters are returned as-is.
|
|
79
|
+
* Complex keys (objects, arrays, long strings) are MD5 hashed.
|
|
80
|
+
* @param key - The key to generate a cache key from
|
|
81
|
+
* @returns A string suitable for use as a cache key
|
|
40
82
|
*/
|
|
41
83
|
generateKey(key) {
|
|
42
84
|
if (typeof key === "string" && key.length <= 250) {
|
package/dist/cache.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cache.mts"],"sourcesContent":["import { JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from './CacheProviderInterface.mjs';\nimport { createHash } from 'crypto';\n\nexport type cacheOptions = {\n ttl?: number;\n};\n\nexport class Cache {\n constructor(private provider: CacheProviderInterface) {}\n\n async get<T>(key: JSONValue): Promise<T | undefined> {\n return this.provider.get(this.generateKey(key)) as Promise<T | undefined>;\n }\n\n async put(key: JSONValue, value: any, ttl?: number): Promise<void> {\n return this.provider.put(this.generateKey(key), value, ttl);\n }\n\n async delete(key: JSONValue): Promise<void> {\n return this.provider.delete(this.generateKey(key));\n }\n\n async has(key: JSONValue): Promise<boolean> {\n return this.provider.has(this.generateKey(key));\n }\n\n async increment(key: JSONValue, amount: number = 1): Promise<number> {\n return this.provider.increment(this.generateKey(key), amount);\n }\n\n async remember<T>(\n key: JSONValue,\n callback: () => Promise<T>,\n options: cacheOptions = {}\n ): Promise<T> {\n options.ttl = options.ttl ?? 3600; // default TTL 1 hour\n\n const cached = await this.get<T>(key);\n if (cached) {\n return cached;\n }\n\n const result = await callback();\n await this.put(key, result, options.ttl);\n return result;\n }\n\n /**\n * Generates a cache key by serializing and
|
|
1
|
+
{"version":3,"sources":["../src/cache.mts"],"sourcesContent":["import { JSONValue } from '@devbro/neko-helper';\nimport { CacheProviderInterface } from './CacheProviderInterface.mjs';\nimport { createHash } from 'crypto';\n\n/**\n * Options for cache operations.\n */\nexport type cacheOptions = {\n /** Time to live in seconds */\n ttl?: number;\n};\n\n/**\n * Cache class providing a unified interface for various cache providers.\n * Handles key generation, serialization, and common cache operations.\n */\nexport class Cache {\n /**\n * Creates a new Cache instance.\n * @param provider - The cache provider implementation to use\n */\n constructor(private provider: CacheProviderInterface) {}\n\n /**\n * Retrieves a value from the cache.\n * @template T - The expected type of the cached value\n * @param key - The cache key (can be string, number, object, or array)\n * @returns The cached value or undefined if not found or expired\n */\n async get<T>(key: JSONValue): Promise<T | undefined> {\n return this.provider.get(this.generateKey(key)) as Promise<T | undefined>;\n }\n\n /**\n * Stores a value in the cache.\n * @param key - The cache key (can be string, number, object, or array)\n * @param value - The value to cache\n * @param ttl - Time to live in seconds (optional)\n */\n async put(key: JSONValue, value: any, ttl?: number): Promise<void> {\n return this.provider.put(this.generateKey(key), value, ttl);\n }\n\n /**\n * Deletes a value from the cache.\n * @param key - The cache key to delete\n */\n async delete(key: JSONValue): Promise<void> {\n return this.provider.delete(this.generateKey(key));\n }\n\n /**\n * Checks if a key exists in the cache.\n * @param key - The cache key to check\n * @returns True if the key exists and has not expired, false otherwise\n */\n async has(key: JSONValue): Promise<boolean> {\n return this.provider.has(this.generateKey(key));\n }\n\n /**\n * Increments a numeric value in the cache atomically.\n * @param key - The cache key to increment\n * @param amount - The amount to increment by (default: 1)\n * @returns The new value after incrementing\n */\n async increment(key: JSONValue, amount: number = 1): Promise<number> {\n return this.provider.increment(this.generateKey(key), amount);\n }\n\n /**\n * Gets a value from cache or executes callback and caches the result.\n * This is useful for caching expensive operations.\n * @template T - The expected type of the value\n * @param key - The cache key\n * @param callback - Function to execute if cache miss occurs\n * @param options - Cache options including TTL (default: 3600 seconds)\n * @returns The cached value or the result of the callback\n */\n async remember<T>(\n key: JSONValue,\n callback: () => Promise<T>,\n options: cacheOptions = {}\n ): Promise<T> {\n options.ttl = options.ttl ?? 3600; // default TTL 1 hour\n\n const cached = await this.get<T>(key);\n if (cached) {\n return cached;\n }\n\n const result = await callback();\n await this.put(key, result, options.ttl);\n return result;\n }\n\n /**\n * Generates a cache key by serializing and hashing complex keys.\n * Simple string keys under 250 characters are returned as-is.\n * Complex keys (objects, arrays, long strings) are MD5 hashed.\n * @param key - The key to generate a cache key from\n * @returns A string suitable for use as a cache key\n */\n generateKey(key: JSONValue): string {\n if (typeof key === 'string' && key.length <= 250) {\n return key;\n }\n return createHash('md5').update(JSON.stringify(key)).digest('hex');\n }\n}\n"],"mappings":";;AAEA,SAAS,kBAAk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