From 17ca417d0db09fa623e3e9c7864300451a776a7a Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 19 Dec 2023 03:45:20 +0800 Subject: [PATCH 1/3] fix: publish include wrong sourcemap (#622) --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index ba3cc566..4c1f02bf 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ - "dist" + "dist/**/*.js", + "dist/**/*.d.ts" ], "scripts": { "build": "rm -rf dist && tsc -p tsconfig.build.json", From 6e0288d1e8f1b7c6771d306b62acc95d7b151fb6 Mon Sep 17 00:00:00 2001 From: Jared Wray Date: Mon, 18 Dec 2023 11:45:40 -0800 Subject: [PATCH 2/3] updating clean to scripts (#623) --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 4c1f02bf..ae976bd3 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ ], "scripts": { "build": "rm -rf dist && tsc -p tsconfig.build.json", + "clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./yarn.lock", "test": "vitest run --coverage", "release": "yarn check && yarn test -- --run && yarn build", "prepare": "yarn build", @@ -63,6 +64,7 @@ "eslint-plugin-prettier": "5.0.1", "lint-staged": "15.1.0", "prettier": "3.1.0", + "rimraf": "^5.0.5", "typescript": "5.2.2", "vitest": "0.34.6" }, From 0771b7f3ae6014c9f6afa0fb23d497a51391539d Mon Sep 17 00:00:00 2001 From: Denkan Date: Mon, 18 Dec 2023 21:01:46 +0100 Subject: [PATCH 3/3] feat: support sync cache creation (#598) * feat: support sync cache creation Add non-async `createCache()` that `caching()` uses to set up methods. * Update readme with `createCache()` * Update test.yml --------- Co-authored-by: Jared Wray --- .github/workflows/test.yml | 2 +- README.md | 26 ++++++++++++++++++++++++++ src/caching.ts | 35 ++++++++++++++++++++++++++++------- test/caching.test.ts | 14 +++++++++++++- 4 files changed, 68 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6ce035cf..b47e294a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - node-version: ['16', '18', '20'] + node-version: ['18', '20'] steps: - uses: actions/checkout@v3 diff --git a/README.md b/README.md index 36c1621d..d4b569f2 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,32 @@ You can use your own custom store by creating one with the same API as the built - [Example Custom Store redis](https://github.com/node-cache-manager/node-cache-manager-redis-yet) - [Example Custom Store ioredis](https://github.com/node-cache-manager/node-cache-manager-ioredis-yet) +#### Create single cache store synchronously + +As `caching()` requires async functionality to resolve some stores, this is not well-suited to use for default function/constructor parameters etc. + +If you need to create a cache store synchronously, you can instead use `createCache()`: + +```typescript +import { createCache, memoryStore } from 'node-cache-manager'; + +// Create memory cache synchronously +const memoryCache = createCache(memoryStore(), { + max: 100, + ttl: 10 * 1000 /*milliseconds*/, +}); + +// Default parameter in function +function myService(cache = createCache(memoryStore())) {} + +// Default parameter in class constructor +const DEFAULT_CACHE = createCache(memoryStore(), { ttl: 60 * 1000 }); +// ... +class MyService { + constructor(private cache = DEFAULT_CACHE) {} +} +``` + ### Multi-Store ```typescript diff --git a/src/caching.ts b/src/caching.ts index 8a6b30a1..808755b9 100644 --- a/src/caching.ts +++ b/src/caching.ts @@ -1,5 +1,5 @@ import { coalesceAsync } from 'promise-coalesce'; -import { MemoryCache, MemoryConfig, memoryStore } from './stores'; +import { MemoryCache, MemoryConfig, MemoryStore, memoryStore } from './stores'; export type Config = { ttl?: Milliseconds; @@ -63,13 +63,34 @@ export async function caching( export async function caching( factory: Stores, args?: CachingConfig, -): Promise | MemoryCache> { - let store: Store; - if (factory === 'memory') store = memoryStore(args as MemoryConfig); - else if (typeof factory === 'function') - store = await factory(args as FactoryConfig); - else store = factory; +): Promise | Cache | MemoryCache> { + if (factory === 'memory') { + const store = memoryStore(args as MemoryConfig); + return createCache(store, args as MemoryConfig); + } + if (typeof factory === 'function') { + const store = await factory(args as FactoryConfig); + return createCache(store, args); + } + const store = factory; + return createCache(store, args); +} + +export function createCache( + store: MemoryStore, + args?: MemoryConfig, +): MemoryCache; + +export function createCache(store: Store, args?: Config): Cache; + +/** + * Create cache instance by store (non-async). + */ +export function createCache( + store: S, + args?: C, +): Cache { return { /** * Wraps a function in cache. I.e., the first time the function is run, diff --git a/test/caching.test.ts b/test/caching.test.ts index 10314ecc..e2a904ac 100644 --- a/test/caching.test.ts +++ b/test/caching.test.ts @@ -2,7 +2,7 @@ import { faker } from '@faker-js/faker'; import promiseCoalesce from 'promise-coalesce'; import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { Cache, MemoryConfig, caching, memoryStore } from '../src'; +import { caching, Cache, MemoryConfig, memoryStore, createCache } from '../src'; import { sleep } from './utils'; // Allow the module to be mocked so we can assert @@ -389,3 +389,15 @@ describe('caching', () => { }); }); }); + +describe('createCache', () => { + it('should create cache instance by store', async () => { + const store = memoryStore(); + const cache1 = await caching(store); + const cache2 = createCache(store); + expect(cache1.store).toBe(cache2.store); + Object.entries(cache1).forEach(([key, value]) => { + expect(cache2[key as keyof Cache].toString()).toBe(value.toString()); + }); + }); +});