namirasoft-core 1.4.43 → 1.4.45
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/dist/CacheService.d.ts +5 -4
- package/dist/CacheService.js +27 -20
- package/dist/CacheService.js.map +1 -1
- package/package.json +2 -2
- package/src/CacheService.ts +27 -20
package/dist/CacheService.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { IStorage } from "./IStorage";
|
|
2
2
|
export declare class CacheService<DataType> {
|
|
3
3
|
private name;
|
|
4
|
-
private duration_minutes;
|
|
5
4
|
private storage;
|
|
5
|
+
private duration_minutes;
|
|
6
|
+
private getVersion;
|
|
6
7
|
private getFromSource;
|
|
7
|
-
|
|
8
|
-
constructor(name: string,
|
|
8
|
+
onExpired?: () => void;
|
|
9
|
+
constructor(name: string, storage: IStorage, duration_minutes: number, getVersion: () => Promise<string>, getFromSource: () => Promise<DataType>);
|
|
9
10
|
get(): Promise<DataType>;
|
|
10
|
-
set(data: DataType): void
|
|
11
|
+
set(data: DataType, version?: string): Promise<void>;
|
|
11
12
|
del(): void;
|
|
12
13
|
}
|
package/dist/CacheService.js
CHANGED
|
@@ -11,39 +11,46 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.CacheService = void 0;
|
|
13
13
|
class CacheService {
|
|
14
|
-
constructor(name, duration_minutes,
|
|
14
|
+
constructor(name, storage, duration_minutes, getVersion, getFromSource) {
|
|
15
15
|
this.name = name;
|
|
16
|
-
this.duration_minutes = duration_minutes;
|
|
17
16
|
this.storage = storage;
|
|
17
|
+
this.duration_minutes = duration_minutes;
|
|
18
|
+
this.getVersion = getVersion;
|
|
18
19
|
this.getFromSource = getFromSource;
|
|
19
|
-
this.onExpired = onExpired;
|
|
20
20
|
}
|
|
21
21
|
get() {
|
|
22
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
let
|
|
23
|
+
let version = yield this.getVersion();
|
|
24
24
|
let value = this.storage.get(this.name, "");
|
|
25
25
|
if (value) {
|
|
26
26
|
let cache = JSON.parse(value);
|
|
27
|
-
if (
|
|
28
|
-
|
|
27
|
+
if (cache.version == version)
|
|
28
|
+
if (new Date(cache.expires_at) > new Date())
|
|
29
|
+
return cache.data;
|
|
29
30
|
}
|
|
30
|
-
ans = yield this.getFromSource();
|
|
31
|
-
this.set(ans);
|
|
31
|
+
let ans = yield this.getFromSource();
|
|
32
|
+
yield this.set(ans, version);
|
|
32
33
|
return ans;
|
|
33
34
|
});
|
|
34
35
|
}
|
|
35
|
-
set(data) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
set(data, version) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
if (!version)
|
|
39
|
+
version = yield this.getVersion();
|
|
40
|
+
let expires_at = new Date();
|
|
41
|
+
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
+
let value = JSON.stringify({ data, version, expires_at });
|
|
43
|
+
this.storage.set(this.name, value);
|
|
44
|
+
if (this.onExpired) {
|
|
45
|
+
let sleep = expires_at.getTime() - new Date().getTime();
|
|
46
|
+
if (sleep <= 0)
|
|
47
|
+
sleep = 5 * 1000;
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
var _a;
|
|
50
|
+
(_a = this.onExpired) === null || _a === void 0 ? void 0 : _a.call(this);
|
|
51
|
+
}, sleep);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
47
54
|
}
|
|
48
55
|
del() {
|
|
49
56
|
this.storage.del(this.name);
|
package/dist/CacheService.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CacheService.js","sourceRoot":"","sources":["../src/CacheService.ts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AC,UAAU,CAAC,UAAU,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAC;YACvE,IAAI,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC,CAAC;YAC1D,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAEnC,IAAI,IAAI,CAAC,SAAS,EAClB,CAAC;gBACG,IAAI,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;gBACxD,IAAI,KAAK,IAAI,CAAC;oBACV,KAAK,GAAG,CAAC,GAAG,IAAI,CAAC;gBACrB,UAAU,CAAC,GAAG,EAAE;;oBAEZ,MAAA,IAAI,CAAC,SAAS,oDAAI,CAAC;gBACvB,CAAC,EAAE,KAAK,CAAC,CAAC;YACd,CAAC;QACL,CAAC;KAAA;IACD,GAAG;QAEC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,CAAC;CACJ;AAvDD,oCAuDC"}
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"framework": "npm",
|
|
9
9
|
"application": "package",
|
|
10
10
|
"private": false,
|
|
11
|
-
"version": "1.4.
|
|
11
|
+
"version": "1.4.45",
|
|
12
12
|
"author": "Amir Abolhasani",
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"main": "./dist/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"build": ""
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@types/node": "^22.10.
|
|
20
|
+
"@types/node": "^22.10.10",
|
|
21
21
|
"axios": "^1.7.9",
|
|
22
22
|
"buffer": "^6.0.3",
|
|
23
23
|
"moment": "^2.30.1",
|
package/src/CacheService.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { IStorage } from "./IStorage";
|
|
|
2
2
|
|
|
3
3
|
interface NSCachStorage<DataType>
|
|
4
4
|
{
|
|
5
|
+
version?: string;
|
|
5
6
|
expires_at: Date;
|
|
6
7
|
data: DataType;
|
|
7
8
|
}
|
|
@@ -9,47 +10,53 @@ interface NSCachStorage<DataType>
|
|
|
9
10
|
export class CacheService<DataType>
|
|
10
11
|
{
|
|
11
12
|
private name: string;
|
|
12
|
-
private duration_minutes: number;
|
|
13
13
|
private storage: IStorage;
|
|
14
|
+
private duration_minutes: number;
|
|
15
|
+
private getVersion: () => Promise<string>;
|
|
14
16
|
private getFromSource: () => Promise<DataType>;
|
|
15
|
-
|
|
16
|
-
constructor(name: string,
|
|
17
|
+
public onExpired?: () => void;
|
|
18
|
+
constructor(name: string, storage: IStorage, duration_minutes: number, getVersion: () => Promise<string>, getFromSource: () => Promise<DataType>)
|
|
17
19
|
{
|
|
18
20
|
this.name = name;
|
|
19
|
-
this.duration_minutes = duration_minutes;
|
|
20
21
|
this.storage = storage;
|
|
22
|
+
this.duration_minutes = duration_minutes;
|
|
23
|
+
this.getVersion = getVersion;
|
|
21
24
|
this.getFromSource = getFromSource;
|
|
22
|
-
this.onExpired = onExpired;
|
|
23
25
|
}
|
|
24
26
|
async get(): Promise<DataType>
|
|
25
27
|
{
|
|
26
|
-
let
|
|
28
|
+
let version = await this.getVersion();
|
|
27
29
|
let value = this.storage.get(this.name, "");
|
|
28
30
|
if (value)
|
|
29
31
|
{
|
|
30
32
|
let cache = JSON.parse(value) as NSCachStorage<DataType>;
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
+
if (cache.version == version)
|
|
34
|
+
if (new Date(cache.expires_at) > new Date())
|
|
35
|
+
return cache.data;
|
|
33
36
|
}
|
|
34
|
-
ans = await this.getFromSource();
|
|
35
|
-
this.set(ans);
|
|
37
|
+
let ans = await this.getFromSource();
|
|
38
|
+
await this.set(ans, version);
|
|
36
39
|
return ans;
|
|
37
40
|
}
|
|
38
|
-
set(data: DataType)
|
|
41
|
+
async set(data: DataType, version?: string)
|
|
39
42
|
{
|
|
43
|
+
if (!version)
|
|
44
|
+
version = await this.getVersion();
|
|
40
45
|
let expires_at = new Date();
|
|
41
46
|
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
-
let value = JSON.stringify({ data, expires_at });
|
|
47
|
+
let value = JSON.stringify({ data, version, expires_at });
|
|
43
48
|
this.storage.set(this.name, value);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (sleep <= 0)
|
|
47
|
-
sleep = 5 * 1000;
|
|
48
|
-
setTimeout(() =>
|
|
49
|
+
|
|
50
|
+
if (this.onExpired)
|
|
49
51
|
{
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
let sleep = expires_at.getTime() - new Date().getTime();
|
|
53
|
+
if (sleep <= 0)
|
|
54
|
+
sleep = 5 * 1000;
|
|
55
|
+
setTimeout(() =>
|
|
56
|
+
{
|
|
57
|
+
this.onExpired?.();
|
|
58
|
+
}, sleep);
|
|
59
|
+
}
|
|
53
60
|
}
|
|
54
61
|
del()
|
|
55
62
|
{
|