Comparing version
@@ -142,2 +142,6 @@ import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv'; | ||
}; | ||
type SetOptions = { | ||
ttl?: number | string; | ||
expire?: number | Date; | ||
}; | ||
declare class CacheableMemory extends Hookified { | ||
@@ -249,7 +253,8 @@ private _lru; | ||
* @param {any} value - The value to set | ||
* @param {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable. | ||
* @param {number|string|SetOptions} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable. | ||
* If you want to set expire directly you can do that by setting the expire property in the SetOptions. | ||
* If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live. | ||
* @returns {void} | ||
*/ | ||
set(key: string, value: any, ttl?: number | string): void; | ||
set(key: string, value: any, ttl?: number | string | SetOptions): void; | ||
/** | ||
@@ -404,2 +409,8 @@ * Sets the values of the keys | ||
} | ||
/** | ||
* Creates a new Keyv instance with a new KeyvCacheableMemory store. This also removes the serialize/deserialize methods from the Keyv instance for optimization. | ||
* @param options | ||
* @returns | ||
*/ | ||
declare function createKeyv(options?: KeyvCacheableMemoryOptions): Keyv; | ||
@@ -642,2 +653,2 @@ declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined; | ||
export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync }; | ||
export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableMemoryOptions, type CacheableOptions, CacheableStats, KeyvCacheableMemory, type WrapOptions, type WrapSyncOptions, createKeyv, shorthandToMilliseconds, shorthandToTime, wrap, wrapSync }; |
// src/index.ts | ||
import { Keyv } from "keyv"; | ||
import { Keyv as Keyv2 } from "keyv"; | ||
import { Hookified as Hookified2 } from "hookified"; | ||
@@ -72,2 +72,7 @@ | ||
// src/keyv-memory.ts | ||
import { | ||
Keyv | ||
} from "keyv"; | ||
// src/memory.ts | ||
@@ -455,3 +460,4 @@ import { Hookified } from "hookified"; | ||
* @param {any} value - The value to set | ||
* @param {number|string} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable. | ||
* @param {number|string|SetOptions} [ttl] - Time to Live - If you set a number it is miliseconds, if you set a string it is a human-readable. | ||
* If you want to set expire directly you can do that by setting the expire property in the SetOptions. | ||
* If you set undefined, it will use the default time-to-live. If both are undefined then it will not have a time-to-live. | ||
@@ -464,5 +470,17 @@ * @returns {void} | ||
if (ttl !== void 0 || this._ttl !== void 0) { | ||
const finalTtl = shorthandToTime(ttl ?? this._ttl); | ||
if (finalTtl !== void 0) { | ||
expires = finalTtl; | ||
if (typeof ttl === "object") { | ||
if (ttl.expire) { | ||
expires = typeof ttl.expire === "number" ? ttl.expire : ttl.expire.getTime(); | ||
} | ||
if (ttl.ttl) { | ||
const finalTtl = shorthandToTime(ttl.ttl); | ||
if (finalTtl !== void 0) { | ||
expires = finalTtl; | ||
} | ||
} | ||
} else { | ||
const finalTtl = shorthandToTime(ttl ?? this._ttl); | ||
if (finalTtl !== void 0) { | ||
expires = finalTtl; | ||
} | ||
} | ||
@@ -718,5 +736,8 @@ } | ||
if (this._checkInterval > 0) { | ||
if (this._interval) { | ||
clearInterval(this._interval); | ||
} | ||
this._interval = setInterval(() => { | ||
this.checkExpiration(); | ||
}, this._checkInterval); | ||
}, this._checkInterval).unref(); | ||
} | ||
@@ -857,2 +878,14 @@ } | ||
}; | ||
function createKeyv(options) { | ||
const store = new KeyvCacheableMemory(options); | ||
const namespace = options?.namespace; | ||
let ttl; | ||
if (options?.ttl && Number.isInteger(options.ttl)) { | ||
ttl = options?.ttl; | ||
} | ||
const keyv = new Keyv({ store, namespace, ttl }); | ||
keyv.serialize = void 0; | ||
keyv.deserialize = void 0; | ||
return keyv; | ||
} | ||
@@ -1079,3 +1112,3 @@ // src/stats.ts | ||
KeyvHooks, | ||
Keyv as Keyv2 | ||
Keyv as Keyv3 | ||
} from "keyv"; | ||
@@ -1098,3 +1131,3 @@ var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => { | ||
var Cacheable = class extends Hookified2 { | ||
_primary = new Keyv({ store: new KeyvCacheableMemory() }); | ||
_primary = createKeyv(); | ||
_secondary; | ||
@@ -1258,3 +1291,3 @@ _nonBlocking = false; | ||
setPrimary(primary) { | ||
this._primary = primary instanceof Keyv ? primary : new Keyv(primary); | ||
this._primary = primary instanceof Keyv2 ? primary : new Keyv2(primary); | ||
this._primary.on("error", (error) => { | ||
@@ -1270,3 +1303,3 @@ this.emit("error" /* ERROR */, error); | ||
setSecondary(secondary) { | ||
this._secondary = secondary instanceof Keyv ? secondary : new Keyv(secondary); | ||
this._secondary = secondary instanceof Keyv2 ? secondary : new Keyv2(secondary); | ||
this._secondary.on("error", (error) => { | ||
@@ -1631,5 +1664,6 @@ this.emit("error" /* ERROR */, error); | ||
CacheableStats, | ||
Keyv2 as Keyv, | ||
Keyv3 as Keyv, | ||
KeyvCacheableMemory, | ||
KeyvHooks, | ||
createKeyv, | ||
shorthandToMilliseconds, | ||
@@ -1636,0 +1670,0 @@ shorthandToTime, |
{ | ||
"name": "cacheable", | ||
"version": "1.8.5", | ||
"description": "Simple Caching Engine using Keyv", | ||
"version": "1.8.6", | ||
"description": "High Performance Layer 1 / Layer 2 Caching with Keyv Storage", | ||
"type": "module", | ||
@@ -24,14 +24,14 @@ "main": "./dist/index.cjs", | ||
"devDependencies": { | ||
"@keyv/redis": "^3.0.1", | ||
"@types/node": "^22.9.0", | ||
"@vitest/coverage-v8": "^2.1.4", | ||
"@keyv/redis": "^4.0.2", | ||
"@types/node": "^22.10.2", | ||
"@vitest/coverage-v8": "^2.1.8", | ||
"lru-cache": "^11.0.2", | ||
"rimraf": "^6.0.1", | ||
"tsup": "^8.3.5", | ||
"typescript": "^5.6.3", | ||
"vitest": "^2.1.4", | ||
"xo": "^0.59.3" | ||
"typescript": "^5.7.2", | ||
"vitest": "^2.1.8", | ||
"xo": "^0.60.0" | ||
}, | ||
"dependencies": { | ||
"hookified": "^1.5.0", | ||
"hookified": "^1.5.1", | ||
"keyv": "^5.2.1" | ||
@@ -71,2 +71,3 @@ }, | ||
"build": "rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean", | ||
"prepublish": "pnpm build", | ||
"test": "xo --fix && vitest run --coverage", | ||
@@ -73,0 +74,0 @@ "test:ci": "xo && vitest run", |
[<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable) | ||
# Cacheable | ||
> High Performance Layer 1 / Layer 2 Caching with Keyv Storage | ||
> Simple Caching Engine using Keyv | ||
[](https://codecov.io/gh/jaredwray/cacheable) | ||
@@ -378,3 +376,3 @@ [](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml) | ||
await keyv.set('foo', 'bar'); | ||
const value = await keyv.get('key'); | ||
const value = await keyv.get('foo'); | ||
console.log(value); // bar | ||
@@ -381,0 +379,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
165987
1.83%3988
1.97%385
-0.52%Updated