1- #Cache
1+ # Cache
22
33[ ![ Latest version] ( https://img.shields.io/npm/v/@plgworks/cache.svg?maxAge=3600 )] [ npm ]
4- [ ![ Downloads per month] ( https://img.shields.io/npm/dm/@plgworks/cache.svg?maxAge=3600 )] [ npm ]
54
65[ npm ] : https://www.npmjs.com/package/@plgworks/cache
76
8- Cache is the central cache implementation module for several modules.
9- It contains three caching engines. The decision of which caching engine to use is governed while creating the cache object.
10- The caching engines implemented are:
11-
12- * [ Memcached] ( https://memcached.org/ )
13- * [ Redis] ( https://redis.io/docs/ )
14- * In-memory (use with single threaded process in development mode only)
7+ Cache NPM implements wrapper over multiple caching engines - [ Memcached] ( https://memcached.org/ ) , [ Redis] ( https://redis.io/docs/ ) and In-memory (use with single threaded process in development mode only).
8+ The decision of which caching engine to use is governed while creating the Cache NPM object.
159
1610## Why Cache?
1711 Core packages of different caching systems do not have a common interface, i.e. they have the same functionality implemented with different method signatures.
@@ -29,11 +23,23 @@ npm install @plgworks/cache --save
2923
3024## Initialize
3125
32- ### Cache Initialization Params
26+ ``` js
27+ const Cache = require (' @plgworks/cache' );
28+
29+ const configStrategy = {}; // Refer the next section for detailed documentation on configStrategy
30+ const cache = Cache .getInstance (configStrategy);
31+
32+ const cacheImplementer = cache .cacheInstance ;
33+ ```
34+
35+ ** Note** : To print detailed logs, add ` CACHE_DEBUG_ENABLED = '1' ` in your env variables.
36+
37+ ### Config Strategy
3338** ` configStrategy ` ** is a mandatory parameter which specifies the configuration strategy to be used for a particular cache engine.
34- An example of the configStrategy is:
39+
40+ An example of the configStrategy is:
3541``` js
36- configStrategy = {
42+ const configStrategy = {
3743 cache: {
3844 engine: " none/redis/memcached" ,
3945 host: " " ,
@@ -58,11 +64,10 @@ configStrategy = {
5864- ** namespace** : It is in-memory cache namespace.
5965
6066
61- <b >Below are the examples of configStrategies:</b >
62- * Redis
63-
67+ #### Redis Example
68+ Following is an example of redis engine config strategy to be used in initializing Cache.
6469``` js
65- configStrategy = {
70+ const configStrategy = {
6671 cache: {
6772 engine: " redis" ,
6873 host: " localhost" ,
@@ -74,10 +79,10 @@ configStrategy = {
7479 }
7580}
7681````
77- * Memcached
78-
82+ #### Memcache Example
83+ Following is an example of memcache engine config strategy to be used in initializing Cache.
7984` ` ` js
80- configStrategy = {
85+ const configStrategy = {
8186 cache: {
8287 engine: "memcached",
8388 servers: ["127.0.0.1:11211"],
@@ -86,9 +91,10 @@ configStrategy = {
8691 }
8792}
8893` ` ` `
89- * In-memory
94+ #### In-memory Example
95+ Following is an example of in-memory engine config strategy to be used in initializing Cache.
9096` ` ` js
91- configStrategy = {
97+ const configStrategy = {
9298 cache: {
9399 engine: " none" ,
94100 namespace: " A" ,
@@ -98,20 +104,10 @@ configStrategy = {
98104}
99105```
100106
101- ### Create Cache Object:
102-
103- ``` js
104- Cache = require (' @plgworks/cache' );
105- cache = Cache .getInstance (configStrategy);
106-
107- cacheImplementer = cache .cacheInstance ;
108- ```
109- Note: To print detailed logs, add ` CACHE_DEBUG_ENABLED = '1' ` in your env variables.
110-
111- ## Examples:
107+ ## ` cacheImplementer ` methods
108+ Irrespective of the caching engine, the methods exposed in ` cacheImplementer ` have the consistent signature.
112109
113110### Store and retrieve data in cache using ` set ` and ` get ` :
114-
115111``` js
116112const resolvePromise = function (cacheResponse ){
117113 if (cacheResponse .isSuccess ()) {
@@ -162,41 +158,35 @@ cacheImplementer.decrement('testCounterKey', 5).then(resolvePromise);
162158
163159``` js
164160cacheImplementer .set (' testKey' , " testData" ).then (console .log );
165- cacheImplementer .touch (' testKey' , 10 ).then (function (cacheResponse ){
166- if (cacheResponse .isSuccess ()) {
167- console .log (cacheResponse .data .response );
168- } else {
169- console .log (cacheResponse);
170- }
171- });
161+ cacheImplementer .touch (' testKey' , 10 ).then (resolvePromise);
172162```
173163
174164## Running test cases
175165### Set environment variables of particular cache engine for which you want to run the tests.
176166
177167* Redis
178- ````
168+ ``` shell script
179169source test/env/redis.sh
180- ````
170+ ```
181171* Memcached
182- ````
172+ ``` shell script
183173source test/env/memcached.sh
184- ````
174+ ```
185175* In-memory
186- ````
176+ ``` shell script
187177source test/env/inMemory.sh
188- ````
178+ ```
189179### Cache engines must be running on the specified ports.
190180
191181* Redis (6380,6381)
192- ````
182+ ``` shell script
193183redis-server --port 6380
194- ````
184+ ```
195185* Memcached (11212,11213,11214,11215)
196- ````
186+ ``` shell script
197187memcached -p 11212 -d
198- ````
188+ ```
199189### Run tests
200- ````
190+ ``` shell script
201191./node_modules/.bin/mocha --recursive " ./test/*.js"
202- ````
192+ ```
0 commit comments