Giter Site home page Giter Site logo

egg-cache's Introduction

egg-cache

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Based on cache-manager

All store engine based on cache-manager can be used.

中文文档

Installation

npm i egg-cache -S

or

yarn add egg-cache

Configuration

// config/plugin.js
exports.cache = {
  enable: true,
  package: 'egg-cache',
};
// config/config.default.js
exports.cache = {
  default: 'memory',
  stores: {
    memory: {
      driver: 'memory',
      max: 100,
      ttl: 0,
    },
  },
};

Usage

await app.cache.set('foo', 'bar', 60, { foo: 'bar' });

await app.cache.get('foo'); // 'bar'

await app.cache.has('foo'); // true

await app.cache.del('foo');
await app.cache.get('foo', 'default');  // 'default'

await app.cache.has('foo'); // false

// closure
await app.cache.set('foo', () => {
  return 'bar';
}); // 'bar'

// Promise
await app.cache.set('foo', () => {
  return Promise.resolve('bar');
});  // 'bar'

// Get cached value. If it's not existed, get and save the value from closure
await app.cache.get('foo', () => {
  return 'bar';
}); // 'bar'

// You can declare an `expire` option
await app.cache.get('foo', () => {
  return 'bar';
}, 60, {
  foo: 'bar'
});

//  foo was cached
await app.cache.get('foo'); // 'bar'

// clear cache
await app.cache.reset();

Add store

  1. config: the store in the configuration uses the driver instead.
// config/config.default.js

const redisStore = require('cache-manager-ioredis');

exports.cache = {
  default: 'memory',
  stores: {
    memory: {
      driver: 'memory',
      max: 100,
      ttl: 0,
    },
    redis: { // full config: https://github.com/dabroek/node-cache-manager-ioredis#single-store
      driver: redisStore,
      host: 'localhost',
      port: 6379,
      password: '',
      db: 0,
      ttl: 600,
      valid: _ => _ !== null,
    },
  },
};
  1. usage
const store = app.cache.store('redis');

await store.set('foo', 'bar');
await store.get('foo'); // 'bar'

await store.del('foo');
await store.has('foo'); // false

Api

cache.set(name, value, [expire=null, options=null]);

Set Cache

  • name cache name
  • value cache value
  • expire (Optional) expire(default from config file,the unit is second, 0 means nerver expire)
  • options (Optional) Refer to cache-manager)

cache.get(name, [defaultValue=null, expire=null, options=null]);

cache.del(name);

cache.has(name);

cache.store(name, [options=null]);

cache.reset();

Default configuration

Refer to config/config.default.js

Unit Test

npm test

Issue

Refer to Issues.

License

MIT

egg-cache's People

Contributors

jasine avatar relzhong avatar runrioter avatar seekcx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

egg-cache's Issues

缓存一致性

缓存更新后,如何保证进程之间的最终一致性

打算重写一下

目前的这个功能实现太简单了,完全无法满足日常需求,准备重写底层的包,实现 memory 和 redis 的驱动,留下扩展口,也可以方便扩展其它的驱动。

先计划一下,再列 roadmap 。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.