hybrid-cache
v1.0.30
Published
hybrid-cache is a cache uses memory-cache and Redis
Downloads
15
Readme
hybrid-cache - A cache uses memory-cache and Redis
The hybrid-cache caches in-memory and synchronizes on Redis servers. It only supports some basic commands and focuses on fastest response.
Installation
npm install hybrid-cache
Usage Example
'use strict';
const HybridCache = require('hybrid-cache');
const hybridCache = HybridCache();
hybridCache.init({
waitingTime: 3, // Default 2 seconds
isSyncRedis: true // Default false
});
hybridCache.addRedisServer({
connString: null,
host: '127.0.0.1',
port: '3000',
password: null,
retryTime: 2, // Default retry 5 times
retryInterval: 5 // Waiting 2 seconds between each retry
}, function (err, result) {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.addRedisServer('redis://<user>:<pwd>@<host>:<port>', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
console.log();
hybridCache.set('key1', 'value1', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.setex('key2', 1, 'value2', (err, result) =>{
console.log(`Error: ${err} Result: ${result}`);
});
console.log();
hybridCache.get('key1', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('key2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
setTimeout(() => {
console.log();
hybridCache.get('key1', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('key2', (err, result) =>{
console.log(`Error: ${err} Result: ${result}`);
});
}, 2000);
setTimeout(() => {
console.log();
hybridCache.flushall();
hybridCache.get('key1', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('key2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 3000);
setTimeout(() => {
console.log();
hybridCache.incrby('AAA', 10, (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 4000);
setTimeout(() => {
console.log();
hybridCache.Redis.set('keyInRedis', 'valueInRedis', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Redis.get('keyInRedis', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Mem.get('keyInRedis', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('keyInRedis', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 5000);
setTimeout(() => {
console.log();
hybridCache.Mem.set('keyInMem', 'valueInMem', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Redis.get('keyInMem', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('keyInMem', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Mem.get('keyInMem', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 6000);
setTimeout(() => {
console.log();
hybridCache.Mem.setex('keyInMem2', 1, 'valueInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Redis.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Mem.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 7000);
setTimeout(() => {
console.log();
hybridCache.Redis.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
hybridCache.Mem.get('keyInMem2', (err, result) => {
console.log(`Error: ${err} Result: ${result}`);
});
}, 9000);
This will display:
Error: null Result: OK
Error: null Result: OK
Error: null Result: OK
Error: null Result: OK
Error: null Result: value1
Error: null Result: value2
Error: null Result: value1
Error: null Result: null
Error: null Result: null
Error: null Result: null
Error: null Result: 10
Error: null Result: null
Error: null Result: OK
Error: null Result: valueInRedis
Error: null Result: valueInRedis
Error: null Result: OK
Error: null Result: valueInMem
Error: null Result: valueInMem
Error: null Result: null
Error: null Result: OK
Error: null Result: valueInMem2
Error: null Result: valueInMem2
Error: null Result: null
Error: null Result: null
Error: null Result: null
Error: null Result: null
Note that the API is entirely asynchronous. To get data back from the server, you'll need to use a callback.
API
set = function(key, value, callback)
Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
Return value
Simple string reply: always OK.
setex = function(key, seconds, value, callback)
Set key to hold the string value and set key to timeout after a given number of seconds. An error is returned when seconds is invalid.
Return value
Simple string reply
get = function(key, callback)
Get the value of key. If the key does not exist the special value null is returned.
Return value
Bulk string reply: the value of key, or null when key does not exist or timeout (setWaitingTime function).
incrby = function(key, value, callback)
Increments the number stored at key by increment. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.
Return value
Integer reply: the value of key after the increment
Redis.*
To only use Redis, Redis.* support full list of commands implemented by Redis. For more details, please refer https://redis.io/commands.
Mem.set = function(key, value, callback)
Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.
Return value
Simple string reply: always OK.
Mem.setex = function(key, seconds, value, callback)
Set key to hold the string value and set key to timeout after a given number of seconds. An error is returned when seconds is invalid.
Return value
Simple string reply
Mem.get = function(key, callback)
Get the value of key. If the key does not exist the special value null is returned.
Return value
Bulk string reply: the value of key, or null when key does not exist.