@aomex/cache 0.0.6 → 0.0.8
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 +16 -0
- package/dist/index.d.ts +12 -2
- package/dist/index.js +36 -10
- package/dist/index.js.map +1 -1
- package/package.json +8 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @aomex/cache
|
|
2
2
|
|
|
3
|
+
## 0.0.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`43e3481`](https://github.com/aomex/aomex/commit/43e3481ff4edab59642b27527beb72e282b1768c) Thanks [@geekact](https://github.com/geekact)! - feat(cache): add method getAndDelete
|
|
8
|
+
|
|
9
|
+
- [`db8c690`](https://github.com/aomex/aomex/commit/db8c690ee820dac86ef5274b544fe49f3ca41d10) Thanks [@geekact](https://github.com/geekact)! - chore(cache): upgrade lru-cache from 8.0.0 to 9.0.0
|
|
10
|
+
|
|
11
|
+
## 0.0.7
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- [`bf2c963`](https://github.com/aomex/aomex/commit/bf2c9639a609402be2ff094ceabe4fd2231214f8) Thanks [@geekact](https://github.com/geekact)! - fix(cache): forget to return while calling addValue
|
|
16
|
+
|
|
17
|
+
- [`e3b1be4`](https://github.com/aomex/aomex/commit/e3b1be4d02793001d441a1f1b1ced060e95b4f8a) Thanks [@geekact](https://github.com/geekact)! - fix(cache): addValue may be incorrect with multiple threads
|
|
18
|
+
|
|
3
19
|
## 0.0.6
|
|
4
20
|
|
|
5
21
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { LRUCache } from 'lru-cache';
|
|
2
2
|
|
|
3
3
|
interface CacheOptions {
|
|
4
4
|
keyPrefix?: string;
|
|
@@ -19,6 +19,11 @@ declare abstract class Cache {
|
|
|
19
19
|
*/
|
|
20
20
|
get<T>(key: string, defaultValue: T): Promise<T>;
|
|
21
21
|
get<T extends string | number | object | boolean>(key: string): Promise<T | null>;
|
|
22
|
+
/**
|
|
23
|
+
* Get specific key and immediately delete this key from engine.
|
|
24
|
+
*/
|
|
25
|
+
getAndDelete<T>(key: string, defaultValue: T): Promise<T>;
|
|
26
|
+
getAndDelete<T extends string | number | object | boolean>(key: string): Promise<T | null>;
|
|
22
27
|
/**
|
|
23
28
|
* Get value from key, a set callback will be triggered when the value is `null`.
|
|
24
29
|
*
|
|
@@ -66,6 +71,7 @@ declare abstract class Cache {
|
|
|
66
71
|
protected abstract setValue(key: string, value: string, duration?: number): Promise<boolean>;
|
|
67
72
|
protected abstract deleteValue(key: string): Promise<boolean>;
|
|
68
73
|
protected abstract deleteAllValues(): Promise<boolean>;
|
|
74
|
+
protected parseValue(value: string | null, defaultValue?: string | number | object | boolean): any;
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
interface MemoryCacheOptions extends CacheOptions {
|
|
@@ -76,13 +82,17 @@ interface MemoryCacheOptions extends CacheOptions {
|
|
|
76
82
|
gcProbability?: number;
|
|
77
83
|
}
|
|
78
84
|
declare class MemoryCache extends Cache {
|
|
79
|
-
readonly lru:
|
|
85
|
+
readonly lru: LRUCache<string, string>;
|
|
80
86
|
protected readonly gcProbability: number;
|
|
81
87
|
constructor(options?: MemoryCacheOptions);
|
|
88
|
+
getAndDelete<T>(key: string, defaultValue: T): Promise<T>;
|
|
89
|
+
getAndDelete<T extends string | number | object | boolean>(key: string): Promise<T | null>;
|
|
82
90
|
protected existsKey(key: string): Promise<boolean>;
|
|
83
91
|
protected getValue(key: string): Promise<string | null>;
|
|
92
|
+
protected syncGetValue(key: string): string | null;
|
|
84
93
|
protected setValue(key: string, value: string, duration?: number): Promise<boolean>;
|
|
85
94
|
protected deleteValue(key: string): Promise<boolean>;
|
|
95
|
+
protected syncDeleteValue(key: string): boolean;
|
|
86
96
|
protected deleteAllValues(): Promise<boolean>;
|
|
87
97
|
protected gc(): void;
|
|
88
98
|
}
|
package/dist/index.js
CHANGED
|
@@ -14,14 +14,14 @@ var Cache = class {
|
|
|
14
14
|
async get(key, defaultValue) {
|
|
15
15
|
const hashKey = this.buildKey(key);
|
|
16
16
|
const result = await this.getValue(hashKey);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return null;
|
|
17
|
+
return this.parseValue(result, defaultValue);
|
|
18
|
+
}
|
|
19
|
+
async getAndDelete(key, defaultValue) {
|
|
20
|
+
const result = await this.get(key, defaultValue);
|
|
21
|
+
if (result !== null) {
|
|
22
|
+
await this.delete(key);
|
|
24
23
|
}
|
|
24
|
+
return result;
|
|
25
25
|
}
|
|
26
26
|
async getOrSet(key, orSet, duration) {
|
|
27
27
|
let value = await this.get(key);
|
|
@@ -75,26 +75,49 @@ var Cache = class {
|
|
|
75
75
|
return this.keyPrefix + hashKey;
|
|
76
76
|
}
|
|
77
77
|
async addValue(key, value, duration) {
|
|
78
|
-
|
|
78
|
+
if (await this.existsKey(key))
|
|
79
|
+
return false;
|
|
80
|
+
if (!await this.setValue(key, value, duration))
|
|
81
|
+
return false;
|
|
82
|
+
return await this.getValue(key) === value;
|
|
83
|
+
}
|
|
84
|
+
parseValue(value, defaultValue) {
|
|
85
|
+
if (value === null) {
|
|
86
|
+
return defaultValue === void 0 ? null : defaultValue;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(value);
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
79
93
|
}
|
|
80
94
|
};
|
|
81
95
|
|
|
82
96
|
// src/memory-cache.ts
|
|
83
|
-
import
|
|
97
|
+
import { LRUCache } from "lru-cache";
|
|
84
98
|
var MemoryCache = class extends Cache {
|
|
85
99
|
lru;
|
|
86
100
|
gcProbability;
|
|
87
101
|
constructor(options = {}) {
|
|
88
102
|
super(options);
|
|
89
103
|
this.gcProbability = (options.gcProbability ?? 10) / 100;
|
|
90
|
-
this.lru = new
|
|
104
|
+
this.lru = new LRUCache({
|
|
91
105
|
max: options.maxItems || 1e3
|
|
92
106
|
});
|
|
93
107
|
}
|
|
108
|
+
async getAndDelete(key, defaultValue) {
|
|
109
|
+
const hashKey = this.buildKey(key);
|
|
110
|
+
const result = this.syncGetValue(hashKey);
|
|
111
|
+
result !== null && this.syncDeleteValue(hashKey);
|
|
112
|
+
return this.parseValue(result, defaultValue);
|
|
113
|
+
}
|
|
94
114
|
async existsKey(key) {
|
|
95
115
|
return this.lru.has(key);
|
|
96
116
|
}
|
|
97
117
|
async getValue(key) {
|
|
118
|
+
return this.syncGetValue(key);
|
|
119
|
+
}
|
|
120
|
+
syncGetValue(key) {
|
|
98
121
|
const data = this.lru.get(key);
|
|
99
122
|
return data === void 0 ? null : data;
|
|
100
123
|
}
|
|
@@ -106,6 +129,9 @@ var MemoryCache = class extends Cache {
|
|
|
106
129
|
return true;
|
|
107
130
|
}
|
|
108
131
|
async deleteValue(key) {
|
|
132
|
+
return this.syncDeleteValue(key);
|
|
133
|
+
}
|
|
134
|
+
syncDeleteValue(key) {
|
|
109
135
|
return this.lru.delete(key);
|
|
110
136
|
}
|
|
111
137
|
async 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 * 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
|
|
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 return this.parseValue(result, defaultValue);\n }\n\n /**\n * Get specific key and immediately delete this key from engine.\n */\n async getAndDelete<T>(key: string, defaultValue: T): Promise<T>;\n async getAndDelete<T extends string | number | object | boolean>(\n key: string,\n ): Promise<T | null>;\n async getAndDelete(\n key: string,\n defaultValue?: string | number | object | boolean,\n ): Promise<any> {\n const result = await this.get(key, defaultValue);\n if (result !== null) {\n await this.delete(key);\n }\n return result;\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 if (await this.existsKey(key)) return false;\n if (!(await this.setValue(key, value, duration))) return false;\n return (await this.getValue(key)) === value;\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 protected parseValue(\n value: string | null,\n defaultValue?: string | number | object | boolean,\n ) {\n if (value === null) {\n return defaultValue === void 0 ? null : defaultValue;\n }\n\n try {\n return JSON.parse(value);\n } catch {\n return null;\n }\n }\n}\n","import { LRUCache } from 'lru-cache';\nimport { Cache, type 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 public readonly lru: LRUCache<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 LRUCache<string, string>({\n max: options.maxItems || 1000,\n });\n }\n\n override async getAndDelete<T>(key: string, defaultValue: T): Promise<T>;\n override async getAndDelete<T extends string | number | object | boolean>(\n key: string,\n ): Promise<T | null>;\n override async getAndDelete(\n key: string,\n defaultValue?: string | number | object | boolean,\n ): Promise<any> {\n const hashKey = this.buildKey(key);\n const result = this.syncGetValue(hashKey);\n result !== null && this.syncDeleteValue(hashKey);\n return this.parseValue(result, defaultValue);\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 return this.syncGetValue(key);\n }\n\n protected syncGetValue(key: string): 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.syncDeleteValue(key);\n }\n\n protected syncDeleteValue(key: string): 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,OAA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K,aAAa,GAAG;AAAA,EAC9B;AAAA,EAEU,aAAa,KAA4B;AACjD,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,gBAAgB,GAAG;AAAA,EACjC;AAAA,EAEU,gBAAgB,KAAsB;AAC9C,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.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,7 +26,12 @@
|
|
|
26
26
|
"homepage": "https://aomex.js.org",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"lru-cache": "^
|
|
29
|
+
"lru-cache": "^9.0.0"
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@aomex/utility": "^0.0.7"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "vitest"
|
|
36
|
+
}
|
|
32
37
|
}
|