An utility for redis aim to improve the performance of redis, it includes FlashCacheRedis
now.
Save value in memory to reduce the frequency of reading redis.
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);
});
});
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.
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]]) {
});
An utility class to send redis command in batch.
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');
}
An utility class to send redis command in batch with multiple keys.
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');
}