@mulingai-npm/redis 3.8.4 → 3.9.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.
|
@@ -3,7 +3,7 @@ export interface CachedPageContent {
|
|
|
3
3
|
id: string;
|
|
4
4
|
path: string;
|
|
5
5
|
locale: string;
|
|
6
|
-
|
|
6
|
+
route: string;
|
|
7
7
|
payload: string;
|
|
8
8
|
createdAt: number;
|
|
9
9
|
lastUsedAt: number | null;
|
|
@@ -16,12 +16,12 @@ export declare class PageContentManager {
|
|
|
16
16
|
private key;
|
|
17
17
|
private build;
|
|
18
18
|
private scan;
|
|
19
|
-
addPageContent(locale: string,
|
|
20
|
-
getPageContent(locale: string,
|
|
19
|
+
addPageContent(locale: string, route: string, payload: unknown, ttl?: number): Promise<void>;
|
|
20
|
+
getPageContent(locale: string, route: string): Promise<CachedPageContent | null>;
|
|
21
21
|
getAllPageContents(): Promise<CachedPageContent[]>;
|
|
22
22
|
getAllPageContentsByLocale(locale: string): Promise<CachedPageContent[]>;
|
|
23
|
-
clearPageContent(locale: string,
|
|
23
|
+
clearPageContent(locale: string, route: string): Promise<void>;
|
|
24
24
|
clearAllPageContents(): Promise<void>;
|
|
25
25
|
clearAllPageContentsByLocale(locale: string): Promise<void>;
|
|
26
|
-
incrementUsage(locale: string,
|
|
26
|
+
incrementUsage(locale: string, route: string): Promise<void>;
|
|
27
27
|
}
|
|
@@ -5,17 +5,17 @@ class PageContentManager {
|
|
|
5
5
|
constructor(redis) {
|
|
6
6
|
this.redis = redis;
|
|
7
7
|
}
|
|
8
|
-
key(locale,
|
|
9
|
-
return `page-content--[${locale.toLowerCase()}]-[${
|
|
8
|
+
key(locale, route) {
|
|
9
|
+
return `page-content--[${locale.toLowerCase()}]-[${route.toLowerCase()}]`;
|
|
10
10
|
}
|
|
11
|
-
build(locale,
|
|
12
|
-
const id = this.key(locale,
|
|
11
|
+
build(locale, route, payload) {
|
|
12
|
+
const id = this.key(locale, route);
|
|
13
13
|
const now = Date.now();
|
|
14
14
|
return {
|
|
15
15
|
id,
|
|
16
|
-
path: `/${locale}/${
|
|
16
|
+
path: `/${locale}/${route}`,
|
|
17
17
|
locale,
|
|
18
|
-
|
|
18
|
+
route,
|
|
19
19
|
payload: JSON.stringify(payload),
|
|
20
20
|
createdAt: now,
|
|
21
21
|
lastUsedAt: null,
|
|
@@ -32,13 +32,13 @@ class PageContentManager {
|
|
|
32
32
|
} while (cursor !== 0);
|
|
33
33
|
return keys;
|
|
34
34
|
}
|
|
35
|
-
async addPageContent(locale,
|
|
36
|
-
const obj = this.build(locale,
|
|
35
|
+
async addPageContent(locale, route, payload, ttl = PageContentManager.DEFAULT_TTL) {
|
|
36
|
+
const obj = this.build(locale, route, payload);
|
|
37
37
|
await this.redis.set(obj.id, JSON.stringify(obj));
|
|
38
38
|
await this.redis.expire(obj.id, ttl);
|
|
39
39
|
}
|
|
40
|
-
async getPageContent(locale,
|
|
41
|
-
const raw = await this.redis.get(this.key(locale,
|
|
40
|
+
async getPageContent(locale, route) {
|
|
41
|
+
const raw = await this.redis.get(this.key(locale, route));
|
|
42
42
|
return raw ? JSON.parse(raw) : null;
|
|
43
43
|
}
|
|
44
44
|
async getAllPageContents() {
|
|
@@ -59,8 +59,8 @@ class PageContentManager {
|
|
|
59
59
|
const res = await p.exec();
|
|
60
60
|
return res.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
|
|
61
61
|
}
|
|
62
|
-
async clearPageContent(locale,
|
|
63
|
-
await this.redis.unlink(this.key(locale,
|
|
62
|
+
async clearPageContent(locale, route) {
|
|
63
|
+
await this.redis.unlink(this.key(locale, route));
|
|
64
64
|
}
|
|
65
65
|
async clearAllPageContents() {
|
|
66
66
|
const keys = await this.scan('page-content--[*');
|
|
@@ -72,9 +72,9 @@ class PageContentManager {
|
|
|
72
72
|
if (keys.length)
|
|
73
73
|
await this.redis.unlink(...keys);
|
|
74
74
|
}
|
|
75
|
-
async incrementUsage(locale,
|
|
75
|
+
async incrementUsage(locale, route) {
|
|
76
76
|
var _a;
|
|
77
|
-
const k = this.key(locale,
|
|
77
|
+
const k = this.key(locale, route);
|
|
78
78
|
const pipe = this.redis.pipeline();
|
|
79
79
|
pipe.get(k);
|
|
80
80
|
pipe.ttl(k);
|