1.0.5 • Published 5 years ago

@openstfoundation/openst-cache v1.0.5

Weekly downloads
-
License
LGPL-3.0
Repository
github
Last release
5 years ago

OpenST Cache

Latest version Travis Downloads per month Gitter

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.

1.0.6-beta.13

5 years ago

1.0.6-beta.12

5 years ago

1.0.6-beta.11

5 years ago

1.0.6-beta.10

5 years ago

1.0.6-beta.9

5 years ago

1.0.6-beta.8

5 years ago

1.0.6-beta.7

6 years ago

1.0.6-beta.6

6 years ago

1.0.6-beta.5

6 years ago

1.0.6-beta.4

6 years ago

1.0.5

6 years ago

1.0.6-beta.3

6 years ago

1.0.6-beta.2

6 years ago

1.0.6-beta.1

6 years ago

1.0.5-beta.9

6 years ago

1.0.5-beta.8

6 years ago

1.0.5-beta.7

6 years ago

1.0.5-beta.6

6 years ago

1.0.5-beta.5

6 years ago

1.0.5-beta.4

6 years ago

1.0.5-beta.2

6 years ago

1.0.5-beta.1

6 years ago

1.0.4

6 years ago

1.0.4-beta.1

6 years ago

1.0.3

6 years ago

1.0.3-beta.4

6 years ago

1.0.3-beta.3

6 years ago

1.0.3-beta.2

6 years ago

1.0.3-beta.1

6 years ago

1.0.2-beta.1

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.1-beta.5

6 years ago

1.0.1-beta.4

6 years ago

1.0.1-beta.3

6 years ago

1.0.1-beta.2

6 years ago

1.0.1-beta.1

6 years ago

1.0.0

6 years ago

0.9.4

6 years ago

0.9.3

6 years ago

0.9.2

6 years ago

0.9.2-beta.9

6 years ago

0.9.2-beta.7

6 years ago

0.9.2-beta.6

6 years ago

0.9.2-beta.5

6 years ago

0.9.2-beta.4

6 years ago

0.9.2-beta.3

6 years ago

0.9.2-beta.2

6 years ago

0.9.2-beta.1

6 years ago