Home  >  Article  >  Web Front-end  >  Encapsulated cache class implemented as a cache through redis in nodejs

Encapsulated cache class implemented as a cache through redis in nodejs

亚连
亚连Original
2018-06-07 11:59:232369browse

This article mainly introduces the encapsulated cache class implemented by nodejs using redis as the cache medium. It involves the operation skills related to nodejs operating redis for cache settings. Friends in need can refer to it

The examples in this article describe the use of nodejs Redis is an encapsulated cache class implemented as a cache medium. Share it with everyone for your reference, the details are as follows:

Previously, redis was used as the cache medium under node, and redis was encapsulated with a layer of packaging.

First: Install the npm package redis

const redis = require('redis');

Second: Encapsulation

// cache.js
const redis = require('redis');
const config = require('config');
const logger = require('winston');
const redisObj = {
  client: null,
  connect: function () {
    this.client = redis.createClient(config.redis);
    this.client.on('error', function (err) {
      logger.error('redisCache Error ' + err);
    });
    this.client.on('ready', function () {
      logger.info('redisCache connection succeed');
    });
  },
  init: function () {
    this.connect(); // 创建连接
    const instance = this.client;
    // 主要重写了一下三个方法。可以根据需要定义。
    const get = instance.get;
    const set = instance.set;
    const setex = instance.setex;
    instance.set = function (key, value, callback) {
      if (value !== undefined) {
        set.call(instance, key, JSON.stringify(value), callback);
      }
    };
    instance.get = function (key, callback) {
      get.call(instance, key, (err, val) => {
        if (err) {
          logger.warn('redis.get: ', key, err);
        }
        callback(null, JSON.parse(val));
      });
    };
    // 可以不用传递expires参数。在config文件里进行配置。
    instance.setex = function (key, value, callback) {
      if (value !== undefined) {
        setex.call(instance, key, config.cache.maxAge, JSON.stringify(value), callback);
      }
    };
    return instance;
  },
};
// 返回的是一个redis.client的实例
module.exports = redisObj.init();

How to use

const cache = require('./cache');
cache.get(key, (err, val) => {
  if (val) {
    // do something
  } else {
    // do otherthing
  }
});
cache.set(key, val, (err, res) => {
  // do something
});
cache.setex(key, val, (err, res) => {
  // do something
})

The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.

Related articles:

ajax requests vue.js to render the page to load

How to solve the problem of page flashing when Vue.js displays data

How to set the title method of each page using vue-router

The above is the detailed content of Encapsulated cache class implemented as a cache through redis in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn