Skip to content

Commit

Permalink
cacheable - adding in createKeyv and updating memory instance (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray authored Dec 3, 2024
1 parent 8d3c8fb commit 53d0628
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/cacheable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/cacheable/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/cacheable/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
25 changes: 24 additions & 1 deletion packages/cacheable/src/keyv-memory.ts
Original file line number Diff line number Diff line change
@@ -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 & {
Expand Down Expand Up @@ -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;
}
9 changes: 8 additions & 1 deletion packages/cacheable/test/keyv-memory.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 53d0628

Please sign in to comment.