node-cache-9
This is an easy-to-use data caching module that allows anyone to simply put data into the cache without additional operations.
中文说明
Homepage
Prerequisites
- node >=8
- redis >=2.8.0
Install
npm i node-cache-9
Usage
'use strict';const cache9 = ;const config = xxx: class: 'memory' ttl: 5 * 60 ;const options = ttl: 10 * 60 ;const cache = cache9; { // get data from raw or cache const data = await cachexxx; // update expiration time cachexxx; // clear the cache cachexxxclear'cachekey'; console; // get multiply datas from raw or cache const datas = await cachexxx; // update expiration time cachexxx; // clear the cached data cachexxx; cachexxx; console;};
Create cache
The module provides two ways to create a cache
const cache9 = ;const config = xxx: class: 'memory' ttl: 5 * 60 yyy: class: 'redis' ttl: 10 * 60 rds: host: '127.0.0.1' db: 0 ;// create a set of cachesconst cacheGroup = cache9;cacheGroupxxx;cacheGroupyyy;// create a cacheconst cache = cache9;cache;
Cache driver class
The cache driver class is a class that contains some specific methods. We operate the cache by the methods provided by the driver class.
Single cache
function | return | description |
---|---|---|
async get(key,func,options) | any | Get data from cache or func() method |
renew(key,options) | - | Update expiration time |
async clear(key) | - | Clear cache |
setCache(key, data, options) | - | Set cache data |
async getCache(key, options) | any | Get data from cache |
Muliply caches
function | return | description |
---|---|---|
async getM(key,list,saveKey,func,options) | {list, json} | The data is obtained from the cache in batches, and the data not in the cache is sorted out, and is obtained and cached by the func(lst) function, where saveKey(obj) is used to obtain a key corresponding to a value |
renewM(key,list,options) | - | Update expiration time |
async clearM(key, list, options) | - | Clear caches, parameter list can be omitted |
setCacheM(key, subkey, data, options) | - | Set cache data |
async getCacheM(key, list, options) | {list, json} | Get data from cache |
Default dirvers
- memory: Memory cache driver class, using memory as a cache, can cache any data type
- redis: Redis cache driver class, using redis server to cache data, data to be cached must support JSON.stringify
BaseDriver
BaseDriver is the base class for memory and redis. You can use BaseDriver to quickly create a new cache driver class
const BaseDriver = BaseDriver; {}const config = xxx: class: yourDriver
Config
name | driver | description | default |
---|---|---|---|
class | - | Cache driver class, can be a built-in cache driver class name, or a custom cache driver class | undefined |
ttl | - | Time to live(second) | 0 |
keep | - | Whether the expired cache is used as the return result when data cannot be obtained from the data source | false |
autoRenew | - | When the data is retrieved from the cache, the cache expiration time is also updated | false |
cacheUndefined | - | When data is fetched in batches, those data sources that have not returned are treated as null write caches | false |
channel | memory | The redis publish/subscribe channel,need getRedis or rds | undefined |
channel | redis | The prefix of the redis cache, the prefix and the key are separated by a colon | 'cache9' |
getRedis | memory | ()=>{ return {pub, sub}},pub and sub are redis client | undefined |
getRedis | redis | ()=>{ return rds},rds is a redis client | undefined |
rds | - | Redis configures JSON, this property is invalid if getRedis is configured | undefined |
clearTime | memory | The time to automatically delete the cache, in seconds, which cannot be less than 1800 seconds. | 0 |
raw | redis | When reading and writing cached data, do not use JSON.stringify and JSON.parse | false |
Options
name | driver | description | default |
---|---|---|---|
ttl | - | Same as config | null |
keep | - | Same as config | false |
autoRenew | - | Same as config | false |
cacheUndefined | - | Same as config | false |
getKey | - | Function for calculating the subkey corresponding to the object | obj=>obj |
disable | - | Disable caching this time | false |
update | - | Force update cache | false |
raw | redis | Same as config | false |
Run tests
Run redis-server in localhost first
npm run test
Author
985ch
- Github: @985ch
Show your support
Give a ⭐️ if this project helped you!
License
Copyright © 2019 985ch.
This project is MIT licensed.
This README was translate by google
This README was generated with ❤️ by readme-md-generator