SFN-Cache
Simple Friendly Node.js Cache.
This package relies on Redis, so you must have a Redis server started first.
Install
npm install sfn-cache --save
Import
const Cache = ;
Example
async var cache = null; try var cache = data = await cache _data = await cache; console; console; catch e console; ifcache cache;;
API
-
new Cache(dsn?: string)
Creates a cache channel with a redis URL. -
new Cache(options?: { [x: string]: any })
options
All available options forredis.createClient()
.
-
cache.set(key: string, value: any, ttl?: number): Promise<any>
Stores or updates a value.ttl
Time-To-Live (in milliseconds).
-
cache.get(key: string): Promise<any>
Retrieves a value by a given key. -
cache.delete(key: string): Promise<void>
Deletes a key from the cache. -
destroy(): Promise<void>
Clears the cache entirely, the cache will be closed after calling this method. -
close(): void
Closes the cache channel, if you don't close the channel, the program will hang until the redis server closes the connection. -
cache.connected
If the channel is open, it will betrue
. -
cache.closed
Opposite toconnected
.
new Cache()
// Simplest way:var cache = ; // Pass a redis URL:var cache = "redis://127.0.0.1:6379"; // Pass an object:var cache = host: "127.0.0.1" port: 6379 password: "my-p@ssw0rd" database: 1 prefix: "my-cache:"; // Pass a redis client:const redis = ;var client = redis;var cache = client;