1.1.7 • Published 4 years ago

node-cache-9 v1.1.7

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

node-cache-9

node version NPM version build status Test coverage David deps npm download

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 = require('node-cache-9');
const config = {
  xxx: {
    class: 'memory',
    ttl: 5 * 60,
  },
};
const options = { ttl: 10 * 60 };
const cache = cache9.init(config);

(async function() {
  // get data from raw or cache
  const data = await cache.xxx.get('cachekey', async () => {
    // get your data and return it here
    return 'something';
  }, options);
  // update expiration time
  cache.xxx.renew('cachekey', options);
  // clear the cache
  cache.xxx.clear('cachekey');
  console.log(data);

  // get multiply datas from raw or cache
  const datas = await cache.xxx.getM('mainKey', [ 'objA', 'objB', 'objC' ], obj => obj.key, async lst => {
    // get your data and return as array here
    return [];
  });
  // update expiration time
  cache.xxx.renewM('mainKey', [ 'objA', 'objB' ], options);
  // clear the cached data
  cache.xxx.clearM('mainKey', [ 'objC' ]);
  cache.xxx.clearM('mainKey');
  console.log(datas.list);
})();

Create cache

The module provides two ways to create a cache

const cache9 = require('node-cache-9');
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 caches
const cacheGroup = cache9.init(config);
cacheGroup.xxx.get(key, func);
cacheGroup.yyy.get(key, func);
// create a cache
const cache = cache9.create(config.xxx);
cache.get(key, func);

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

functionreturndescription
async get(key,func,options)anyGet 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)anyGet data from cache

Muliply caches

functionreturndescription
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 = require('node-cache-9').BaseDriver;
class yourDriver extends BaseDriver {}
const config = {
  xxx:{
    class: yourDriver
  }
}

Config

namedriverdescriptiondefault
class-Cache driver class, can be a built-in cache driver class name, or a custom cache driver classundefined
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 sourcefalse
autoRenew-When the data is retrieved from the cache, the cache expiration time is also updatedfalse
cacheUndefined-When data is fetched in batches, those data sources that have not returned are treated as null write cachesfalse
channelmemoryThe redis publish/subscribe channel,need getRedis or rdsundefined
channelredisThe prefix of the redis cache, the prefix and the key are separated by a colon'cache9'
getRedismemory()=>{ return {pub, sub}},pub and sub are redis clientundefined
getRedisredis()=>{ return rds},rds is a redis clientundefined
rds-Redis configures JSON, this property is invalid if getRedis is configuredundefined
clearTimememoryThe time to automatically delete the cache, in seconds, which cannot be less than 1800 seconds.0
rawredisWhen reading and writing cached data, do not use JSON.stringify and JSON.parsefalse

Options

namedriverdescriptiondefault
ttl-Same as confignull
keep-Same as configfalse
autoRenew-Same as configfalse
cacheUndefined-Same as configfalse
getKey-Function for calculating the subkey corresponding to the objectobj=>obj
disable-Disable caching this timefalse
update-Force update cachefalse
rawredisSame as configfalse

Run tests

Run redis-server in localhost first

npm run test

Author

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

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago