@mulingai-npm/redis 3.5.2 → 3.6.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.
@@ -0,0 +1,23 @@
1
+ import { RedisClient } from '../redis-client';
2
+ export interface CachedPageContent {
3
+ id: string;
4
+ path: string;
5
+ locale: string;
6
+ slug: string;
7
+ payload: string;
8
+ }
9
+ export declare class PageContentManager {
10
+ private redis;
11
+ private static DEFAULT_TTL;
12
+ constructor(redis: RedisClient);
13
+ private key;
14
+ private build;
15
+ private scanAll;
16
+ addPageContent(locale: string, slug: string, payload: unknown, ttl?: number): Promise<void>;
17
+ getPageContent(locale: string, slug: string): Promise<CachedPageContent | null>;
18
+ getAllPageContents(): Promise<CachedPageContent[]>;
19
+ getAllPageContentsByLocale(locale: string): Promise<CachedPageContent[]>;
20
+ clearPageContent(locale: string, slug: string): Promise<void>;
21
+ clearAllPageContents(): Promise<void>;
22
+ clearAllPageContentsByLocale(locale: string): Promise<void>;
23
+ }
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PageContentManager = void 0;
4
+ class PageContentManager {
5
+ constructor(redis) {
6
+ this.redis = redis;
7
+ }
8
+ key(locale, slug) {
9
+ return `page-content--[${locale.toLowerCase()}]-[${slug.toLowerCase()}]`;
10
+ }
11
+ build(locale, slug, payload) {
12
+ const id = this.key(locale, slug);
13
+ return { id, path: `/${locale}/${slug}`, locale, slug, payload: JSON.stringify(payload) };
14
+ }
15
+ async scanAll() {
16
+ let cursor = '0';
17
+ const out = [];
18
+ 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;
24
+ }
25
+ async addPageContent(locale, slug, payload, ttl = PageContentManager.DEFAULT_TTL) {
26
+ const obj = this.build(locale, slug, payload);
27
+ await this.redis.set(obj.id, JSON.stringify(obj));
28
+ await this.redis.expire(obj.id, ttl);
29
+ }
30
+ async getPageContent(locale, slug) {
31
+ const raw = await this.redis.get(this.key(locale, slug));
32
+ return raw ? JSON.parse(raw) : null;
33
+ }
34
+ async getAllPageContents() {
35
+ const keys = await this.scanAll();
36
+ if (!keys.length)
37
+ return [];
38
+ const p = this.redis.pipeline();
39
+ keys.forEach((k) => p.get(k));
40
+ const r = await p.exec();
41
+ return r.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
42
+ }
43
+ async getAllPageContentsByLocale(locale) {
44
+ const all = await this.getAllPageContents();
45
+ return all.filter((c) => c.locale.toLowerCase() === locale.toLowerCase());
46
+ }
47
+ async clearPageContent(locale, slug) {
48
+ await this.redis.unlink(this.key(locale, slug));
49
+ }
50
+ async clearAllPageContents() {
51
+ const keys = await this.scanAll();
52
+ if (keys.length)
53
+ await this.redis.unlink(...keys);
54
+ }
55
+ 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);
60
+ }
61
+ }
62
+ exports.PageContentManager = PageContentManager;
63
+ PageContentManager.DEFAULT_TTL = 24 * 60 * 60;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "3.5.2",
3
+ "version": "3.6.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {