@avanio/expire-cache
Advanced tools
Comparing version 0.4.0 to 0.5.0
import { LogMapping, MapLogger, ILoggerLike } from '@avanio/logger-like'; | ||
import { ICache as ICache$1 } from '@luolapeikko/cache-types'; | ||
@@ -9,2 +10,3 @@ /** | ||
* Synchronous cache interface | ||
* @deprecated Use ```import {type ICache} from '@luolapeikko/cache-types';``` instead. | ||
* @example | ||
@@ -97,2 +99,3 @@ * function foo(cache: ICache<string>) { | ||
* Asynchronous cache interface | ||
* @deprecated Use ```import {type IAsyncCache} from '@luolapeikko/cache-types';``` instead. | ||
* @example | ||
@@ -183,2 +186,3 @@ * function foo(cache: IAsyncCache<string>) { | ||
* This type can be used to create functions that work with both types of cache interfaces. | ||
* @deprecated Use ```import {type IAsyncCache} from '@luolapeikko/cache-types';``` instead. | ||
* @example | ||
@@ -225,4 +229,5 @@ * function foo(cache: ICacheOrAsync<string>) { | ||
*/ | ||
declare class ExpireCache<Payload, Key = string> extends MapLogger<ExpireCacheLogMapType> implements ICache<Payload, Key> { | ||
declare class ExpireCache<Payload, Key = string> extends MapLogger<ExpireCacheLogMapType> implements ICache$1<Payload, Key> { | ||
private cache; | ||
private cacheTtl; | ||
private handleOnClear; | ||
@@ -248,2 +253,6 @@ private defaultExpireMs; | ||
values(): IterableIterator<Payload>; | ||
/** | ||
* Sets the default expiration time in milliseconds | ||
* @param expireMs - The default expiration time in milliseconds | ||
*/ | ||
setExpireMs(expireMs: number | undefined): void; | ||
@@ -255,3 +264,2 @@ /** | ||
private notifyOnClear; | ||
private cacheAsKeyPayloadMap; | ||
} | ||
@@ -264,4 +272,5 @@ | ||
*/ | ||
declare class ExpireTimeoutCache<Payload, Key = string> extends MapLogger<ExpireCacheLogMapType> implements ICache<Payload, Key> { | ||
declare class ExpireTimeoutCache<Payload, Key = string> extends MapLogger<ExpireCacheLogMapType> implements ICache$1<Payload, Key> { | ||
private cache; | ||
private cacheTimeout; | ||
private handleOnClear; | ||
@@ -287,8 +296,12 @@ private defaultExpireMs; | ||
values(): IterableIterator<Payload>; | ||
/** | ||
* Set the default expiration time in milliseconds | ||
* @param expireMs - The default expiration time in milliseconds | ||
*/ | ||
setExpireMs(expireMs: number | undefined): void; | ||
private clearTimeout; | ||
private notifyOnClear; | ||
private cacheAsKeyPayloadMap; | ||
private getExpireDate; | ||
} | ||
export { ExpireCache, type ExpireCacheLogMapType, ExpireTimeoutCache, type IAsyncCache, type IAsyncCacheOnClearCallback, type ICache, type ICacheOnClearCallback, type ICacheOrAsync }; |
@@ -25,2 +25,3 @@ // src/ExpireCache.ts | ||
this.cache = /* @__PURE__ */ new Map(); | ||
this.cacheTtl = /* @__PURE__ */ new Map(); | ||
this.handleOnClear = /* @__PURE__ */ new Set(); | ||
@@ -34,9 +35,9 @@ this.logKey("constructor", `ExpireCache created, defaultExpireMs: ${String(defaultExpireMs)}`); | ||
this.logKey("set", `ExpireCache set key: ${String(key)}, expireTs: ${String(expireTs)}`); | ||
this.cache.set(key, { data, expires: expireTs }); | ||
this.cache.set(key, data); | ||
this.cacheTtl.set(key, expireTs); | ||
} | ||
get(key) { | ||
var _a; | ||
this.logKey("get", `ExpireCache get key: ${String(key)}`); | ||
this.cleanExpired(); | ||
return (_a = this.cache.get(key)) == null ? void 0 : _a.data; | ||
return this.cache.get(key); | ||
} | ||
@@ -50,5 +51,5 @@ has(key) { | ||
this.logKey("expires", `ExpireCache get expire for key: ${String(key)}`); | ||
const entry = this.cache.get(key); | ||
const expires = this.cacheTtl.get(key); | ||
this.cleanExpired(); | ||
return (entry == null ? void 0 : entry.expires) ? new Date(entry.expires) : void 0; | ||
return expires ? new Date(expires) : void 0; | ||
} | ||
@@ -59,4 +60,5 @@ delete(key) { | ||
if (entry) { | ||
this.notifyOnClear(/* @__PURE__ */ new Map([[key, entry.data]])); | ||
this.notifyOnClear(/* @__PURE__ */ new Map([[key, entry]])); | ||
} | ||
this.cacheTtl.delete(key); | ||
return this.cache.delete(key); | ||
@@ -66,4 +68,5 @@ } | ||
this.logKey("clear", `ExpireCache clear`); | ||
this.notifyOnClear(this.cacheAsKeyPayloadMap()); | ||
this.notifyOnClear(new Map(this.cache)); | ||
this.cache.clear(); | ||
this.cacheTtl.clear(); | ||
} | ||
@@ -80,12 +83,16 @@ size() { | ||
this.cleanExpired(); | ||
return this.cacheAsKeyPayloadMap().entries(); | ||
return new Map(this.cache).entries(); | ||
} | ||
keys() { | ||
this.cleanExpired(); | ||
return this.cacheAsKeyPayloadMap().keys(); | ||
return new Map(this.cache).keys(); | ||
} | ||
values() { | ||
this.cleanExpired(); | ||
return this.cacheAsKeyPayloadMap().values(); | ||
return new Map(this.cache).values(); | ||
} | ||
/** | ||
* Sets the default expiration time in milliseconds | ||
* @param expireMs - The default expiration time in milliseconds | ||
*/ | ||
setExpireMs(expireMs) { | ||
@@ -100,6 +107,10 @@ this.defaultExpireMs = expireMs; | ||
const deleteEntries = /* @__PURE__ */ new Map(); | ||
for (const [key, value] of this.cache.entries()) { | ||
if (value.expires !== void 0 && value.expires < now) { | ||
deleteEntries.set(key, value.data); | ||
this.cache.delete(key); | ||
for (const [key, expire] of this.cacheTtl.entries()) { | ||
if (expire !== void 0 && expire < now) { | ||
const value = this.cache.get(key); | ||
if (value) { | ||
deleteEntries.set(key, value); | ||
this.cache.delete(key); | ||
} | ||
this.cacheTtl.delete(key); | ||
} | ||
@@ -115,9 +126,9 @@ } | ||
} | ||
cacheAsKeyPayloadMap() { | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const [key, value] of this.cache.entries()) { | ||
map.set(key, value.data); | ||
} | ||
return map; | ||
} | ||
/* private cacheAsKeyPayloadMap(): Map<Key, Payload> { | ||
const map = new Map<Key, Payload>(); | ||
for (const [key, value] of this.cache.entries()) { | ||
map.set(key, value.data); | ||
} | ||
return map; | ||
} */ | ||
}; | ||
@@ -149,2 +160,3 @@ | ||
this.cache = /* @__PURE__ */ new Map(); | ||
this.cacheTimeout = /* @__PURE__ */ new Map(); | ||
this.handleOnClear = /* @__PURE__ */ new Set(); | ||
@@ -156,4 +168,3 @@ this.logKey("constructor", `ExpireTimeoutCache created, defaultExpireMs: ${String(defaultExpireMs)}`); | ||
this.clearTimeout(key); | ||
const defaultExpireDate = this.defaultExpireMs ? new Date(Date.now() + this.defaultExpireMs) : void 0; | ||
const expiresDate = expires != null ? expires : defaultExpireDate; | ||
const expiresDate = this.getExpireDate(expires); | ||
const expireTs = expiresDate && expiresDate.getTime() - Date.now(); | ||
@@ -163,8 +174,8 @@ const expireString = expireTs ? `${expireTs.toString()} ms` : "undefined"; | ||
const timeout = expireTs ? setTimeout(() => this.delete(key), expireTs) : void 0; | ||
this.cache.set(key, { data, timeout, expires: expiresDate }); | ||
this.cache.set(key, data); | ||
this.cacheTimeout.set(key, { timeout, expires: expiresDate }); | ||
} | ||
get(key) { | ||
var _a; | ||
this.logKey("get", `ExpireTimeoutCache get key: ${String(key)}`); | ||
return (_a = this.cache.get(key)) == null ? void 0 : _a.data; | ||
return this.cache.get(key); | ||
} | ||
@@ -176,5 +187,5 @@ has(key) { | ||
expires(key) { | ||
var _a; | ||
this.logKey("expires", `ExpireTimeoutCache get expire for key: ${String(key)}`); | ||
const entry = this.cache.get(key); | ||
return entry == null ? void 0 : entry.expires; | ||
return (_a = this.cacheTimeout.get(key)) == null ? void 0 : _a.expires; | ||
} | ||
@@ -186,3 +197,3 @@ delete(key) { | ||
if (entry) { | ||
this.notifyOnClear(/* @__PURE__ */ new Map([[key, entry.data]])); | ||
this.notifyOnClear(/* @__PURE__ */ new Map([[key, entry]])); | ||
} | ||
@@ -193,5 +204,6 @@ return this.cache.delete(key); | ||
this.logKey("clear", `ExpireTimeoutCache clear`); | ||
this.cache.forEach((_value, key) => this.clearTimeout(key)); | ||
this.notifyOnClear(this.cacheAsKeyPayloadMap()); | ||
this.cacheTimeout.forEach((_value, key) => this.clearTimeout(key)); | ||
this.notifyOnClear(new Map(this.cache)); | ||
this.cache.clear(); | ||
this.cacheTimeout.clear(); | ||
} | ||
@@ -207,10 +219,14 @@ size() { | ||
entries() { | ||
return this.cacheAsKeyPayloadMap().entries(); | ||
return new Map(this.cache).entries(); | ||
} | ||
keys() { | ||
return this.cacheAsKeyPayloadMap().keys(); | ||
return new Map(this.cache).keys(); | ||
} | ||
values() { | ||
return this.cacheAsKeyPayloadMap().values(); | ||
return new Map(this.cache).values(); | ||
} | ||
/** | ||
* Set the default expiration time in milliseconds | ||
* @param expireMs - The default expiration time in milliseconds | ||
*/ | ||
setExpireMs(expireMs) { | ||
@@ -220,3 +236,3 @@ this.defaultExpireMs = expireMs; | ||
clearTimeout(key) { | ||
const entry = this.cache.get(key); | ||
const entry = this.cacheTimeout.get(key); | ||
if (entry == null ? void 0 : entry.timeout) { | ||
@@ -230,8 +246,5 @@ clearTimeout(entry.timeout); | ||
} | ||
cacheAsKeyPayloadMap() { | ||
const map = /* @__PURE__ */ new Map(); | ||
for (const [key, value] of this.cache.entries()) { | ||
map.set(key, value.data); | ||
} | ||
return map; | ||
getExpireDate(expires) { | ||
const defaultExpireDate = this.defaultExpireMs ? new Date(Date.now() + this.defaultExpireMs) : void 0; | ||
return expires != null ? expires : defaultExpireDate; | ||
} | ||
@@ -238,0 +251,0 @@ }; |
{ | ||
"name": "@avanio/expire-cache", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Typescript/Javascript cache with expiration", | ||
@@ -11,14 +11,7 @@ "type": "module", | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js", | ||
"types": "./dist/index.d.ts" | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --sourcemap --format cjs,esm --dts --clean", | ||
"test": "c8 mocha", | ||
"coverage": "c8 report --reporter=lcovonly", | ||
"lint": "eslint . --ext .ts", | ||
"validate": "tsc --noEmit --project tsconfig.test.json" | ||
}, | ||
"files": [ | ||
@@ -68,10 +61,12 @@ "dist" | ||
"devDependencies": { | ||
"@stylistic/eslint-plugin": "^2.6.1", | ||
"@types/chai": "^4.3.17", | ||
"@types/chai-as-promised": "^7.1.8", | ||
"@luolapeikko/cache-types": "^0.0.1", | ||
"@stylistic/eslint-plugin": "^2.7.2", | ||
"@stylistic/eslint-plugin-ts": "^2.7.2", | ||
"@types/chai": "^4.3.19", | ||
"@types/chai-as-promised": "^8.0.0", | ||
"@types/mocha": "^10.0.7", | ||
"@types/node": "^18.19.43", | ||
"@types/node": "^20.16.5", | ||
"@types/sinon": "^17.0.3", | ||
"@typescript-eslint/eslint-plugin": "^8.0.0", | ||
"@typescript-eslint/parser": "^8.0.0", | ||
"@typescript-eslint/eslint-plugin": "^8.4.0", | ||
"@typescript-eslint/parser": "^8.4.0", | ||
"c8": "^10.1.2", | ||
@@ -86,3 +81,3 @@ "chai": "^5.1.1", | ||
"eslint-plugin-sonarjs": "^0.23.0", | ||
"mocha": "^10.7.0", | ||
"mocha": "^10.7.3", | ||
"prettier": "^3.3.3", | ||
@@ -96,3 +91,13 @@ "sinon": "^18.0.0", | ||
"@avanio/logger-like": "^0.2.0" | ||
}, | ||
"peerDependencies": { | ||
"@luolapeikko/cache-types": "^0.0.1" | ||
}, | ||
"scripts": { | ||
"build": "tsup src/index.ts --sourcemap --format cjs,esm --dts --clean", | ||
"test": "c8 mocha", | ||
"coverage": "c8 report --reporter=lcovonly", | ||
"lint": "eslint . --ext .ts", | ||
"validate": "tsc --noEmit --project tsconfig.test.json" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
77688
793
0
2
25