Skip to content

Commit 84757be

Browse files
Cache initial commit.
1 parent 941e767 commit 84757be

4 files changed

Lines changed: 46 additions & 83 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
## Cache v1.0.0
2-
- Cache is cache implementation of three caching engines Memcached, Redis and In-process.
3-
2+
- Cache is cache implementation of three caching engines Memcached, Redis and In-memory.

README.md

Lines changed: 43 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
Cache
2-
============
1+
#Cache
2+
33
[![Latest version](https://img.shields.io/npm/v/@plgworks/cache.svg?maxAge=3600)][npm]
44
[![Downloads per month](https://img.shields.io/npm/dm/@plgworks/cache.svg?maxAge=3600)][npm]
55

66
[npm]: https://www.npmjs.com/package/@plgworks/cache
77

8-
Cache is the central cache implementation for several modules.
8+
Cache is the central cache implementation module for several modules.
99
It contains three caching engines. The decision of which caching engine to use is governed while creating the cache object.
1010
The caching engines implemented are:
1111

1212
* [Memcached](https://memcached.org/)
1313
* [Redis](https://redis.io/docs/)
1414
* In-memory (use with single threaded process in development mode only)
15+
1516
## Why Cache?
16-
Implementation method varies according to caching systems.
17-
So implementing different caching systems is always a overhead for developer.
18-
Our package solve this problem by providing common wrapper methods for memcached, redis and in-memory caching system.
17+
Core packages of different caching systems do not have a common interface, i.e. they have the same functionality implemented with different method signatures.
18+
Thus changing from one cache system to another becomes difficult as all the usages need to be revisited.
19+
This NPM package solves the problem by providing common wrapper methods for memcached, redis and in-memory caching systems.
1920

2021
## Prerequisites
2122
Required caching engine for the use case must be installed and up.
22-
Refer [memcached](https://gist.github.com/tomysmile/ba6c0ba4488ea51e6423d492985a7953) and [redis](https://flaviocopes.com/redis-installation/) installation guide.
23+
Refer [memcached](https://memcached.org/) and [redis](https://redis.io/docs/getting-started/installation/) installation guide.
2324

24-
## Install NPM
25-
```bash
25+
## Installion
26+
```shell script
2627
npm install @plgworks/cache --save
2728
```
2829

2930
## Initialize
3031

31-
#### Cache Initialization Params
32+
### Cache Initialization Params
3233
**`configStrategy`** is a mandatory parameter which specifies the configuration strategy to be used for a particular cache engine.
3334
An example of the configStrategy is:
3435
```js
@@ -96,106 +97,68 @@ configStrategy = {
9697
}
9798
}
9899
```
99-
Note: To print detailed logs, add `CACHE_DEBUG_ENABLED = '1'` in your env variables.
100-
## Examples:
101100

102-
#### Create Cache Object:
101+
### Create Cache Object:
103102

104103
```js
105104
Cache = require('@plgworks/cache');
106105
cache = Cache.getInstance(configStrategy);
107106

108107
cacheImplementer = cache.cacheInstance;
109108
```
109+
Note: To print detailed logs, add `CACHE_DEBUG_ENABLED = '1'` in your env variables.
110+
111+
## Examples:
110112

111-
#### Store and retrieve data in cache using `set` and `get`:
113+
### Store and retrieve data in cache using `set` and `get`:
112114

113115
```js
114-
cacheImplementer.set('testKey', 'testValue', 5000).then(function(cacheResponse){
115-
if (cacheResponse.isSuccess()) {
116-
console.log(cacheResponse.data.response);
117-
} else {
118-
console.log(cacheResponse);
119-
}
120-
});
121-
cacheImplementer.get('testKey').then(function(cacheResponse){
122-
if (cacheResponse.isSuccess()) {
123-
console.log(cacheResponse.data.response);
124-
} else {
125-
console.log(cacheResponse);
126-
}
127-
});
116+
const resolvePromise = function(cacheResponse){
117+
if (cacheResponse.isSuccess()) {
118+
console.log(cacheResponse.data.response);
119+
} else {
120+
console.log(cacheResponse);
121+
}
122+
};
123+
124+
cacheImplementer.set('testKey', 'testValue', 5000).then(resolvePromise);
125+
126+
cacheImplementer.get('testKey').then(resolvePromise);
128127
```
129128

130-
#### Manage objects in cache using `setObject` and `getObject`:
129+
### Manage objects in cache using `setObject` and `getObject`:
131130

132131
```js
133-
cacheImplementer.setObject('testObjKey', {dataK1: 'a', dataK2: 'b'}).then(function(cacheResponse){
134-
if (cacheResponse.isSuccess()) {
135-
console.log(cacheResponse.data.response);
136-
} else {
137-
console.log(cacheResponse);
138-
}
139-
});
140-
cacheImplementer.getObject('testObjKey').then(function(cacheResponse){
141-
if (cacheResponse.isSuccess()) {
142-
console.log(cacheResponse.data.response);
143-
} else {
144-
console.log(cacheResponse);
145-
}
146-
});
132+
cacheImplementer.setObject('testObjKey', {dataK1: 'a', dataK2: 'b'}).then(resolvePromise);
133+
cacheImplementer.getObject('testObjKey').then(resolvePromise);
147134
```
148135

149-
#### Retrieve multiple cache data using `multiGet`:
136+
### Retrieve multiple cache data using `multiGet`:
150137

151-
###### * <b>NOTE: Redis returns null from `multiGet` for objects, even if a value is set in the cache; the other caching engines match this behaviour.</b>
138+
<b>NOTE: Redis returns null from `multiGet` for objects, even if a value is set in the cache. The other caching implementers match this behaviour.</b>
152139

153140
```js
154141
cacheImplementer.set('testKeyOne', 'One').then(console.log);
155142
cacheImplementer.set('testKeyTwo', 'Two').then(console.log);
156-
cacheImplementer.multiGet(['testKeyOne', 'testKeyTwo']).then(function(cacheResponse){
157-
if (cacheResponse.isSuccess()) {
158-
console.log(cacheResponse.data.response);
159-
} else {
160-
console.log(cacheResponse);
161-
}
162-
});
143+
cacheImplementer.multiGet(['testKeyOne', 'testKeyTwo']).then(resolvePromise);
163144
```
164145

165-
#### Delete cache using `del`:
146+
### Delete cache using `del`:
166147

167148
```js
168149
cacheImplementer.set('testKey', 'testValue').then(console.log);
169-
cacheImplementer.del('testKey').then(function(cacheResponse){
170-
if (cacheResponse.isSuccess()) {
171-
console.log(cacheResponse.data.response);
172-
} else {
173-
console.log(cacheResponse);
174-
}
175-
});
150+
cacheImplementer.del('testKey').then(resolvePromise);
176151
```
177152

178-
#### Manage counters in cache using `increment` and `decrement`:
153+
### Manage counters in cache using `increment` and `decrement`:
179154

180155
```js
181156
cacheImplementer.set('testCounterKey', 1).then(console.log);
182-
cacheImplementer.increment('testCounterKey', 10).then(function(cacheResponse){
183-
if (cacheResponse.isSuccess()) {
184-
console.log(cacheResponse.data.response);
185-
} else {
186-
console.log(cacheResponse);
187-
}
188-
});
189-
cacheImplementer.decrement('testCounterKey', 5).then(function(cacheResponse){
190-
if (cacheResponse.isSuccess()) {
191-
console.log(cacheResponse.data.response);
192-
} else {
193-
console.log(cacheResponse);
194-
}
195-
});
157+
cacheImplementer.increment('testCounterKey', 10).then(resolvePromise);
158+
cacheImplementer.decrement('testCounterKey', 5).then(resolvePromise);
196159
```
197160

198-
#### Change the cache expiry time using `touch`:
161+
### Change the cache expiry time using `touch`:
199162

200163
```js
201164
cacheImplementer.set('testKey', "testData").then(console.log);
@@ -207,8 +170,9 @@ cacheImplementer.touch('testKey', 10).then(function(cacheResponse){
207170
}
208171
});
209172
```
173+
210174
## Running test cases
211-
##### Set environment variables of particular cache engine for which you want to run the tests.
175+
### Set environment variables of particular cache engine for which you want to run the tests.
212176

213177
* Redis
214178
````
@@ -222,7 +186,7 @@ source test/env/memcached.sh
222186
````
223187
source test/env/inMemory.sh
224188
````
225-
##### Cache engines must be running on the specified ports.
189+
### Cache engines must be running on the specified ports.
226190

227191
* Redis (6380,6381)
228192
````
@@ -232,7 +196,7 @@ redis-server --port 6380
232196
````
233197
memcached -p 11212 -d
234198
````
235-
##### Run tests
199+
### Run tests
236200
````
237201
./node_modules/.bin/mocha --recursive "./test/*.js"
238202
````

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.0-beta.1
1+
1.0.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@plgworks/cache",
3-
"version": "1.0.0-beta.1",
3+
"version": "1.0.0",
44
"description": "Cache is the central cache implementation for several modules.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)