import { CacheItem } from './CacheItem';
import * as crypto from 'crypto';
export class Cache {
private _cache: { [key: string]: CacheItem } = {};
public set(key: string, data: any, expiration?: number | Date): void {
let cacheItem: CacheItem = undefined;
key = this.getHashKey(key);
Iif (!expiration) {
cacheItem = new CacheItem(data);
} else Eif (typeof expiration === 'number') {
let now: Date = new Date();
now.setSeconds(now.getSeconds() + expiration);
cacheItem = new CacheItem(data, now);
} else if (expiration instanceof Date) {
cacheItem = new CacheItem(data, expiration);
}
this._cache[key] = cacheItem;
}
public get<T>(key: string): T {
key = this.getHashKey(key);
let cacheItem: CacheItem = this._cache[key];
if (!cacheItem) {
return undefined;
}
Iif (!cacheItem.expiredOn) {
return cacheItem.data;
}
let now: Date = new Date();
Iif (now > cacheItem.expiredOn) {
this.remove(key);
return undefined;
} else {
return cacheItem.data;
}
}
public remove(key: string): void {
key = this.getHashKey(key);
delete this._cache[key];
}
public clear(): void {
this._cache = {};
}
private getHashKey(key: string): string {
return crypto.createHash('md5').update(key).digest('hex');
}
}
|