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

3.2.1 • Public • Published

@qelos/cache-manager

cache manager library for backend services, using redis or internal memory.

Simple Installation Example

Install

$ npm install @qelos/cache-manager
import {createCacheManager} from '@qelos/cache-manager';
import {redisUrl} from './config';

const cacher = redisUrl ?
  require('@qelos/cache-manager/dist/redis-cache').createRedisCache(redisUrl) :
  require('@qelos/cache-manager/dist/memory-cache').createMemoryCache()

const cacheManager = createCacheManager(cacher);

Get item from cache

const value = await cacheManager.getItem('your-cache-key');

Set item to cache

The value must in a string

await cacheManager.setItem('your-cache-key', 'new value to store in cache');

Wrap a fallback to get an item

In this method, you can specify a key. If the key doesn't exist inside the cache store, it will fall back to the function, return its value and store it inside the cache store.

const value = await cacheManager.wrap('your-cache-key', async () => {
  const data = await someDatabase.query('select * from MyTable where id = X')
  return JSON.stringify(data);
});

Custom cache options

await cacheManager.setItem('your-cache-key', 'new value to store in cache', {
  ttl: 1000 // in seconds
});

Readme

Keywords

none

Package Sidebar

Install

npm i @qelos/cache-manager

Weekly Downloads

0

Version

3.2.1

License

MIT

Unpacked Size

14.1 kB

Total Files

25

Last publish

Collaborators

  • avihainaor
  • davidmeirlevy