Home > Web Front-end > JS Tutorial > Nodejs uses redis to encapsulate cache

Nodejs uses redis to encapsulate cache

php中世界最好的语言
Release: 2018-04-12 17:45:27
Original
3474 people have browsed it

This time I will bring you the method of nodejs using redis to encapsulate the cache. What are the precautions for nodejs to use redis to encapsulate the cache. The following is a practical case, let's take a look.

Previously, redis was used as the cache medium under node, and redis was encapsulated

First: Installnpm package redis

const redis = require('redis');
Copy after login

Second: Encapsulate

// 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();
Copy after login

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
})
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to implement cross-domain upload of VUE UEditor images

How to use the upload component in the vue project

The above is the detailed content of Nodejs uses redis to encapsulate cache. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template