@aomex/cache 0.0.2 → 0.0.4
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 +4 -6
- package/dist/index.d.ts +15 -21
- package/dist/index.js +14 -19
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
# @aomex/cache
|
|
2
2
|
|
|
3
|
-
## 0.0.
|
|
3
|
+
## 0.0.4
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- [`
|
|
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
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
## 0.0.1
|
|
9
|
+
## 0.0.3
|
|
12
10
|
|
|
13
11
|
### Patch Changes
|
|
14
12
|
|
|
15
|
-
- [`
|
|
13
|
+
- [`ceb2d82`](https://github.com/aomex/aomex/commit/ceb2d8269755dca3f79b755224259b8a99bc65d0) Thanks [@geekact](https://github.com/geekact)! - chore(cache,web): upgrade lru-cache from 7.14.1 to 7.17.0
|
package/dist/index.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ declare abstract class Cache {
|
|
|
5
5
|
protected readonly keyPrefix: string;
|
|
6
6
|
constructor(config: CacheOptions);
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Make sure key is not expired.
|
|
9
9
|
*/
|
|
10
10
|
exists(key: string): Promise<boolean>;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Get value from specific key and return `null` if defaultValue is not provided.
|
|
13
13
|
* ```typescript
|
|
14
14
|
* await cache.get('non-exist'); // null
|
|
15
15
|
* await cache.get('non-exist', 'my-value'); // my-value
|
|
@@ -18,49 +18,43 @@ declare abstract class Cache {
|
|
|
18
18
|
get<T>(key: string, defaultValue: T): Promise<T>;
|
|
19
19
|
get<T extends string | number | object | boolean>(key: string): Promise<T | null>;
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* 如果值存在,则立即返回。如果值不存在,则返回回调函数中获取的数据并写入缓存。
|
|
21
|
+
* Get value from key, a set callback will be triggered when the value is `null`.
|
|
24
22
|
*
|
|
25
23
|
* ```typescript
|
|
26
|
-
* await cache.
|
|
27
|
-
* //
|
|
28
|
-
* await cache.
|
|
29
|
-
* // 保质期3600ms,即一小时
|
|
30
|
-
* await cache.getOrSet('my-key', () => 'my-value', 3600);
|
|
24
|
+
* await cache.get('key'); // null
|
|
25
|
+
* await cache.getOrSet('key', () => 'value'); // 'value'
|
|
26
|
+
* await cache.get('key'); // 'value'
|
|
31
27
|
* ```
|
|
32
28
|
*/
|
|
33
29
|
getOrSet<T extends string | number | object | boolean>(key: string, orSet: () => T, duration?: number): Promise<T>;
|
|
34
30
|
getOrSet<T extends string | number | object | boolean>(key: string, orSet: () => Promise<T>, duration?: number): Promise<T>;
|
|
35
31
|
/**
|
|
36
|
-
*
|
|
32
|
+
* Set value
|
|
37
33
|
*
|
|
38
34
|
* ```typescript
|
|
39
35
|
* await cache.set('key', 'value');
|
|
40
|
-
* // 保质期3600ms,即一小时
|
|
41
36
|
* await cache.set('key', 'value', 3600);
|
|
42
37
|
* ```
|
|
43
38
|
*/
|
|
44
39
|
set(key: string, value: string | number | object | boolean, duration?: number): Promise<boolean>;
|
|
45
40
|
/**
|
|
46
|
-
*
|
|
41
|
+
* Set value if a key doesn't exists
|
|
42
|
+
*
|
|
47
43
|
* ```typescript
|
|
48
|
-
* //
|
|
44
|
+
* await cache.exists('key'); // false
|
|
49
45
|
* await cache.add('key', 'value'); // true
|
|
46
|
+
*
|
|
47
|
+
* await cache.exists('key'); // true
|
|
50
48
|
* await cache.add('key', 'value'); // false
|
|
51
|
-
* // 保质期3600ms,即一小时,一小时后允许再设置
|
|
52
|
-
* await cache.add('key-1', 'value', 3600); // true
|
|
53
|
-
* await sleep(3600);
|
|
54
|
-
* await cache.add('key-1', 'value'); // true
|
|
55
49
|
* ```
|
|
56
50
|
*/
|
|
57
51
|
add(key: string, value: any, duration?: number): Promise<boolean>;
|
|
58
52
|
/**
|
|
59
|
-
*
|
|
53
|
+
* Delete a key
|
|
60
54
|
*/
|
|
61
55
|
delete(key: string): Promise<boolean>;
|
|
62
56
|
/**
|
|
63
|
-
*
|
|
57
|
+
* Delete all keys
|
|
64
58
|
*/
|
|
65
59
|
deleteAll(): Promise<boolean>;
|
|
66
60
|
protected buildKey(key: string): string;
|
|
@@ -74,7 +68,7 @@ declare abstract class Cache {
|
|
|
74
68
|
|
|
75
69
|
interface MemoryCacheOptions extends CacheOptions {
|
|
76
70
|
/**
|
|
77
|
-
*
|
|
71
|
+
* Maximum key length, defaults `1000`
|
|
78
72
|
*/
|
|
79
73
|
maxItems?: number;
|
|
80
74
|
gcProbability?: number;
|
package/dist/index.js
CHANGED
|
@@ -6,14 +6,14 @@ var Cache = class {
|
|
|
6
6
|
this.keyPrefix = config.keyPrefix ?? "";
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
9
|
+
* Make sure key is not expired.
|
|
10
10
|
*/
|
|
11
11
|
async exists(key) {
|
|
12
12
|
return this.existsKey(this.buildKey(key));
|
|
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,50 +27,45 @@ 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
|
|
36
35
|
*
|
|
37
36
|
* ```typescript
|
|
38
37
|
* await cache.set('key', 'value');
|
|
39
|
-
* // 保质期3600ms,即一小时
|
|
40
38
|
* await cache.set('key', 'value', 3600);
|
|
41
39
|
* ```
|
|
42
40
|
*/
|
|
43
41
|
async set(key, value, duration) {
|
|
44
42
|
const hashKey = this.buildKey(key);
|
|
45
|
-
|
|
46
|
-
return this.setValue(hashKey, wrappedValue, duration);
|
|
43
|
+
return this.setValue(hashKey, JSON.stringify(value), duration);
|
|
47
44
|
}
|
|
48
45
|
/**
|
|
49
|
-
*
|
|
46
|
+
* Set value if a key doesn't exists
|
|
47
|
+
*
|
|
50
48
|
* ```typescript
|
|
51
|
-
* //
|
|
49
|
+
* await cache.exists('key'); // false
|
|
52
50
|
* await cache.add('key', 'value'); // true
|
|
51
|
+
*
|
|
52
|
+
* await cache.exists('key'); // true
|
|
53
53
|
* await cache.add('key', 'value'); // false
|
|
54
|
-
* // 保质期3600ms,即一小时,一小时后允许再设置
|
|
55
|
-
* await cache.add('key-1', 'value', 3600); // true
|
|
56
|
-
* await sleep(3600);
|
|
57
|
-
* await cache.add('key-1', 'value'); // true
|
|
58
54
|
* ```
|
|
59
55
|
*/
|
|
60
56
|
async add(key, value, duration) {
|
|
61
57
|
const hashKey = this.buildKey(key);
|
|
62
|
-
|
|
63
|
-
return this.addValue(hashKey, wrappedValue, duration);
|
|
58
|
+
return this.addValue(hashKey, JSON.stringify(value), duration);
|
|
64
59
|
}
|
|
65
60
|
/**
|
|
66
|
-
*
|
|
61
|
+
* Delete a key
|
|
67
62
|
*/
|
|
68
63
|
async delete(key) {
|
|
69
64
|
const hashKey = this.buildKey(key);
|
|
70
65
|
return this.deleteValue(hashKey);
|
|
71
66
|
}
|
|
72
67
|
/**
|
|
73
|
-
*
|
|
68
|
+
* Delete all keys
|
|
74
69
|
*/
|
|
75
70
|
async deleteAll() {
|
|
76
71
|
return this.deleteAllValues();
|
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 *
|
|
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;AAChC,SAAK,YAAY,OAAO,aAAa;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,KAA+B;AAC1C,WAAO,KAAK,UAAU,KAAK,SAAS,GAAG,CAAC;AAAA,EAC1C;AAAA,EAaA,MAAM,IACJ,KACA,cACc;AACd,UAAM,UAAU,KAAK,SAAS,GAAG;AACjC,UAAM,SAAS,MAAM,KAAK,SAAS,OAAO;AAE1C,QAAI,WAAW,MAAM;AACnB,aAAO,iBAAiB,SAAS,OAAO;AAAA,IAC1C;AAEA,QAAI;AACF,aAAO,KAAK,MAAM,MAAM;AAAA,IAC1B,QAAE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAqBA,MAAM,SACJ,KACA,OACA,UACY;AACZ,QAAI,QAAkB,MAAM,KAAK,IAAI,GAAG;AACxC,QAAI,UAAU;AAAM,aAAO;AAC3B,UAAM,KAAK,IAAI,KAAK,MAAM,MAAM,GAAG,QAA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;AAAA,EACH;AAAA,EAEA,MAAyB,UAAU,KAA+B;AAChE,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACzB;AAAA,EAEA,MAAgB,SAAS,KAAqC;AAC5D,UAAM,OAAO,KAAK,IAAI,IAAI,GAAG;AAC7B,WAAO,SAAS,SAAY,OAAO;AAAA,EACrC;AAAA,EAEA,MAAgB,SACd,KACA,OACA,UACkB;AAClB,SAAK,GAAG;AACR,SAAK,IAAI,IAAI,KAAK,OAAO;AAAA,MACvB,KAAK;AAAA,IACP,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EAEA,MAAgB,YAAY,KAA+B;AACzD,WAAO,KAAK,IAAI,OAAO,GAAG;AAAA,EAC5B;AAAA,EAEA,MAAgB,kBAAoC;AAClD,SAAK,IAAI,MAAM;AACf,WAAO;AAAA,EACT;AAAA,EAEU,KAAK;AACb,QAAI,KAAK,OAAO,IAAI,KAAK;AAAe;AACxC,SAAK,IAAI,WAAW;AAAA,EACtB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aomex/cache",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"homepage": "https://aomex.js.org",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"lru-cache": "^7.
|
|
29
|
+
"lru-cache": "^7.17.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {}
|
|
32
32
|
}
|