Cache

It is very helpful to use caches proper correctly in projects. So, ThinkJS provide a variety of caching methods,includes: Memory cache, file cache, Memcache and redis.

Cache Types

ThinkJS supports the following cache types:

  • memory Cache stored in Memory
  • file Cache stored in file system
  • memcache Cache stored in memcache
  • redis Cache stored in redis

If you use Memcache or redis, you need set configuration information. See also configuration of memcache configuration of redis

Configurate Cache

The default cache configuration likes following. You can edit src/common/config/cache.js to change the configration.

export default {
  type: "file", // the cache type
  timeout: 6 * 3600, // when the cache will expired , default is 6 hours.
  adapter: { // configurations of different type adaptor
    file: {
      path: think.RUNTIME_PATH + '/cache', // the path cache files put in 
      path_depth: 2, // max depth generated by cache files
      file_ext: '.json' // cache files extend name
    },
    redis: {
      prefix: 'thinkjs_'
    },
    memcache: {
      prefix: 'thinkjs_'
    }
  }
};

Note:ThinkJS supports adaptor configuration from the version 2.0.6.

In memcache or redis cache type, the prefix field is used. In this case, ThinkJS uses key + prefix as the storage key to prevent the conflict with other projects. If you don't want to set prefix, you can set it to empty string, like this:

export default {
  prefix: "" // it set the prefix of cache key to empty.
}

Use Cache

You can add, delete, update and search the cache by using method think.cache, see also API -> think for more details.

You can usemethod this.cache to operate cache, if your class is inherited from think.http.base, see also API -> think.http.base for more details.

Extend Cache

You can create a cache class named foo by using following command:

thinkjs adapter cache/foo

After the completion of the excuting, ThinkJS will create the file src/common/adapter/cache/foo.js. Then you need to implement the following methods to extend cache class:

export default class extends think.cache.base {
  /**
   * init 
   * @param  {Object} options []
   * @return {}         []
   */
  init(options){
    //set gc type & start gc
    this.gcType = 'cache_foo';
    think.gc(this);
  }
  /**
   * get cache
   * @param  {String} name []
   * @return {Promise}      []
   */
  get(name){

  }
  /**
   * set cache
   * @param {String} name    []
   * @param {Mixed} value   []
   * @param {Number} timeout []
   * @return {Promise}
   */
  set(name, value, timeout){

  }
  /**
   * delete cache
   * @param  {String} name []
   * @return {Promise}      []
   */
  delete(name){

  }
  /**
   * gc function
   * @return {Promise} []
   */
  gc(){

  }
}

To know the implemation of cache in ThinkJS, please see also (https://github.com/75team/thinkjs/tree/master/src/adapter/cache)

Use Third Party Cache Adapter

To know how to use third party cache Adapter, please see also Adapter -> intro