Skip to content

neoskop/redis-promise-cache

Repository files navigation

@neoskop/redis-promise-cache

Build Coverage

Store promises and synchrone values in redis.

Prevents multiple redundant calls to the same (expensive) resource. See here for more details.

Usage

import { RedisPromiseCache } from '@neoskop/redis-promise-cache'

const cache = new RedisPromiseCache({ resourceTag: 'db' });
const db : any;

/**
 *  with helper method 
 */
async function getResource(id : string) {
    return cache.getResource(id, () => db.get(id));
}

/**
 *  with "low level" api
 */
async function getResource2(id : string) {
    const cacheEntry = cache.get(id);
    if(cacheEntry) {
        return cacheEntry;
    }

    const promise = db.get(id);
    cache.set(id, promise);

    return promise;
}

Testing

Run tests with yarn test. See package.json for additional test scripts.

Versioning

This package follows SemVer and uses @neoskop/flow-bump for versioning.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

Store promises and synchrone values in redis

Resources

License

Stars

Watchers

Forks

Packages

No packages published