|
1 | | -# Cache |
| 1 | + |
2 | 2 |
|
3 | 3 | [][npm] |
4 | 4 |
|
5 | | -[npm]: https://www.npmjs.com/package/@plgworks/cache |
| 5 | +[npm]: https://www.npmjs.com/package/@plgworks/unicache |
6 | 6 |
|
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. |
| 7 | +UniCache is an NPM package that provides singleton interface and behavior for [Memcached](https://memcached.org/), [Redis](https://redis.io/docs/) and |
| 8 | +In-memory caching. Easily interact or switch between them in minutes! |
9 | 9 |
|
10 | | -## Why Cache? |
11 | | - Core packages of different caching systems do not have a common interface, i.e. they have the same functionality implemented with different method signatures. |
12 | | - Thus changing from one cache system to another becomes difficult as all the usages need to be revisited. |
13 | | - This NPM package solves the problem by providing common wrapper methods for memcached, redis and in-memory caching systems. |
| 10 | +## Why UniCache? |
| 11 | +- UniCache abstracts the unnecessary deviations between the base packages, in turn helping you learn about and interact with 3 different caching engines (Memcached, Redis and In-memory) all at once. |
| 12 | +- Singleton interface of UniCache is not only compatible with Memcached and Redis but also for In-Memory cache. |
| 13 | +- If your backend is interacting with multiple caching engines, UniCache helps developers to reduce translation layer for input and output - thus reducing development time and effort. |
| 14 | +- When using multiple caching engines simultaneously or want to switch between them, consistent output from UniCache will help in faster development. Even exceptions are given out consistently. |
| 15 | +- Be rest assured that your code will not need any further changes in order to use the upcoming base NPM package versions. UniCache will take care of it. |
| 16 | +- UniCache is thoroughly tested and is fully compatible with AWS ElastiCache for Redis and AWS ElastiCache for Memcached. |
14 | 17 |
|
15 | 18 | ## Prerequisites |
16 | | - Required caching engine for the use case must be installed and up. |
17 | | - Refer [memcached](https://memcached.org/) and [redis](https://redis.io/docs/getting-started/installation/) installation guide. |
| 19 | +- [Node.js](https://nodejs.org/en/) (>= version 6) |
| 20 | +- [NPM](https://www.npmjs.com/package/npm) |
18 | 21 |
|
19 | | -## Installion |
| 22 | +Follow the installation guides to get the caching engines of your choice, up and running: |
| 23 | +- [Memcached installation guide](https://memcached.org/) |
| 24 | +- [Redis installation guide](https://redis.io/docs/getting-started/installation/) |
| 25 | + |
| 26 | +## Install NPM |
20 | 27 | ```shell script |
21 | | -npm install @plgworks/cache --save |
| 28 | +npm install @plgworks/unicache --save |
22 | 29 | ``` |
23 | 30 |
|
24 | 31 | ## Initialize |
| 32 | +While using the package, create a singleton object of UniCache and then use it across the application. Example snippet for the UniCache singleton object is given below. |
25 | 33 |
|
26 | 34 | ```js |
27 | | -const Cache = require('@plgworks/cache'); |
| 35 | +// Include the following snippet in a separate file, which can be required all accross the code to get unicache instance. |
| 36 | +// If using different caching engines simultaneously in a single codebase, have different files for each. |
| 37 | +const UniCache = require('@plgworks/unicache'); |
28 | 38 |
|
29 | | -const configStrategy = {}; // Refer the next section for detailed documentation on configStrategy |
30 | | -const cache = Cache.getInstance(configStrategy); |
| 39 | +const configStrategy = { |
| 40 | + engine: "none/redis/memcached", |
| 41 | + // Other keys depend on the engine, refer to the next section on Config Strategy, for the same. |
| 42 | +}; |
31 | 43 |
|
32 | | -const cacheImplementer = cache.cacheInstance; |
| 44 | +module.exports = UniCache.getInstance(configStrategy); |
| 45 | +``` |
| 46 | + |
| 47 | +The singleton object can be used as given below. |
| 48 | +```js |
| 49 | +const cacheProvider = require('path-to-your-uni-cache-singleton-provider'); |
| 50 | +const cacheImplementer = cacheProvider.cacheInstance; |
| 51 | + |
| 52 | +cacheImplementer.set('testKey', 'testValue', 5000); |
| 53 | +cacheImplementer.get('testKey'); |
33 | 54 | ``` |
34 | 55 |
|
35 | 56 | **Note**: To print detailed logs, add `CACHE_DEBUG_ENABLED = '1'` in your env variables. |
36 | 57 |
|
37 | 58 | ### Config Strategy |
38 | | -**`configStrategy`** is a mandatory parameter which specifies the configuration strategy to be used for a particular cache engine. |
| 59 | +**`configStrategy`** is a mandatory parameter which specifies the configuration strategy to be used for the caching engine. |
| 60 | +Using the `engine` property, you can select which caching engine to use. |
39 | 61 |
|
40 | 62 | An example of the configStrategy is: |
41 | 63 | ```js |
42 | 64 | const configStrategy = { |
43 | | - cache: { |
44 | 65 | engine: "none/redis/memcached", |
45 | | - host: "", |
46 | | - port: "", |
47 | | - password: "", |
48 | | - enableTsl: "", |
49 | | - defaultTtl: 10000, |
50 | | - consistentBehavior: "", |
51 | | - servers:[], |
52 | | - namespace: "" |
53 | | - } |
| 66 | + // other keys depend on the engine. |
54 | 67 | }; |
55 | 68 | ``` |
56 | | -- **engine**: redis, memcached are different types of caching engine. For in-memory cache engine parameter will be `none`. |
| 69 | + |
| 70 | +#### Redis Config Strategy |
| 71 | +Following is the redis engine config strategy to be used in initializing UniCache. |
| 72 | +```js |
| 73 | +const configStrategy = { |
| 74 | + engine: "redis", |
| 75 | + host: "localhost", |
| 76 | + port: "6380", |
| 77 | + password: "dsdsdsd", |
| 78 | + enableTsl: "0", |
| 79 | + defaultTtl: 36000, |
| 80 | + consistentBehavior: "1" |
| 81 | +} |
| 82 | +```` |
| 83 | +- **engine**: redis caching engine to be used. |
57 | 84 | - **host**: Host of the redis caching engine. |
58 | 85 | - **port**: Port on which redis caching engine is running. |
59 | 86 | - **password**: Redis caching engine password. |
60 | 87 | - **enableTsl**: This field is used to enable tsl. |
61 | 88 | - **defaultTtl**: Default cache expiry time in sec. |
62 | 89 | - **consistentBehavior**: This field is required to create cache instance key. |
63 | | -- **servers**: servers is an array of memcached servers. |
64 | | -- **namespace**: It is in-memory cache namespace. |
65 | 90 |
|
66 | | - |
67 | | -#### Redis Example |
68 | | -Following is an example of redis engine config strategy to be used in initializing Cache. |
69 | | -```js |
70 | | -const configStrategy = { |
71 | | - cache: { |
72 | | - engine: "redis", |
73 | | - host: "localhost", |
74 | | - port: "6830", |
75 | | - password: "dsdsdsd", |
76 | | - enableTsl: "0", |
77 | | - defaultTtl: 36000, |
78 | | - consistentBehavior: "1" |
79 | | - } |
80 | | -} |
81 | | -```` |
82 | | -#### Memcache Example |
83 | | -Following is an example of memcache engine config strategy to be used in initializing Cache. |
| 91 | +#### Memcache Config Strategy |
| 92 | +Following is the memcache engine config strategy to be used in initializing UniCache. |
84 | 93 | ```js |
85 | 94 | const configStrategy = { |
86 | | - cache: { |
87 | | - engine: "memcached", |
88 | | - servers: ["127.0.0.1:11211"], |
89 | | - defaultTtl: 36000, |
90 | | - consistentBehavior: "1" |
91 | | - } |
| 95 | + engine: "memcached", |
| 96 | + servers: ["127.0.0.1:11211"], |
| 97 | + defaultTtl: 36000, |
| 98 | + consistentBehavior: "1" |
92 | 99 | } |
93 | 100 | ```` |
94 | | -#### In-memory Example |
95 | | -Following is an example of in-memory engine config strategy to be used in initializing Cache. |
| 101 | +- **engine**: memcached caching engine to be used. |
| 102 | +- **servers**: servers is an array of memcached servers. |
| 103 | +- **defaultTtl**: Default cache expiry time in sec. |
| 104 | +- **consistentBehavior**: This field is required to create cache instance key. |
| 105 | +
|
| 106 | +#### In-Memory Config Strategy |
| 107 | +Following is the in-memory engine config strategy to be used in initializing UniCache. |
96 | 108 | ```js |
97 | 109 | const configStrategy = { |
98 | | - cache: { |
99 | | - engine: "none", |
100 | | - namespace: "A", |
101 | | - defaultTtl: "36000", |
102 | | - consistentBehavior: "1" |
103 | | - } |
| 110 | + engine: "none", |
| 111 | + namespace: "A", |
| 112 | + defaultTtl: "36000", |
| 113 | + consistentBehavior: "1" |
104 | 114 | } |
105 | 115 | ``` |
| 116 | +- **engine**: For in-memory cache engine parameter will be `none`. |
| 117 | +- **namespace**: It is in-memory cache namespace. |
| 118 | +- **defaultTtl**: Default cache expiry time in sec. |
| 119 | +- **consistentBehavior**: This field is required to create cache instance key. |
106 | 120 |
|
107 | 121 | ## `cacheImplementer` methods |
108 | 122 | Irrespective of the caching engine, the methods exposed in `cacheImplementer` have the consistent signature. |
@@ -161,32 +175,31 @@ cacheImplementer.set('testKey', "testData").then(console.log); |
161 | 175 | cacheImplementer.touch('testKey', 10).then(resolvePromise); |
162 | 176 | ``` |
163 | 177 |
|
164 | | -## Running test cases |
165 | | -### Set environment variables of particular cache engine for which you want to run the tests. |
166 | | - |
167 | | -* Redis |
168 | | -```shell script |
169 | | -source test/env/redis.sh |
170 | | -``` |
171 | | -* Memcached |
172 | | -```shell script |
173 | | -source test/env/memcached.sh |
174 | | -``` |
175 | | -* In-memory |
176 | | -```shell script |
177 | | -source test/env/inMemory.sh |
178 | | -``` |
179 | | -### Cache engines must be running on the specified ports. |
| 178 | +## Test Cases |
| 179 | +Test cases cover all the 3 caching engines. Also a coverage report is generated at the end. |
180 | 180 |
|
181 | | -* Redis (6380,6381) |
| 181 | +### Step 1: Start Cache engines |
| 182 | +Following processes are needed for running test cases. |
| 183 | +* Start Redis on 2 ports - 6380 and 6381 as needed by the test cases. |
182 | 184 | ```shell script |
183 | 185 | redis-server --port 6380 |
| 186 | +redis-server --port 6381 |
184 | 187 | ``` |
185 | | -* Memcached (11212,11213,11214,11215) |
| 188 | +* Start Memcached on 4 ports - 11212,11213,11214 and 11215 as needed by the test cases. |
186 | 189 | ```shell script |
187 | 190 | memcached -p 11212 -d |
| 191 | +memcached -p 11213 -d |
| 192 | +memcached -p 11214 -d |
| 193 | +memcached -p 11215 -d |
188 | 194 | ``` |
189 | | -### Run tests |
| 195 | + |
| 196 | +### Step 2: Run tests |
| 197 | +For running tests for all the 3 caching engines, use the following command. |
190 | 198 | ```shell script |
191 | | -./node_modules/.bin/mocha --recursive "./test/*.js" |
| 199 | +npm run test |
192 | 200 | ``` |
| 201 | + |
| 202 | +## Contribution |
| 203 | +We welcome more helping hands to make UniCache better. Feel free to report issues, raise PRs for fixes & enhancements. |
| 204 | + |
| 205 | +<p align="left">Built with :heart: by <a href="https://plgworks.com/" target="_blank">PLG Works</a></p> |
0 commit comments