Not a Penny More

    cache-manager
    TypeScript icon, indicating that this package has built-in type declarations

    5.1.4 • Public • Published

    node-cache-manager npm version codecov

    Flexible NodeJS cache module

    A cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface.

    Features

    • Made with Typescript and compatible with ESModules
    • Easy way to wrap any function in cache.
    • Tiered caches -- data gets stored in each cache and fetched from the highest. priority cache(s) first.
    • Use any cache you want, as long as it has the same API.
    • 100% test coverage via vitest.

    Installation

    pnpm install cache-manager
    

    Usage Examples

    Single Store

    import { caching } from 'cache-manager';
    
    const memoryCache = await caching('memory', {
      max: 100,
      ttl: 10 * 1000 /*milliseconds*/,
    });
    
    const ttl = 5 * 1000; /*milliseconds*/
    await memoryCache.set('foo', 'bar', ttl);
    
    console.log(await memoryCache.get('foo'));
    // >> "bar"
    
    await memoryCache.del('foo');
    
    console.log(await memoryCache.get('foo'));
    // >> undefined
    
    const getUser = (id: string) => new Promise.resolve({ id: id, name: 'Bob' });
    
    const userId = 123;
    const key = 'user_' + userId;
    
    console.log(await memoryCache.wrap(key, () => getUser(userId), ttl));
    // >> { id: 123, name: 'Bob' }

    See unit tests in test/caching.test.ts for more information.

    Example setting/getting several keys with mset() and mget()

    await memoryCache.store.mset(
      [
        ['foo', 'bar'],
        ['foo2', 'bar2'],
      ],
      ttl,
    );
    
    console.log(await memoryCache.store.mget('foo', 'foo2'));
    // >> ['bar', 'bar2']
    
    // Delete keys with mdel() passing arguments...
    await memoryCache.store.mdel('foo', 'foo2');

    Example Express App Usage

    Custom Stores

    You can use your own custom store by creating one with the same API as the built-in memory stores.

    Multi-Store

    import { multiCaching } from 'cache-manager';
    
    const multiCache = multiCaching([memoryCache, someOtherCache]);
    const userId2 = 456;
    const key2 = 'user_' + userId;
    const ttl = 5;
    
    // Sets in all caches.
    await multiCache.set('foo2', 'bar2', ttl);
    
    // Fetches from highest priority cache that has the key.
    console.log(await multiCache.get('foo2'));
    // >> "bar2"
    
    // Delete from all caches
    await multiCache.del('foo2');

    See unit tests in test/multi-caching.test.ts for more information.

    Store Engines

    Official and updated to last version

    Third party

    Contribute

    If you would like to contribute to the project, please fork it and send us a pull request. Please add tests for any new features or bug fixes.

    License

    node-cache-manager is licensed under the MIT license.

    Install

    npm i cache-manager

    DownloadsWeekly Downloads

    947,209

    Version

    5.1.4

    License

    MIT

    Unpacked Size

    69.8 kB

    Total Files

    19

    Last publish

    Collaborators

    • bryandonovan
    • botikass