@hile/cache 2.0.3 → 2.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/dist/define.d.ts +3 -1
- package/dist/define.js +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +25 -2
- package/package.json +2 -2
package/dist/define.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare class Cache<R> {
|
|
2
2
|
readonly data: R;
|
|
3
3
|
private _expire;
|
|
4
|
+
readonly __$flag__ = "cache";
|
|
4
5
|
constructor(data: R);
|
|
5
6
|
setExpire(seconds: number): this;
|
|
6
7
|
get expire(): number;
|
|
@@ -13,5 +14,6 @@ export type DefineCacheHandler<T extends string = string, R = any> = (opts: Extr
|
|
|
13
14
|
export type DefineCacheResult<T extends string = string, R = any> = {
|
|
14
15
|
fn: DefineCacheHandler<T, R>;
|
|
15
16
|
key: string;
|
|
17
|
+
fieldable: boolean;
|
|
16
18
|
};
|
|
17
|
-
export declare function defineCache<T extends string = string, R = any>(key: T, fn: DefineCacheHandler<T, R
|
|
19
|
+
export declare function defineCache<T extends string = string, R = any>(key: T, fn: DefineCacheHandler<T, R>, fieldable?: boolean): DefineCacheResult<T, R>;
|
package/dist/define.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export class Cache {
|
|
2
2
|
data;
|
|
3
3
|
_expire = 0; // 0: 永不过期
|
|
4
|
+
__$flag__ = 'cache';
|
|
4
5
|
constructor(data) {
|
|
5
6
|
this.data = data;
|
|
6
7
|
}
|
|
@@ -12,10 +13,11 @@ export class Cache {
|
|
|
12
13
|
return this._expire;
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
|
-
export function defineCache(key, fn) {
|
|
16
|
+
export function defineCache(key, fn, fieldable = false) {
|
|
16
17
|
return {
|
|
17
18
|
fn,
|
|
18
19
|
key,
|
|
20
|
+
fieldable,
|
|
19
21
|
};
|
|
20
22
|
}
|
|
21
23
|
// defineCache('user:{id:string}:ddd:{x:number}:idsaf:{y:boolean}', async (params) => {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DefineCacheResult, ExtractParams } from './define.js';
|
|
2
|
-
import { Redis } from 'ioredis';
|
|
2
|
+
import { ChainableCommander, Redis } from 'ioredis';
|
|
3
3
|
export * from './define.js';
|
|
4
4
|
export declare class RedisCache {
|
|
5
5
|
private readonly prefix;
|
|
@@ -7,6 +7,7 @@ export declare class RedisCache {
|
|
|
7
7
|
private readonly _regexp;
|
|
8
8
|
constructor(prefix: string, redis: Redis);
|
|
9
9
|
private makeKey;
|
|
10
|
+
private _multi;
|
|
10
11
|
private _write;
|
|
11
12
|
private _read;
|
|
12
13
|
private _remove;
|
|
@@ -16,5 +17,6 @@ export declare class RedisCache {
|
|
|
16
17
|
read: (params: ExtractParams<T>) => Promise<R | undefined>;
|
|
17
18
|
remove: (params: ExtractParams<T>) => Promise<number>;
|
|
18
19
|
has: (params: ExtractParams<T>) => Promise<boolean>;
|
|
20
|
+
multi: (params: ExtractParams<T>, callback: (multi: ChainableCommander, key: string) => unknown) => Promise<[error: Error | null, result: unknown][] | null>;
|
|
19
21
|
}>;
|
|
20
22
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Cache } from './define.js';
|
|
2
1
|
export * from './define.js';
|
|
3
2
|
export class RedisCache {
|
|
4
3
|
prefix;
|
|
@@ -11,10 +10,20 @@ export class RedisCache {
|
|
|
11
10
|
makeKey(key, options) {
|
|
12
11
|
return this.prefix + key.replace(this._regexp, (_, key) => String(options[key]));
|
|
13
12
|
}
|
|
13
|
+
async _multi(target, params, callback) {
|
|
14
|
+
const key = this.makeKey(target.key, params);
|
|
15
|
+
const redis = this.redis;
|
|
16
|
+
const exists = await redis.exists(key);
|
|
17
|
+
if (!exists)
|
|
18
|
+
await this._write(target, params);
|
|
19
|
+
const multi = redis.multi();
|
|
20
|
+
callback(multi, key);
|
|
21
|
+
return await multi.exec();
|
|
22
|
+
}
|
|
14
23
|
async _write(target, params) {
|
|
15
24
|
const key = this.makeKey(target.key, params);
|
|
16
25
|
const cache = await target.fn(params);
|
|
17
|
-
if (
|
|
26
|
+
if (cache.__$flag__ !== 'cache') {
|
|
18
27
|
throw new Error('Cache result must be an instance of Cache');
|
|
19
28
|
}
|
|
20
29
|
const redis = this.redis;
|
|
@@ -25,6 +34,13 @@ export class RedisCache {
|
|
|
25
34
|
}
|
|
26
35
|
return;
|
|
27
36
|
}
|
|
37
|
+
if (target.fieldable) {
|
|
38
|
+
await redis.hset(key, cache.data);
|
|
39
|
+
if (cache.expire > 0) {
|
|
40
|
+
await redis.expire(key, cache.expire);
|
|
41
|
+
}
|
|
42
|
+
return cache.data;
|
|
43
|
+
}
|
|
28
44
|
const payload = JSON.stringify(cache.data);
|
|
29
45
|
if (cache.expire > 0) {
|
|
30
46
|
await redis.setex(key, cache.expire, payload);
|
|
@@ -40,6 +56,10 @@ export class RedisCache {
|
|
|
40
56
|
const exists = await redis.exists(key);
|
|
41
57
|
if (!exists)
|
|
42
58
|
return await this._write(target, params);
|
|
59
|
+
if (target.fieldable) {
|
|
60
|
+
const fields = await redis.hgetall(key);
|
|
61
|
+
return fields;
|
|
62
|
+
}
|
|
43
63
|
const text = await redis.get(key);
|
|
44
64
|
if (!text)
|
|
45
65
|
return await this._write(target, params);
|
|
@@ -72,6 +92,9 @@ export class RedisCache {
|
|
|
72
92
|
},
|
|
73
93
|
has: async (params) => {
|
|
74
94
|
return await this._has(target, params);
|
|
95
|
+
},
|
|
96
|
+
multi: async (params, callback) => {
|
|
97
|
+
return await this._multi(target, params, callback);
|
|
75
98
|
}
|
|
76
99
|
};
|
|
77
100
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/cache",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"ioredis": "^5.11.0"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "ecfe460c6ccbd1c5cbe2189cb16d5b1e67aaff36"
|
|
27
27
|
}
|