@mulingai-npm/redis 3.9.1 → 3.10.1
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,25 @@
|
|
|
1
|
+
import { RedisClient } from '../redis-client';
|
|
2
|
+
export interface CachedPricingContent {
|
|
3
|
+
id: string;
|
|
4
|
+
locale: string;
|
|
5
|
+
pricingType: string;
|
|
6
|
+
country: string;
|
|
7
|
+
currency: string;
|
|
8
|
+
sku?: string;
|
|
9
|
+
payload: string;
|
|
10
|
+
createdAt: number;
|
|
11
|
+
lastUsedAt: number | null;
|
|
12
|
+
totalUsage: number;
|
|
13
|
+
}
|
|
14
|
+
export declare class PricingContentManager {
|
|
15
|
+
private redis;
|
|
16
|
+
private static DEFAULT_TTL;
|
|
17
|
+
constructor(redis: RedisClient);
|
|
18
|
+
private key;
|
|
19
|
+
private build;
|
|
20
|
+
private scan;
|
|
21
|
+
addPricingContent(locale: string, pricingType: string, country: string, currency: string, sku: string | undefined, payload: unknown, ttl?: number): Promise<void>;
|
|
22
|
+
getPricingContent(locale: string, pricingType: string, country: string, currency: string, sku: string | undefined): Promise<CachedPricingContent | null>;
|
|
23
|
+
clearPricingContent(locale: string, pricingType: string, country: string, currency: string, sku: string | undefined): Promise<void>;
|
|
24
|
+
incrementUsage(locale: string, pricingType: string, country: string, currency: string, sku: string | undefined): Promise<void>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PricingContentManager = void 0;
|
|
4
|
+
class PricingContentManager {
|
|
5
|
+
constructor(redis) {
|
|
6
|
+
this.redis = redis;
|
|
7
|
+
}
|
|
8
|
+
key(locale, pricingType, country, currency, sku) {
|
|
9
|
+
const parts = [locale.toLowerCase(), pricingType.toLowerCase(), country.toUpperCase(), currency.toUpperCase()];
|
|
10
|
+
if (sku) {
|
|
11
|
+
parts.push(sku.toLowerCase());
|
|
12
|
+
}
|
|
13
|
+
return `pricing--[${parts.join(']-[')}]`;
|
|
14
|
+
}
|
|
15
|
+
build(locale, pricingType, country, currency, sku, payload) {
|
|
16
|
+
const id = this.key(locale, pricingType, country, currency, sku);
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
return {
|
|
19
|
+
id,
|
|
20
|
+
locale,
|
|
21
|
+
pricingType,
|
|
22
|
+
country,
|
|
23
|
+
currency,
|
|
24
|
+
sku,
|
|
25
|
+
payload: JSON.stringify(payload),
|
|
26
|
+
createdAt: now,
|
|
27
|
+
lastUsedAt: null,
|
|
28
|
+
totalUsage: 0
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async scan(pattern) {
|
|
32
|
+
let cursor = 0;
|
|
33
|
+
const keys = [];
|
|
34
|
+
do {
|
|
35
|
+
const [next, batch] = await this.redis.scan(cursor, pattern);
|
|
36
|
+
cursor = Number(next);
|
|
37
|
+
keys.push(...batch);
|
|
38
|
+
} while (cursor !== 0);
|
|
39
|
+
return keys;
|
|
40
|
+
}
|
|
41
|
+
async addPricingContent(locale, pricingType, country, currency, sku, payload, ttl = PricingContentManager.DEFAULT_TTL) {
|
|
42
|
+
const obj = this.build(locale, pricingType, country, currency, sku, payload);
|
|
43
|
+
await this.redis.set(obj.id, JSON.stringify(obj));
|
|
44
|
+
await this.redis.expire(obj.id, ttl);
|
|
45
|
+
}
|
|
46
|
+
async getPricingContent(locale, pricingType, country, currency, sku) {
|
|
47
|
+
const raw = await this.redis.get(this.key(locale, pricingType, country, currency, sku));
|
|
48
|
+
return raw ? JSON.parse(raw) : null;
|
|
49
|
+
}
|
|
50
|
+
async clearPricingContent(locale, pricingType, country, currency, sku) {
|
|
51
|
+
await this.redis.unlink(this.key(locale, pricingType, country, currency, sku));
|
|
52
|
+
}
|
|
53
|
+
async incrementUsage(locale, pricingType, country, currency, sku) {
|
|
54
|
+
var _a;
|
|
55
|
+
const k = this.key(locale, pricingType, country, currency, sku);
|
|
56
|
+
const pipe = this.redis.pipeline();
|
|
57
|
+
pipe.get(k);
|
|
58
|
+
pipe.ttl(k);
|
|
59
|
+
const [getRes, ttlRes] = (await pipe.exec());
|
|
60
|
+
const raw = getRes === null || getRes === void 0 ? void 0 : getRes[1];
|
|
61
|
+
if (!raw) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const ttl = Number((_a = ttlRes === null || ttlRes === void 0 ? void 0 : ttlRes[1]) !== null && _a !== void 0 ? _a : -1);
|
|
65
|
+
const obj = JSON.parse(raw);
|
|
66
|
+
obj.totalUsage += 1;
|
|
67
|
+
obj.lastUsedAt = Date.now();
|
|
68
|
+
await this.redis.set(k, JSON.stringify(obj));
|
|
69
|
+
if (ttl > 0) {
|
|
70
|
+
await this.redis.expire(k, ttl);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.PricingContentManager = PricingContentManager;
|
|
75
|
+
PricingContentManager.DEFAULT_TTL = 2 * 60 * 60;
|