namirasoft-core
Advanced tools
Comparing version 1.4.43 to 1.4.44
import { IStorage } from "./IStorage"; | ||
export declare class CacheService<DataType> { | ||
private name; | ||
private storage; | ||
private duration_minutes; | ||
private storage; | ||
private getVersion; | ||
private getFromSource; | ||
private onExpired; | ||
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void); | ||
constructor(name: string, storage: IStorage, duration_minutes: number, getVersion: () => Promise<string>, getFromSource: () => Promise<DataType>); | ||
get(): Promise<DataType>; | ||
set(data: DataType): void; | ||
set(data: DataType, version?: string): Promise<void>; | ||
del(): void; | ||
} |
@@ -14,35 +14,33 @@ "use strict"; | ||
class CacheService { | ||
constructor(name, duration_minutes, storage, getFromSource, onExpired) { | ||
constructor(name, storage, duration_minutes, getVersion, getFromSource) { | ||
this.name = name; | ||
this.storage = storage; | ||
this.duration_minutes = duration_minutes; | ||
this.storage = storage; | ||
this.getVersion = getVersion; | ||
this.getFromSource = getFromSource; | ||
this.onExpired = onExpired; | ||
} | ||
get() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let ans; | ||
let version = yield this.getVersion(); | ||
let value = this.storage.get(this.name, ""); | ||
if (value) { | ||
let cache = JSON.parse(value); | ||
if (new Date(cache.expires_at) > new Date()) | ||
return cache.data; | ||
if (cache.version == version) | ||
if (new Date(cache.expires_at) > new Date()) | ||
return cache.data; | ||
} | ||
ans = yield this.getFromSource(); | ||
this.set(ans); | ||
let ans = yield this.getFromSource(); | ||
yield this.set(ans, version); | ||
return ans; | ||
}); | ||
} | ||
set(data) { | ||
let expires_at = new Date(); | ||
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes); | ||
let value = JSON.stringify({ data, expires_at }); | ||
this.storage.set(this.name, value); | ||
let sleep = expires_at.getTime() - new Date().getTime(); | ||
if (sleep <= 0) | ||
sleep = 5 * 1000; | ||
setTimeout(() => { | ||
if (this.onExpired) | ||
this.onExpired(); | ||
}, sleep); | ||
set(data, version) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!version) | ||
version = yield this.getVersion(); | ||
let expires_at = new Date(); | ||
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes); | ||
let value = JSON.stringify({ data, version, expires_at }); | ||
this.storage.set(this.name, value); | ||
}); | ||
} | ||
@@ -49,0 +47,0 @@ del() { |
@@ -11,3 +11,3 @@ { | ||
"private": false, | ||
"version": "1.4.43", | ||
"version": "1.4.44", | ||
"author": "Amir Abolhasani", | ||
@@ -21,3 +21,3 @@ "license": "MIT", | ||
"dependencies": { | ||
"@types/node": "^22.10.7", | ||
"@types/node": "^22.10.10", | ||
"axios": "^1.7.9", | ||
@@ -24,0 +24,0 @@ "buffer": "^6.0.3", |
@@ -5,2 +5,3 @@ import { IStorage } from "./IStorage"; | ||
{ | ||
version?: string; | ||
expires_at: Date; | ||
@@ -13,17 +14,19 @@ data: DataType; | ||
private name: string; | ||
private storage: IStorage; | ||
private duration_minutes: number; | ||
private storage: IStorage; | ||
private getVersion: () => Promise<string>; | ||
private getFromSource: () => Promise<DataType>; | ||
private onExpired: () => void; | ||
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void) | ||
// private onExpired: () => void; | ||
constructor(name: string, storage: IStorage, duration_minutes: number, getVersion: () => Promise<string>, getFromSource: () => Promise<DataType>) | ||
{ | ||
this.name = name; | ||
this.storage = storage; | ||
this.duration_minutes = duration_minutes; | ||
this.storage = storage; | ||
this.getVersion = getVersion; | ||
this.getFromSource = getFromSource; | ||
this.onExpired = onExpired; | ||
// this.onExpired = onExpired; | ||
} | ||
async get(): Promise<DataType> | ||
{ | ||
let ans: DataType; | ||
let version = await this.getVersion(); | ||
let value = this.storage.get(this.name, ""); | ||
@@ -33,24 +36,27 @@ if (value) | ||
let cache = JSON.parse(value) as NSCachStorage<DataType>; | ||
if (new Date(cache.expires_at) > new Date()) | ||
return cache.data; | ||
if (cache.version == version) | ||
if (new Date(cache.expires_at) > new Date()) | ||
return cache.data; | ||
} | ||
ans = await this.getFromSource(); | ||
this.set(ans); | ||
let ans = await this.getFromSource(); | ||
await this.set(ans, version); | ||
return ans; | ||
} | ||
set(data: DataType) | ||
async set(data: DataType, version?: string) | ||
{ | ||
if (!version) | ||
version = await this.getVersion(); | ||
let expires_at = new Date(); | ||
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes); | ||
let value = JSON.stringify({ data, expires_at }); | ||
let value = JSON.stringify({ data, version, expires_at }); | ||
this.storage.set(this.name, value); | ||
// | ||
let sleep = expires_at.getTime() - new Date().getTime(); | ||
if (sleep <= 0) | ||
sleep = 5 * 1000; | ||
setTimeout(() => | ||
{ | ||
if (this.onExpired) | ||
this.onExpired(); | ||
}, sleep); | ||
// let sleep = expires_at.getTime() - new Date().getTime(); | ||
// if (sleep <= 0) | ||
// sleep = 5 * 1000; | ||
// setTimeout(() => | ||
// { | ||
// if (this.onExpired) | ||
// this.onExpired(); | ||
// }, sleep); | ||
} | ||
@@ -57,0 +63,0 @@ del() |
Sorry, the diff of this file is not supported yet
434626
5804
Updated@types/node@^22.10.10