@mulingai-npm/redis 3.7.1 → 3.8.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.
@@ -5,6 +5,9 @@ export interface CachedPageContent {
5
5
  locale: string;
6
6
  slug: string;
7
7
  payload: string;
8
+ createdAt: number;
9
+ lastUsedAt: number | null;
10
+ totalUsage: number;
8
11
  }
9
12
  export declare class PageContentManager {
10
13
  private redis;
@@ -12,7 +15,7 @@ export declare class PageContentManager {
12
15
  constructor(redis: RedisClient);
13
16
  private key;
14
17
  private build;
15
- private scanAll;
18
+ private scan;
16
19
  addPageContent(locale: string, slug: string, payload: unknown, ttl?: number): Promise<void>;
17
20
  getPageContent(locale: string, slug: string): Promise<CachedPageContent | null>;
18
21
  getAllPageContents(): Promise<CachedPageContent[]>;
@@ -20,4 +23,5 @@ export declare class PageContentManager {
20
23
  clearPageContent(locale: string, slug: string): Promise<void>;
21
24
  clearAllPageContents(): Promise<void>;
22
25
  clearAllPageContentsByLocale(locale: string): Promise<void>;
26
+ incrementUsage(locale: string, slug: string): Promise<void>;
23
27
  }
@@ -10,17 +10,27 @@ class PageContentManager {
10
10
  }
11
11
  build(locale, slug, payload) {
12
12
  const id = this.key(locale, slug);
13
- return { id, path: `/${locale}/${slug}`, locale, slug, payload: JSON.stringify(payload) };
13
+ const now = Date.now();
14
+ return {
15
+ id,
16
+ path: `/${locale}/${slug}`,
17
+ locale,
18
+ slug,
19
+ payload: JSON.stringify(payload),
20
+ createdAt: now,
21
+ lastUsedAt: null,
22
+ totalUsage: 0
23
+ };
14
24
  }
15
- async scanAll() {
16
- let cursor = '0';
17
- const out = [];
25
+ async scan(pattern) {
26
+ let cursor = 0;
27
+ const keys = [];
18
28
  do {
19
- const [next, keys] = await this.redis.scan(Number(cursor), 'page-content--[*');
20
- cursor = next;
21
- out.push(...keys);
22
- } while (cursor !== '0');
23
- return out;
29
+ const [next, batch] = await this.redis.scan(cursor, pattern);
30
+ cursor = Number(next);
31
+ keys.push(...batch);
32
+ } while (cursor !== 0);
33
+ return keys;
24
34
  }
25
35
  async addPageContent(locale, slug, payload, ttl = PageContentManager.DEFAULT_TTL) {
26
36
  const obj = this.build(locale, slug, payload);
@@ -32,31 +42,53 @@ class PageContentManager {
32
42
  return raw ? JSON.parse(raw) : null;
33
43
  }
34
44
  async getAllPageContents() {
35
- const keys = await this.scanAll();
45
+ const keys = await this.scan('page-content--[*');
36
46
  if (!keys.length)
37
47
  return [];
38
48
  const p = this.redis.pipeline();
39
49
  keys.forEach((k) => p.get(k));
40
- const r = await p.exec();
41
- return r.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
50
+ const res = await p.exec();
51
+ return res.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
42
52
  }
43
53
  async getAllPageContentsByLocale(locale) {
44
- const all = await this.getAllPageContents();
45
- return all.filter((c) => c.locale.toLowerCase() === locale.toLowerCase());
54
+ const keys = await this.scan(`page-content--[${locale.toLowerCase()}]-[*`);
55
+ if (!keys.length)
56
+ return [];
57
+ const p = this.redis.pipeline();
58
+ keys.forEach((k) => p.get(k));
59
+ const res = await p.exec();
60
+ return res.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
46
61
  }
47
62
  async clearPageContent(locale, slug) {
48
63
  await this.redis.unlink(this.key(locale, slug));
49
64
  }
50
65
  async clearAllPageContents() {
51
- const keys = await this.scanAll();
66
+ const keys = await this.scan('page-content--[*');
52
67
  if (keys.length)
53
68
  await this.redis.unlink(...keys);
54
69
  }
55
70
  async clearAllPageContentsByLocale(locale) {
56
- const keys = await this.scanAll();
57
- const toDel = keys.filter((k) => k.startsWith(`page-content--[${locale.toLowerCase()}]-[`));
58
- if (toDel.length)
59
- await this.redis.unlink(...toDel);
71
+ const keys = await this.scan(`page-content--[${locale.toLowerCase()}]-[*`);
72
+ if (keys.length)
73
+ await this.redis.unlink(...keys);
74
+ }
75
+ async incrementUsage(locale, slug) {
76
+ var _a;
77
+ const k = this.key(locale, slug);
78
+ const pipe = this.redis.pipeline();
79
+ pipe.get(k);
80
+ pipe.ttl(k);
81
+ const [getRes, ttlRes] = (await pipe.exec());
82
+ const raw = getRes === null || getRes === void 0 ? void 0 : getRes[1];
83
+ if (!raw)
84
+ return;
85
+ const ttl = Number((_a = ttlRes === null || ttlRes === void 0 ? void 0 : ttlRes[1]) !== null && _a !== void 0 ? _a : -1);
86
+ const obj = JSON.parse(raw);
87
+ obj.totalUsage += 1;
88
+ obj.lastUsedAt = Date.now();
89
+ await this.redis.set(k, JSON.stringify(obj));
90
+ if (ttl > 0)
91
+ await this.redis.expire(k, ttl);
60
92
  }
61
93
  }
62
94
  exports.PageContentManager = PageContentManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "3.7.1",
3
+ "version": "3.8.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {