@openstfoundation/openst-cache v1.0.5
OpenST Cache
OpenST Cache is the central cache implementation for all OpenST products and can easily be plugged-in.
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)
Contructor parameters:
There are 2 parameters required while creating the cache implementer.
First parameter is mandatory and it specifies the cache engine to be used. The values can be 'none', 'memcached' or 'redis'
Second parameter is optional and specifies the behaviour consistency of the cache accross all cache engines. Few implementation differ in redis and memcached, for example the behaviour of increment and decrement when initial value is not set for a key. So if the system is designed considering one cache engine and later when the cache engine is changed then the system may break. To avoid such cases this parameter tell the implementer wheather behaviour of the cache engine be should be consistent or not.
Below are the examples:
// import the cache module
const openSTCache = require('@openstfoundation/openst-cache');
//redis engine
const cacheImplementer = new openSTCache.cache('redis', false);
//memcached engine
const cacheImplementer = new openSTCache.cache('memcached', true);
//In-process engine
const cacheImplementer = new openSTCache.cache('none', false);
Install OpenST Cache
npm install @openstfoundation/openst-cache --save
Set ENV Variables
Define the default TTL:
export OST_DEFAULT_TTL=3600 # In seconds
If the cache engine is redis, set the following ENV variables:
export OST_REDIS_HOST='127.0.0.1'
export OST_REDIS_PORT=6379
export OST_REDIS_PASS=st123 # Redis authentication password defined as "requirepass"
export OST_REDIS_TLS_ENABLED=0 # Possible values are 1 and 0
If the cache engine is memcached, set the following ENV variable:
export OST_MEMCACHE_SERVERS='127.0.0.1:11211' # comma seperated memcached instances eg: '127.0.0.1:11211, 192.168.1.101:11211'
Examples:
Create OpenST Cache Object:
const openSTCache = require('@openstfoundation/openst-cache');
const cacheImplementer = new openSTCache.cache('redis', false);
Store and retrieve data in cache using set
and get
:
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);
}
});
Manage objects in cache using setObject
and getObject
:
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);
}
});
Retrieve multiple cache data using multiGet
:
* NOTE: Redis returns null from multiGet
for objects, even if a value is set in the cache; the other caching engines match this behaviour.
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);
}
});
Delete cache using del
:
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);
}
});
Manage counters in cache using increment
and decrement
:
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);
}
});
Change the cache expiry time using touch
:
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);
}
});
For further implementation details, please refer to the API documentation.
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago