Skip to content

Commit

Permalink
Merge pull request #15 from PLG-Works/release-1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilkhedar authored Oct 18, 2022
2 parents 987feca + 87917d0 commit 6bfa643
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## UniCache v1.0.0-beta.1
## UniCache v1.0.0
- First release of UniCache having support for 3 caching engines - Memcached, Redis and In-memory Cache
- Test case coverage improved to 95%.
73 changes: 41 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
![UniCache logo Dark](https://user-images.githubusercontent.com/7627517/195535780-47906a3b-c302-4c59-bb1e-d171914ff1bd.png)

[![Latest version](https://img.shields.io/npm/v/@plgworks/cache.svg?maxAge=3600)][npm]
[![Latest version](https://img.shields.io/npm/v/@plgworks/unicache.svg?maxAge=3600)][npm]

[npm]: https://www.npmjs.com/package/@plgworks/unicache

UniCache is an NPM package that provides singleton interface and behavior for [Memcached](https://memcached.org/), [Redis](https://redis.io/docs/) and
In-memory caching. Easily interact or switch between them in minutes!
UniCache is an open-source NPM package that provides unified / singleton interface and behavior for [Memcached](https://memcached.org/),
[Redis](https://redis.io/docs/) and In-memory caching. Easily interact or switch between them in minutes!

## Why UniCache?
- 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.
- UniCache abstracts the unnecessary deviations between the base NPM packages of Memcached and Redis, in turn helping you
learn about and interact with 3 different caching engines (Memcached, Redis and In-memory) all at once.
- Singleton interface of UniCache is not only compatible with Memcached and Redis but also for In-Memory cache.
- 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.
- 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.
- 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.
- 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. Even exceptions are given out consistently.
- When using multiple caching engines simultaneously or want to switch between them, consistent output from UniCache
will help in faster development.
- 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.
- UniCache is thoroughly tested and is fully compatible with AWS ElastiCache for Redis and AWS ElastiCache for Memcached.

## Prerequisites
Expand All @@ -29,7 +33,8 @@ npm install @plgworks/unicache --save
```

## Initialize
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.
While using the package, create a singleton object of UniCache for each caching engines and then use it across the application.
Example snippet for the UniCache singleton object initialization is given below.

```js
// Include the following snippet in a separate file, which can be required all accross the code to get unicache instance.
Expand All @@ -49,14 +54,14 @@ The singleton object can be used as given below.
const cacheProvider = require('path-to-your-uni-cache-singleton-provider');
const cacheImplementer = cacheProvider.cacheInstance;

cacheImplementer.set('testKey', 'testValue', 5000);
cacheImplementer.get('testKey');
cacheImplementer.set('testKey', 'testValue', 5000).then(console.log);
cacheImplementer.get('testKey').then(console.log);
```

**Note**: To print detailed logs, add `CACHE_DEBUG_ENABLED = '1'` in your env variables.
**Note**: To print detailed logs, add `UNICACHE_DEBUG_ENABLED = '1'` in the ENV variables.

### Config Strategy
**`configStrategy`** is a mandatory parameter which specifies the configuration strategy to be used for the caching engine.
**`configStrategy`** is a mandatory parameter which specifies the configuration to be used for the caching engine.
Using the `engine` property, you can select which caching engine to use.

An example of the configStrategy is:
Expand All @@ -68,7 +73,7 @@ const configStrategy = {
```

#### Redis Config Strategy
Following is the redis engine config strategy to be used in initializing UniCache.
Following is how the config strategy looks for redis caching engine.
```js
const configStrategy = {
engine: "redis",
Expand All @@ -80,16 +85,16 @@ const configStrategy = {
consistentBehavior: "1"
}
````
- **engine**: redis caching engine to be used.
- **host**: Host of the redis caching engine.
- **port**: Port on which redis caching engine is running.
- **password**: Redis caching engine password.
- **enableTsl**: This field is used to enable tsl.
- **defaultTtl**: Default cache expiry time in sec.
- **consistentBehavior**: This field is required to create cache instance key.
- **engine**: Pass value `redis` for using UniCahce with Redis.
- **host**: Host of the Redis server.
- **port**: Port of the Redis server.
- **password**: Password for auth with Redis server.
- **enableTsl**: Pass '1' for enabling TSL. Otherwise '0'
- **defaultTtl**: Default time to live (TTL) for cache in seconds.
- **consistentBehavior**: Pass '1' to use the consistent behaviour accross various caching engines option. Otherwise '0'.

#### Memcache Config Strategy
Following is the memcache engine config strategy to be used in initializing UniCache.
Following is how the config strategy looks for Memcache caching engine.
```js
const configStrategy = {
engine: "memcached",
Expand All @@ -98,13 +103,13 @@ const configStrategy = {
consistentBehavior: "1"
}
````
- **engine**: memcached caching engine to be used.
- **servers**: servers is an array of memcached servers.
- **defaultTtl**: Default cache expiry time in sec.
- **consistentBehavior**: This field is required to create cache instance key.
- **engine**: Pass value `memcached` for using UniCahce with Memcache.
- **servers**: Servers is an array of memcached servers' hosts.
- **defaultTtl**: Default time to live (TTL) for cache in seconds.
- **consistentBehavior**: Pass '1' to use the consistent behaviour accross various caching engines option. Otherwise '0'.
#### In-Memory Config Strategy
Following is the in-memory engine config strategy to be used in initializing UniCache.
Following is how the config strategy looks for In-memory caching engine.
```js
const configStrategy = {
engine: "none",
Expand All @@ -113,13 +118,14 @@ const configStrategy = {
consistentBehavior: "1"
}
```
- **engine**: For in-memory cache engine parameter will be `none`.
- **namespace**: It is in-memory cache namespace.
- **defaultTtl**: Default cache expiry time in sec.
- **consistentBehavior**: This field is required to create cache instance key.
- **engine**: Pass value `none` for using UniCahce with In-memory cache.
- **namespace**: In-memory cache namespace. You can segregate cache keys in the same machine with different namespaces.
- **defaultTtl**: Default time to live (TTL) for cache in seconds.
- **consistentBehavior**: Pass '1' to use the consistent behaviour accross various caching engines option. Otherwise '0'.

## `cacheImplementer` methods
Irrespective of the caching engine, the methods exposed in `cacheImplementer` have the consistent signature.
Irrespective of the caching engine, the methods exposed in `cacheImplementer` have the consistent signature,
i.e. singleton interface implementation.

### Store and retrieve data in cache using `set` and `get`:
```js
Expand Down Expand Up @@ -177,14 +183,17 @@ cacheImplementer.touch('testKey', 10).then(resolvePromise);

## Test Cases
Test cases cover all the 3 caching engines. Also a coverage report is generated at the end.
Running test cases is a 2 step process.

### Step 1: Start Cache engines
### Step 1: Start Cache Engines
Following processes are needed for running test cases.

* Start Redis on 2 ports - 6380 and 6381 as needed by the test cases.
```shell script
redis-server --port 6380
redis-server --port 6381
```

* Start Memcached on 4 ports - 11212,11213,11214 and 11215 as needed by the test cases.
```shell script
memcached -p 11212 -d
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0-beta.1
1.0.0
2 changes: 1 addition & 1 deletion config/coreConstant.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CoreConstant {
*/
get DEBUG_ENABLED() {
// eslint-disable-next-line no-process-env
return process.env.CACHE_DEBUG_ENABLED;
return process.env.UNICACHE_DEBUG_ENABLED;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@plgworks/unicache",
"version": "1.0.0-beta.1",
"version": "1.0.0",
"description": "UniCache is an NPM package that provides singleton interface and behavior for Memcached, Redis and In-memory caching. Easily interact or switch between them in minutes!",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 6bfa643

Please sign in to comment.