Code Monkey home page Code Monkey logo

redis-speed's Introduction

redis-speed

An utility for redis aim to improve the performance of redis, it includes FlashCacheRedis now.

build status Test coverage David deps node version

NPM

FlashCacheRedis

Save value in memory to reduce the frequency of reading redis.

Usage

const {FlashCacheRedis} = require('redis-speed');
const Redis = require('ioredis');
const redisClient = new Redis();//connect to the redis server of localhost:6379
const {cacheQueryAdapter} = new FlashCacheRedis({
    redisClient,//the redis client object
    interval:1000
});
const {expect} = require('chai');
const KEY = 'flash_cache_redis:basic';
const VALUE = {name:'sunny',id:1};

redisClient.set(KEY,JSON.stringify(VALUE),function(err) {//save session
    if (err) {
        return console.error(err);
    }
    //It will cache the value in FlashCacheRedis instance, when you call the function of `cacheQueryAdapter.get` next, it will read the value directly from memory.
    cacheQueryAdapter.get(KEY,function(err,obj) {
        if (err) {
            return console.error(err);
        }
        try {
            obj = JSON.parse(obj);
        } catch (e) {
            return console.error(e);
        }
        expect(obj).to.have.property('name').and.equal(VALUE.name);
    });
});

RedisHelper

An utility class to do parallel job with redis. If your redis is run in cluster mode, it will not return the data properly with some functions, such as pipeline or mutil. RedisHelper is designed to resoved such issue.

Usage

const {RedisHelper} = require('redis-speed');
const key1 = 'key1';
const value1 = Math.random() + '';
const key2 = 'key2';
const value2 = Math.random() + '';
const tasks = [
    ['set',key1,value1],
    ['set',key2,value2]
];
redisHelper.doParallelJobs(tasks,function(none,[[err1],[err2]]) {

});

redisBatchIncr

An utility class to send redis command in batch.

Usage

const {
    redisBatchIncr: {
        EVENT_ONE_LOOP_FINISHED,
        EVENT_SEND_ERROR,
        BatchHincr,
        BatchZincrby
    }
} = require('redis-speed');
const redisClient = new Redis();//connect to the redis server of localhost:6379

const LOOP_COUNT = 100;
const INTERVAL = 200;
const key = ('test:' + Math.random()).replace('.','');
const cmd = new BatchHincr({redisClient,loopInterval:INTERVAL,key});

cmd.on(EVENT_ONE_LOOP_FINISHED,function() {
    redisClient.hget(key,'name',function(err,reply) {
        if (err) {
            return console.error(err);
        }
        expect(Number(reply)).to.be.equal(LOOP_COUNT);
    });
});
cmd.on(EVENT_SEND_ERROR,function(err) {
    console.error(err);
});

for(var i=0;i<LOOP_COUNT;i++) {
    cmd.addData(1,'name');
}

redisMultiBatchIncr

An utility class to send redis command in batch with multiple keys.

Usage

const {
    redisMultiBatchIncr: {
        EVENT_ONE_LOOP_FINISHED,
        EVENT_SEND_ERROR,
        BatchMultiHincr,
        BatchMultiZincrby
    }
} = require('redis-speed');
const redisClient = new Redis();//connect to the redis server of localhost:6379

const LOOP_COUNT = 100;
const INTERVAL = 200;

const cmd = new BatchMultiHincr({redisClient,loopInterval:INTERVAL});
const key = ('test:' + Math.random()).replace('.','');
cmd.on(EVENT_ONE_LOOP_FINISHED,function() {
    redisClient.hget(key,'name',function(err,reply) {
        if (err) {
            return done(err);
        }
        expect(Number(reply)).to.be.equal(LOOP_COUNT);
        done();
    });
});
cmd.on(EVENT_SEND_ERROR,function(err) {
    done(err);
});

for(var i=0;i<LOOP_COUNT;i++) {
    cmd.addData(key,1,'name');
}

API

api

License

MIT

redis-speed's People

Contributors

yunnysunny avatar

Stargazers

ttang avatar

Watchers

 avatar James Cloos avatar  avatar

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.