README
Cache
Moxie Cache is the central cache implementation for several moxie modules.
It contains three caching engines. The decision of which caching engine to use is governed while creating the cache object. The caching engines implemented are:
- Memcached
- Redis
- In-process (use with single threaded process in development mode only)
Constructor parameters:
There is 1 parameter required while creating the cache implementer.
- First parameter is mandatory and it specifies the configuration strategy to be used. An example of the configStrategy is:
configStrategy = {
cache: {
engine: "none/redis/memcache"
}
};
Below are the examples:
// import the cache module
const MoxieCache = require('@moxiedotxyz/cache');
// configStrategy for redis engine
configStrategy = {
cache: {
engine: "redis",
host: "localhost",
port: "6830",
password: "dsdsdsd",
enableTsl: "0",
defaultTtl: 36000,
consistentBehavior: "1"
}
}
// configStrategy for memcached engine
configStrategy = {
cache: {
engine: "memcached",
servers: ["127.0.0.1:11211"],
defaultTtl: 36000,
consistentBehavior: "1"
}
}
// configStrategy for in-memory engine
configStrategy = {
cache: {
engine: "none",
namespace: "A",
defaultTtl: "36000",
consistentBehavior: "1"
}
}
Install
npm install @moxiedotxyz/cache --save
Examples:
Create Moxie Cache Object:
MoxieCache = require('@moxiedotxyz/cache');
MoxieCache = MoxieCache.getInstance(configStrategy);
cacheImplementer = MoxieCache.cacheInstance;
set
and get
:
Store and retrieve data in cache using cacheImplementer.set('testKey', 'testValue', 5000).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
cacheImplementer.get('testKey').then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
setObject
and getObject
:
Manage objects in cache using cacheImplementer.setObject('testObjKey', {dataK1: 'a', dataK2: 'b'}).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
cacheImplementer.getObject('testObjKey').then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
multiGet
:
Retrieve multiple cache data using multiGet
for objects, even if a value is set in the cache; the other caching engines match this behaviour.
* NOTE: Redis returns null from cacheImplementer.set('testKeyOne', 'One').then(console.log);
cacheImplementer.set('testKeyTwo', 'Two').then(console.log);
cacheImplementer.multiGet(['testKeyOne', 'testKeyTwo']).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
del
:
Delete cache using cacheImplementer.set('testKey', 'testValue').then(console.log);
cacheImplementer.del('testKey').then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
increment
and decrement
:
Manage counters in cache using cacheImplementer.set('testCounterKey', 1).then(console.log);
cacheImplementer.increment('testCounterKey', 10).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
cacheImplementer.decrement('testCounterKey', 5).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});
touch
:
Change the cache expiry time using cacheImplementer.set('testKey', "testData").then(console.log);
cacheImplementer.touch('testKey', 10).then(function(cacheResponse){
if (cacheResponse.isSuccess()) {
console.log(cacheResponse.data.response);
} else {
console.log(cacheResponse);
}
});