@jayfong/x-server 1.27.4 → 1.28.0
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 +7 -0
- package/lib/_cjs/services/cache.js +22 -0
- package/lib/services/cache.d.ts +1 -0
- package/lib/services/cache.js +22 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [1.28.0](https://github.com/jfWorks/x-server/compare/v1.27.4...v1.28.0) (2022-05-15)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **cache:** add rememberMany ([d6460d3](https://github.com/jfWorks/x-server/commit/d6460d363000700745834c02d145ec86658400d1))
|
|
11
|
+
|
|
5
12
|
### [1.27.4](https://github.com/jfWorks/x-server/compare/v1.27.3...v1.27.4) (2022-05-15)
|
|
6
13
|
|
|
7
14
|
|
|
@@ -115,6 +115,28 @@ class CacheService {
|
|
|
115
115
|
|
|
116
116
|
return value;
|
|
117
117
|
}
|
|
118
|
+
|
|
119
|
+
async rememberMany(raws, key, action, ttl = this.options.ttl) {
|
|
120
|
+
const keys = raws.map(key);
|
|
121
|
+
const values = await this.getMany(...keys);
|
|
122
|
+
const shouldUpdateIndexes = [];
|
|
123
|
+
|
|
124
|
+
for (let i = 0, len = values.length; i < len; i++) {
|
|
125
|
+
if (values[i] === null) {
|
|
126
|
+
shouldUpdateIndexes.push(i);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (shouldUpdateIndexes.length) {
|
|
131
|
+
const nextValues = await action(shouldUpdateIndexes.map(i => raws[i]));
|
|
132
|
+
await Promise.all(nextValues.map(async (nextValue, index) => {
|
|
133
|
+
values[shouldUpdateIndexes[index]] = nextValue;
|
|
134
|
+
await this.set(keys[shouldUpdateIndexes[index]], nextValue, ttl);
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return values;
|
|
139
|
+
}
|
|
118
140
|
/**
|
|
119
141
|
* 自增。
|
|
120
142
|
*
|
package/lib/services/cache.d.ts
CHANGED
|
@@ -61,6 +61,7 @@ export declare class CacheService<TData extends Data = Data, TKey extends Key<TD
|
|
|
61
61
|
* @returns 返回获取到的内容
|
|
62
62
|
*/
|
|
63
63
|
remember<K extends TKeyPath, V extends Value<TData, K>>(key: K, action: () => V | Promise<V>, ttl?: MsValue | ((data: V, defaultTTL: MsValue) => MsValue)): Promise<V>;
|
|
64
|
+
rememberMany<T, K extends TKeyPath, V extends Value<TData, K>>(raws: T[], key: (raw: T) => K, action: (raws: T[]) => V[] | Promise<V[]>, ttl?: MsValue | ((data: V, defaultTTL: MsValue) => MsValue)): Promise<V[]>;
|
|
64
65
|
/**
|
|
65
66
|
* 自增。
|
|
66
67
|
*
|
package/lib/services/cache.js
CHANGED
|
@@ -105,6 +105,28 @@ export class CacheService {
|
|
|
105
105
|
|
|
106
106
|
return value;
|
|
107
107
|
}
|
|
108
|
+
|
|
109
|
+
async rememberMany(raws, key, action, ttl = this.options.ttl) {
|
|
110
|
+
const keys = raws.map(key);
|
|
111
|
+
const values = await this.getMany(...keys);
|
|
112
|
+
const shouldUpdateIndexes = [];
|
|
113
|
+
|
|
114
|
+
for (let i = 0, len = values.length; i < len; i++) {
|
|
115
|
+
if (values[i] === null) {
|
|
116
|
+
shouldUpdateIndexes.push(i);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (shouldUpdateIndexes.length) {
|
|
121
|
+
const nextValues = await action(shouldUpdateIndexes.map(i => raws[i]));
|
|
122
|
+
await Promise.all(nextValues.map(async (nextValue, index) => {
|
|
123
|
+
values[shouldUpdateIndexes[index]] = nextValue;
|
|
124
|
+
await this.set(keys[shouldUpdateIndexes[index]], nextValue, ttl);
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return values;
|
|
129
|
+
}
|
|
108
130
|
/**
|
|
109
131
|
* 自增。
|
|
110
132
|
*
|