@aomex/cache 0.0.3 → 0.0.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/CHANGELOG.md +6 -0
- package/dist/index.js +5 -8
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @aomex/cache
|
|
2
2
|
|
|
3
|
+
## 0.0.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`9c0bb28`](https://github.com/aomex/aomex/commit/9c0bb285d25594310e5450634223100a01bf1b5d) Thanks [@geekact](https://github.com/geekact)! - feat(cache): getOrSet always returning fresh value from api
|
|
8
|
+
|
|
3
9
|
## 0.0.3
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
package/dist/index.js
CHANGED
|
@@ -13,7 +13,7 @@ var Cache = class {
|
|
|
13
13
|
}
|
|
14
14
|
async get(key, defaultValue) {
|
|
15
15
|
const hashKey = this.buildKey(key);
|
|
16
|
-
|
|
16
|
+
const result = await this.getValue(hashKey);
|
|
17
17
|
if (result === null) {
|
|
18
18
|
return defaultValue === void 0 ? null : defaultValue;
|
|
19
19
|
}
|
|
@@ -27,9 +27,8 @@ var Cache = class {
|
|
|
27
27
|
let value = await this.get(key);
|
|
28
28
|
if (value !== null)
|
|
29
29
|
return value;
|
|
30
|
-
|
|
31
|
-
await this.
|
|
32
|
-
return value;
|
|
30
|
+
await this.set(key, await orSet(), duration);
|
|
31
|
+
return await this.get(key);
|
|
33
32
|
}
|
|
34
33
|
/**
|
|
35
34
|
* Set value
|
|
@@ -41,8 +40,7 @@ var Cache = class {
|
|
|
41
40
|
*/
|
|
42
41
|
async set(key, value, duration) {
|
|
43
42
|
const hashKey = this.buildKey(key);
|
|
44
|
-
|
|
45
|
-
return this.setValue(hashKey, wrappedValue, duration);
|
|
43
|
+
return this.setValue(hashKey, JSON.stringify(value), duration);
|
|
46
44
|
}
|
|
47
45
|
/**
|
|
48
46
|
* Set value if a key doesn't exists
|
|
@@ -57,8 +55,7 @@ var Cache = class {
|
|
|
57
55
|
*/
|
|
58
56
|
async add(key, value, duration) {
|
|
59
57
|
const hashKey = this.buildKey(key);
|
|
60
|
-
|
|
61
|
-
return this.addValue(hashKey, wrappedValue, duration);
|
|
58
|
+
return this.addValue(hashKey, JSON.stringify(value), duration);
|
|
62
59
|
}
|
|
63
60
|
/**
|
|
64
61
|
* Delete a key
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cache.ts","../src/memory-cache.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\n\nexport interface CacheOptions {\n keyPrefix?: string;\n}\n\nexport abstract class Cache {\n protected readonly keyPrefix: string;\n\n constructor(config: CacheOptions) {\n this.keyPrefix = config.keyPrefix ?? '';\n }\n\n /**\n * Make sure key is not expired.\n */\n async exists(key: string): Promise<boolean> {\n return this.existsKey(this.buildKey(key));\n }\n\n /**\n * Get value from specific key and return `null` if defaultValue is not provided.\n * ```typescript\n * await cache.get('non-exist'); // null\n * await cache.get('non-exist', 'my-value'); // my-value\n * ```\n */\n async get<T>(key: string, defaultValue: T): Promise<T>;\n async get<T extends string | number | object | boolean>(\n key: string,\n ): Promise<T | null>;\n async get(\n key: string,\n defaultValue?: string | number | object | boolean,\n ): Promise<any> {\n const hashKey = this.buildKey(key);\n
|
|
1
|
+
{"version":3,"sources":["../src/cache.ts","../src/memory-cache.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\n\nexport interface CacheOptions {\n keyPrefix?: string;\n}\n\nexport abstract class Cache {\n protected readonly keyPrefix: string;\n\n constructor(config: CacheOptions) {\n this.keyPrefix = config.keyPrefix ?? '';\n }\n\n /**\n * Make sure key is not expired.\n */\n async exists(key: string): Promise<boolean> {\n return this.existsKey(this.buildKey(key));\n }\n\n /**\n * Get value from specific key and return `null` if defaultValue is not provided.\n * ```typescript\n * await cache.get('non-exist'); // null\n * await cache.get('non-exist', 'my-value'); // my-value\n * ```\n */\n async get<T>(key: string, defaultValue: T): Promise<T>;\n async get<T extends string | number | object | boolean>(\n key: string,\n ): Promise<T | null>;\n async get(\n key: string,\n defaultValue?: string | number | object | boolean,\n ): Promise<any> {\n const hashKey = this.buildKey(key);\n const result = await this.getValue(hashKey);\n\n if (result === null) {\n return defaultValue === void 0 ? null : defaultValue;\n }\n\n try {\n return JSON.parse(result);\n } catch {\n return null;\n }\n }\n\n /**\n * Get value from key, a set callback will be triggered when the value is `null`.\n *\n * ```typescript\n * await cache.get('key'); // null\n * await cache.getOrSet('key', () => 'value'); // 'value'\n * await cache.get('key'); // 'value'\n * ```\n */\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => T,\n duration?: number,\n ): Promise<T>;\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => Promise<T>,\n duration?: number,\n ): Promise<T>;\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => T | Promise<T>,\n duration?: number,\n ): Promise<T> {\n let value: T | null = await this.get(key);\n if (value !== null) return value;\n await this.set(key, await orSet(), duration);\n return (await this.get<T>(key))!;\n }\n\n /**\n * Set value\n *\n * ```typescript\n * await cache.set('key', 'value');\n * await cache.set('key', 'value', 3600);\n * ```\n */\n async set(\n key: string,\n value: string | number | object | boolean,\n duration?: number,\n ): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.setValue(hashKey, JSON.stringify(value), duration);\n }\n\n /**\n * Set value if a key doesn't exists\n *\n * ```typescript\n * await cache.exists('key'); // false\n * await cache.add('key', 'value'); // true\n *\n * await cache.exists('key'); // true\n * await cache.add('key', 'value'); // false\n * ```\n */\n async add(key: string, value: any, duration?: number): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.addValue(hashKey, JSON.stringify(value), duration);\n }\n\n /**\n * Delete a key\n */\n async delete(key: string): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.deleteValue(hashKey);\n }\n\n /**\n * Delete all keys\n */\n async deleteAll(): Promise<boolean> {\n return this.deleteAllValues();\n }\n\n protected buildKey(key: string): string {\n const hashKey =\n key.length <= 32 ? key : createHash('md5').update(key).digest('hex');\n return this.keyPrefix + hashKey;\n }\n\n protected async addValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n return (await this.existsKey(key))\n ? false\n : this.setValue(key, value, duration);\n }\n\n protected abstract existsKey(key: string): Promise<boolean>;\n protected abstract getValue(key: string): Promise<string | null>;\n protected abstract setValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean>;\n protected abstract deleteValue(key: string): Promise<boolean>;\n protected abstract deleteAllValues(): Promise<boolean>;\n}\n","import LRU from 'lru-cache';\nimport { Cache, CacheOptions } from './cache';\n\nexport interface MemoryCacheOptions extends CacheOptions {\n /**\n * Maximum key length, defaults `1000`\n */\n maxItems?: number;\n gcProbability?: number;\n}\n\nexport class MemoryCache extends Cache {\n private readonly lru: LRU<string, string>;\n protected readonly gcProbability: number;\n\n constructor(options: MemoryCacheOptions = {}) {\n super(options);\n this.gcProbability = (options.gcProbability ?? 10) / 100;\n this.lru = new LRU<string, string>({\n max: options.maxItems || 1000,\n });\n }\n\n protected override async existsKey(key: string): Promise<boolean> {\n return this.lru.has(key);\n }\n\n protected async getValue(key: string): Promise<string | null> {\n const data = this.lru.get(key);\n return data === undefined ? null : data;\n }\n\n protected async setValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n this.gc();\n this.lru.set(key, value, {\n ttl: duration,\n });\n return true;\n }\n\n protected async deleteValue(key: string): Promise<boolean> {\n return this.lru.delete(key);\n }\n\n protected async deleteAllValues(): Promise<boolean> {\n this.lru.clear();\n return true;\n }\n\n protected gc() {\n if (Math.random() > this.gcProbability) return;\n this.lru.purgeStale();\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAMpB,IAAe,QAAf,MAAqB;AAAA,EACP;AAAA,EAEnB,YAAY,QAAsB;AACh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