Skip to content

qest-cz/cache-async-adapter

Repository files navigation

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'])

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published