diff --git a/packages/cacheable/README.md b/packages/cacheable/README.md index bcacb650..19a4650e 100644 --- a/packages/cacheable/README.md +++ b/packages/cacheable/README.md @@ -2,7 +2,7 @@ # Cacheable -> Simple Caching Engine using Keyv +> High Performance Layer 1 / Layer 2 Caching with Keyv Storage [![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable) [![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml) diff --git a/packages/cacheable/package.json b/packages/cacheable/package.json index e47c709c..c873df6c 100644 --- a/packages/cacheable/package.json +++ b/packages/cacheable/package.json @@ -1,7 +1,7 @@ { "name": "cacheable", "version": "1.8.5", - "description": "Simple Caching Engine using Keyv", + "description": "High Performance Layer 1 / Layer 2 Caching with Keyv Storage", "type": "module", "main": "./dist/index.cjs", "module": "./dist/index.js", diff --git a/packages/cacheable/src/index.ts b/packages/cacheable/src/index.ts index 0ca1ce20..8a07d0ab 100644 --- a/packages/cacheable/src/index.ts +++ b/packages/cacheable/src/index.ts @@ -1,7 +1,7 @@ import {Keyv, type KeyvStoreAdapter} from 'keyv'; import {Hookified} from 'hookified'; import {shorthandToMilliseconds} from './shorthand-time.js'; -import {KeyvCacheableMemory} from './keyv-memory.js'; +import {KeyvCacheableMemory, createKeyv} from './keyv-memory.js'; import {CacheableStats} from './stats.js'; import {type CacheableItem} from './cacheable-item-types.js'; import {hash} from './hash.js'; @@ -32,7 +32,7 @@ export type CacheableOptions = { }; export class Cacheable extends Hookified { - private _primary: Keyv = new Keyv({store: new KeyvCacheableMemory()}); + private _primary: Keyv = createKeyv(); private _secondary: Keyv | undefined; private _nonBlocking = false; private _ttl?: number | string; @@ -641,7 +641,7 @@ export class Cacheable extends Hookified { export {CacheableStats} from './stats.js'; export {CacheableMemory, type CacheableMemoryOptions} from './memory.js'; -export {KeyvCacheableMemory} from './keyv-memory.js'; +export {KeyvCacheableMemory, createKeyv} from './keyv-memory.js'; export {shorthandToMilliseconds, shorthandToTime} from './shorthand-time.js'; export type {CacheableItem} from './cacheable-item-types.js'; export { diff --git a/packages/cacheable/src/keyv-memory.ts b/packages/cacheable/src/keyv-memory.ts index 229dd093..a1d6ed90 100644 --- a/packages/cacheable/src/keyv-memory.ts +++ b/packages/cacheable/src/keyv-memory.ts @@ -1,4 +1,6 @@ -import {type KeyvStoreAdapter, type StoredData} from 'keyv'; +import { + Keyv, type KeyvOptions, type KeyvStoreAdapter, type StoredData, +} from 'keyv'; import {CacheableMemory, type CacheableMemoryOptions} from './memory.js'; export type KeyvCacheableMemoryOptions = CacheableMemoryOptions & { @@ -99,3 +101,24 @@ export class KeyvCacheableMemory implements KeyvStoreAdapter { return this._nCache.get(namespace)!; } } + +/** + * 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 + */ +export function createKeyv(options?: KeyvCacheableMemoryOptions): Keyv { + const store = new KeyvCacheableMemory(options); + const namespace = options?.namespace; + + let ttl; + if (options?.ttl && Number.isInteger(options.ttl)) { + ttl = options?.ttl as number; + } + + const keyv = new Keyv({store, namespace, ttl}); + // Remove seriazlize/deserialize + keyv.serialize = undefined; + keyv.deserialize = undefined; + return keyv; +} diff --git a/packages/cacheable/test/keyv-memory.test.ts b/packages/cacheable/test/keyv-memory.test.ts index 3c37ea31..c9316c88 100644 --- a/packages/cacheable/test/keyv-memory.test.ts +++ b/packages/cacheable/test/keyv-memory.test.ts @@ -1,6 +1,6 @@ import {describe, test, expect} from 'vitest'; import {Keyv} from 'keyv'; -import {KeyvCacheableMemory} from '../src/keyv-memory.js'; +import {KeyvCacheableMemory, createKeyv} from '../src/keyv-memory.js'; describe('Keyv Cacheable Memory', () => { test('should initialize keyv cacheable memory', async () => { @@ -93,4 +93,11 @@ describe('Keyv Cacheable Memory', () => { expect(await cache.get('key1')).toBe('default'); expect(cache.store.get('key1')).toBe('default'); }); + + test('should be able to createKeyv with cacheable memory store', async () => { + const keyv = createKeyv({ttl: 1000, lruSize: 1000}); + expect(keyv).toBeDefined(); + expect(keyv.store).toBeInstanceOf(KeyvCacheableMemory); + expect(keyv.store.opts.ttl).toBe(1000); + }); });