@qest/cache-async-adapter
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Cache

Async cache Redis or local with the same interface which can be further expanded

Use memory storage

import {CacheAdapterType, CacheFactory} from "@qest/cache-async-adapter";

const cacheLocal = new CacheFactory().create(CacheAdapterType.Local);

await cacheLocal.set('key1', {data: 'hello'}, 10000); // time in ms
console.log(await cacheLocal.get('key1')); // {data: 'hello'}

Use Redis storage

import {CacheAdapterType, CacheFactory} from "@qest/cache-async-adapter";
const redisConfig = {
    port: 6379,
    host: '127.0.0.1',
};

const cacheRedis =  new CacheFactory().create(CacheAdapterType.Redis, redisConfig);

await cacheRedis.set('key1', {data: 'hello Redis'}, 10000); // time in ms
console.log(await cacheRedis.get('key1')); // {data: 'hello Redis'}

All functions

get: (key: string) => Promise;

Example:

await cacheLocal.get('key1')
set: (key: string, values: object, msInterval?: number) => Promise;

Example:

await cacheLocal.set('key1', {data: 'hello'}, 10000); // time in ms
invalidate: (key: string) => Promise;

Example:

await cacheLocal.invalidate('key1')
invalidateAll: () => Promise;

Example:

await cacheLocal.invalidateAll()
scan: (key: string) => Promise<T[]>;

Example:

await cacheLocal.set('key1', {data: '1'}); 
await cacheLocal.set('key2', {data: '2'});
cacheRedis.scan(`key:*`); // return [key1: {data: 1}', key2: {data: 2}]   

scanKeys: (regex: string) => Promise<string[]>;

Example:

await cacheLocal.set('key1', {data: 'hello'}); 
await cacheLocal.set('key2', {data: 'hello'});
cacheRedis.scanKeys(`key:*`); // return ['key1', 'key2']    

invalidateByKeys: (keys: string[]) => Promise;

Example:

await cacheLocal.invalidateByKeys(['key1'])

Readme

Keywords

none

Package Sidebar

Install

npm i @qest/cache-async-adapter

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

42.1 kB

Total Files

36

Last publish

Collaborators

  • qest