Comparing version
@@ -1,2 +0,2 @@ | ||
import { Keyv, KeyvStoreAdapter } from 'keyv'; | ||
import { KeyvStoreAdapter, StoredData, Keyv } from 'keyv'; | ||
import { Hookified } from 'hookified'; | ||
@@ -103,2 +103,17 @@ | ||
declare class KeyvCacheableMemory implements KeyvStoreAdapter { | ||
opts: CacheableMemoryOptions; | ||
namespace?: string | undefined; | ||
private readonly _cache; | ||
constructor(options?: CacheableMemoryOptions); | ||
get<Value>(key: string): Promise<StoredData<Value> | undefined>; | ||
set(key: string, value: any, ttl?: number): void; | ||
delete(key: string): Promise<boolean>; | ||
clear(): Promise<void>; | ||
has?(key: string): Promise<boolean>; | ||
getMany?<Value>(keys: string[]): Promise<Array<StoredData<Value | undefined>>>; | ||
deleteMany?(key: string[]): Promise<boolean>; | ||
on(event: string, listener: (...arguments_: any[]) => void): this; | ||
} | ||
declare enum CacheableHooks { | ||
@@ -164,2 +179,2 @@ BEFORE_SET = "BEFORE_SET", | ||
export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats }; | ||
export { Cacheable, CacheableEvents, CacheableHooks, type CacheableItem, CacheableMemory, type CacheableOptions, CacheableStats, KeyvCacheableMemory }; |
@@ -5,177 +5,2 @@ // src/index.ts | ||
// src/stats.ts | ||
var CacheableStats = class { | ||
_hits = 0; | ||
_misses = 0; | ||
_gets = 0; | ||
_sets = 0; | ||
_deletes = 0; | ||
_clears = 0; | ||
_vsize = 0; | ||
_ksize = 0; | ||
_count = 0; | ||
_enabled = false; | ||
constructor(options) { | ||
if (options?.enabled) { | ||
this._enabled = options.enabled; | ||
} | ||
} | ||
get enabled() { | ||
return this._enabled; | ||
} | ||
set enabled(enabled) { | ||
this._enabled = enabled; | ||
} | ||
get hits() { | ||
return this._hits; | ||
} | ||
get misses() { | ||
return this._misses; | ||
} | ||
get gets() { | ||
return this._gets; | ||
} | ||
get sets() { | ||
return this._sets; | ||
} | ||
get deletes() { | ||
return this._deletes; | ||
} | ||
get clears() { | ||
return this._clears; | ||
} | ||
get vsize() { | ||
return this._vsize; | ||
} | ||
get ksize() { | ||
return this._ksize; | ||
} | ||
get count() { | ||
return this._count; | ||
} | ||
incrementHits() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._hits++; | ||
} | ||
incrementMisses() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._misses++; | ||
} | ||
incrementGets() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._gets++; | ||
} | ||
incrementSets() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._sets++; | ||
} | ||
incrementDeletes() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._deletes++; | ||
} | ||
incrementClears() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._clears++; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
incrementVSize(value) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._vsize += this.roughSizeOfObject(value); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
decreaseVSize(value) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._vsize -= this.roughSizeOfObject(value); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
incrementKSize(key) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._ksize += this.roughSizeOfString(key); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
decreaseKSize(key) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._ksize -= this.roughSizeOfString(key); | ||
} | ||
incrementCount() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count++; | ||
} | ||
decreaseCount() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count--; | ||
} | ||
setCount(count) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count = count; | ||
} | ||
roughSizeOfString(value) { | ||
return value.length * 2; | ||
} | ||
roughSizeOfObject(object) { | ||
const objectList = []; | ||
const stack = [object]; | ||
let bytes = 0; | ||
while (stack.length > 0) { | ||
const value = stack.pop(); | ||
if (typeof value === "boolean") { | ||
bytes += 4; | ||
} else if (typeof value === "string") { | ||
bytes += value.length * 2; | ||
} else if (typeof value === "number") { | ||
bytes += 8; | ||
} else if (typeof value === "object" && value !== null && !objectList.includes(value)) { | ||
objectList.push(value); | ||
for (const key in value) { | ||
bytes += key.length * 2; | ||
stack.push(value[key]); | ||
} | ||
} | ||
} | ||
return bytes; | ||
} | ||
reset() { | ||
this._hits = 0; | ||
this._misses = 0; | ||
this._gets = 0; | ||
this._sets = 0; | ||
this._deletes = 0; | ||
this._clears = 0; | ||
this._vsize = 0; | ||
this._ksize = 0; | ||
this._count = 0; | ||
} | ||
resetStoreValues() { | ||
this._vsize = 0; | ||
this._ksize = 0; | ||
this._count = 0; | ||
} | ||
}; | ||
// src/memory-lru.ts | ||
@@ -513,2 +338,232 @@ var ListNode = class { | ||
// src/keyv-memory.ts | ||
var KeyvCacheableMemory = class { | ||
opts = { | ||
ttl: 0, | ||
useClone: true, | ||
lruSize: 0, | ||
checkInterval: 0 | ||
}; | ||
namespace; | ||
_cache = new CacheableMemory(); | ||
constructor(options) { | ||
if (options) { | ||
this.opts = options; | ||
this._cache = new CacheableMemory(options); | ||
} | ||
} | ||
async get(key) { | ||
const result = this._cache.get(key); | ||
console.log("result", result); | ||
if (result) { | ||
return result; | ||
} | ||
return void 0; | ||
} | ||
set(key, value, ttl) { | ||
this._cache.set(key, value, ttl); | ||
} | ||
async delete(key) { | ||
this._cache.delete(key); | ||
return true; | ||
} | ||
async clear() { | ||
this._cache.clear(); | ||
} | ||
async has(key) { | ||
return this._cache.has(key); | ||
} | ||
async getMany(keys) { | ||
const result = []; | ||
for (const key of keys) { | ||
result.push(this._cache.get(key)); | ||
} | ||
return result; | ||
} | ||
async deleteMany(key) { | ||
for (const k of key) { | ||
this._cache.delete(k); | ||
} | ||
return true; | ||
} | ||
on(event, listener) { | ||
return this; | ||
} | ||
}; | ||
// src/stats.ts | ||
var CacheableStats = class { | ||
_hits = 0; | ||
_misses = 0; | ||
_gets = 0; | ||
_sets = 0; | ||
_deletes = 0; | ||
_clears = 0; | ||
_vsize = 0; | ||
_ksize = 0; | ||
_count = 0; | ||
_enabled = false; | ||
constructor(options) { | ||
if (options?.enabled) { | ||
this._enabled = options.enabled; | ||
} | ||
} | ||
get enabled() { | ||
return this._enabled; | ||
} | ||
set enabled(enabled) { | ||
this._enabled = enabled; | ||
} | ||
get hits() { | ||
return this._hits; | ||
} | ||
get misses() { | ||
return this._misses; | ||
} | ||
get gets() { | ||
return this._gets; | ||
} | ||
get sets() { | ||
return this._sets; | ||
} | ||
get deletes() { | ||
return this._deletes; | ||
} | ||
get clears() { | ||
return this._clears; | ||
} | ||
get vsize() { | ||
return this._vsize; | ||
} | ||
get ksize() { | ||
return this._ksize; | ||
} | ||
get count() { | ||
return this._count; | ||
} | ||
incrementHits() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._hits++; | ||
} | ||
incrementMisses() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._misses++; | ||
} | ||
incrementGets() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._gets++; | ||
} | ||
incrementSets() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._sets++; | ||
} | ||
incrementDeletes() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._deletes++; | ||
} | ||
incrementClears() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._clears++; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
incrementVSize(value) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._vsize += this.roughSizeOfObject(value); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
decreaseVSize(value) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._vsize -= this.roughSizeOfObject(value); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
incrementKSize(key) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._ksize += this.roughSizeOfString(key); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
decreaseKSize(key) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._ksize -= this.roughSizeOfString(key); | ||
} | ||
incrementCount() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count++; | ||
} | ||
decreaseCount() { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count--; | ||
} | ||
setCount(count) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
this._count = count; | ||
} | ||
roughSizeOfString(value) { | ||
return value.length * 2; | ||
} | ||
roughSizeOfObject(object) { | ||
const objectList = []; | ||
const stack = [object]; | ||
let bytes = 0; | ||
while (stack.length > 0) { | ||
const value = stack.pop(); | ||
if (typeof value === "boolean") { | ||
bytes += 4; | ||
} else if (typeof value === "string") { | ||
bytes += value.length * 2; | ||
} else if (typeof value === "number") { | ||
bytes += 8; | ||
} else if (typeof value === "object" && value !== null && !objectList.includes(value)) { | ||
objectList.push(value); | ||
for (const key in value) { | ||
bytes += key.length * 2; | ||
stack.push(value[key]); | ||
} | ||
} | ||
} | ||
return bytes; | ||
} | ||
reset() { | ||
this._hits = 0; | ||
this._misses = 0; | ||
this._gets = 0; | ||
this._sets = 0; | ||
this._deletes = 0; | ||
this._clears = 0; | ||
this._vsize = 0; | ||
this._ksize = 0; | ||
this._count = 0; | ||
} | ||
resetStoreValues() { | ||
this._vsize = 0; | ||
this._ksize = 0; | ||
this._count = 0; | ||
} | ||
}; | ||
// src/index.ts | ||
@@ -531,3 +586,3 @@ var CacheableHooks = /* @__PURE__ */ ((CacheableHooks2) => { | ||
var Cacheable = class extends Hookified { | ||
_primary = new Keyv({ store: new CacheableMemory() }); | ||
_primary = new Keyv({ store: new KeyvCacheableMemory() }); | ||
_secondary; | ||
@@ -839,3 +894,4 @@ _nonBlocking = false; | ||
CacheableMemory, | ||
CacheableStats | ||
CacheableStats, | ||
KeyvCacheableMemory | ||
}; |
{ | ||
"name": "cacheable", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "Simple Caching Engine using Keyv", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -12,3 +12,3 @@ [<img align="center" src="https://cacheable.org/logo.svg" alt="Cacheable" />](https://github.com/jaredwray/cacheable) | ||
`cacheable` is a high performance layer 1 / layer 2 caching engine that is focused on distributed caching with enterprise features such as `CacheSync`. It is built on top of the robust storage engine [Keyv](https://keyv.org) and provides a simple API to cache and retrieve data. | ||
`cacheable` is a high performance layer 1 / layer 2 caching engine that is focused on distributed caching with enterprise features such as `CacheSync` (coming soon). It is built on top of the robust storage engine [Keyv](https://keyv.org) and provides a simple API to cache and retrieve data. | ||
@@ -29,3 +29,3 @@ * Simple to use with robust API | ||
`cacheable` is primarily used as an extension to you caching engine with a robust storage backend [Keyv](https://keyv.org), Memonization, Hooks, Events, and Statistics. | ||
`cacheable` is primarily used as an extension to you caching engine with a robust storage backend [Keyv](https://keyv.org), Memonization (Wrap), Hooks, Events, and Statistics. | ||
@@ -120,3 +120,2 @@ ```bash | ||
* [Google Pub/Sub](https://cloud.google.com/pubsub) | ||
* [AWS SQS](https://aws.amazon.com/sqs) | ||
@@ -159,3 +158,3 @@ * [RabbitMQ](https://www.rabbitmq.com) | ||
* `set(key, value, ttl? | [{string, string, ttl?}])`: Sets a value in the cache. | ||
* `set(key, value, ttl?)`: Sets a value in the cache. | ||
* `setMany([{key, value, ttl?}])`: Sets multiple values in the cache. | ||
@@ -194,4 +193,4 @@ * `get(key)`: Gets a value from the cache. | ||
const cache = new CacheableMemory(options); | ||
await cache.set('key', 'value'); | ||
const value = await cache.get('key'); // value | ||
cache.set('key', 'value'); | ||
const value = cache.get('key'); // value | ||
``` | ||
@@ -198,0 +197,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70050
5.21%1978
6.75%225
-0.44%