Skip to content

Commit

Permalink
Merge pull request #12 from PLG-Works/prep_for_ph_launch
Browse files Browse the repository at this point in the history
Release 1.0
  • Loading branch information
kedarchandrayan authored Oct 14, 2022
2 parents 1c50f75 + 1d59336 commit a28c74f
Show file tree
Hide file tree
Showing 33 changed files with 568 additions and 215 deletions.
71 changes: 71 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock
package-lock.json

# SDK
/.idea
/.idea/*
.DS_Store

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Dev Folder
.dev

# Debug log from npm
npm-debug.log
.vscode

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
37 changes: 37 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
dist: bionic
language: node_js
sudo: required
branches:
only:
- master
- prep_for_ph_launch
- /^release-.*/
notifications:
email:
recipients:
- [email protected]
on_success: always
on_failure: always
node_js:
- "18"
- "16"
services:
- "memcached"
before_install:
- sudo apt-get update
- sudo apt-get install nodejs
- sudo apt-get install npm
install:
- npm install
before_script:
- memcached -p 11212 -d
- memcached -p 11213 -d
- memcached -p 11214 -d
- memcached -p 11215 -d
- sudo redis-server /etc/redis/redis.conf --port 6380 --requirepass 'my-secret'
- sudo redis-server /etc/redis/redis.conf --port 6381 --requirepass 'my-secret'
- ps aux | grep 'memcached'
- ps aux | grep 'redis-server'
script:
- npm run test
after_script: "skip"
14 changes: 3 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
## Cache v1.0.3
- LICENSE changes.

## Cache v1.0.2
- `redis` NPM version updated to 3.1.1 for fixing `Potential exponential regex in monitor mode` security vulnerability.

## Cache v1.0.1
- First time cache set intermittent issue fixed for memcached.

## Cache v1.0.0
- Cache is cache implementation of three caching engines Memcached, Redis and In-memory.
## UniCache v1.0.0-beta.1
- First release of UniCache having support for 3 caching engines - Memcached, Redis and In-memory Cache
- Test case coverage improved to 95%.
30 changes: 30 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Contributing

Helping hands are always welcome to contribute with bug fixes and enhancements to UniCache. Feel free to report issues
which you observe while integrating UniCache in your project.

## How to Report Issues
To report an issue, please use the [Github issues tracker](https://github.com/PLG-Works/UniCache/issues). When reporting issues,
please mention the following details, whichever applicable:
- Steps to reproduce with sample snippets.
- Actual vs. expected behaviour.
- Whether it is a bug or proposed enhancement.
- Release version which the issue is concerned with.

## Workflow for Pull Requests
In order to contribute, please fork off the particular release-{version} branch and make your changes there. Your commit
messages should detail why you made your change in addition to what you did (unless it is a tiny change).

If you are enhancing with a new feature, please ensure to add appropriate test cases under `test/` folder. It's also a
good practice to add new test cases in case of bug fixes too.

Please update CHANGELOG.md with each PR.

If you are taking up a big change, please add an issue and discuss with the UniCache team first to get opinions from
multiple developers.

Finally, please respect the coding style of this project. And of course, please test your code thoroughly before raising PR.

Thank you for your help!


177 changes: 95 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,108 +1,122 @@
# Cache
![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]

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

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).
The decision of which caching engine to use is governed while creating the Cache NPM object.
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!

## Why Cache?
Core packages of different caching systems do not have a common interface, i.e. they have the same functionality implemented with different method signatures.
Thus changing from one cache system to another becomes difficult as all the usages need to be revisited.
This NPM package solves the problem by providing common wrapper methods for memcached, redis and in-memory caching systems.
## 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.
- 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.
- UniCache is thoroughly tested and is fully compatible with AWS ElastiCache for Redis and AWS ElastiCache for Memcached.

## Prerequisites
Required caching engine for the use case must be installed and up.
Refer [memcached](https://memcached.org/) and [redis](https://redis.io/docs/getting-started/installation/) installation guide.
- [Node.js](https://nodejs.org/en/) (>= version 6)
- [NPM](https://www.npmjs.com/package/npm)

## Installion
Follow the installation guides to get the caching engines of your choice, up and running:
- [Memcached installation guide](https://memcached.org/)
- [Redis installation guide](https://redis.io/docs/getting-started/installation/)

## Install NPM
```shell script
npm install @plgworks/cache --save
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.

```js
const Cache = require('@plgworks/cache');
// Include the following snippet in a separate file, which can be required all accross the code to get unicache instance.
// If using different caching engines simultaneously in a single codebase, have different files for each.
const UniCache = require('@plgworks/unicache');

const configStrategy = {}; // Refer the next section for detailed documentation on configStrategy
const cache = Cache.getInstance(configStrategy);
const configStrategy = {
engine: "none/redis/memcached",
// Other keys depend on the engine, refer to the next section on Config Strategy, for the same.
};

const cacheImplementer = cache.cacheInstance;
module.exports = UniCache.getInstance(configStrategy);
```

The singleton object can be used as given below.
```js
const cacheProvider = require('path-to-your-uni-cache-singleton-provider');
const cacheImplementer = cacheProvider.cacheInstance;

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

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

### Config Strategy
**`configStrategy`** is a mandatory parameter which specifies the configuration strategy to be used for a particular cache engine.
**`configStrategy`** is a mandatory parameter which specifies the configuration strategy 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:
```js
const configStrategy = {
cache: {
engine: "none/redis/memcached",
host: "",
port: "",
password: "",
enableTsl: "",
defaultTtl: 10000,
consistentBehavior: "",
servers:[],
namespace: ""
}
// other keys depend on the engine.
};
```
- **engine**: redis, memcached are different types of caching engine. For in-memory cache engine parameter will be `none`.

#### Redis Config Strategy
Following is the redis engine config strategy to be used in initializing UniCache.
```js
const configStrategy = {
engine: "redis",
host: "localhost",
port: "6380",
password: "dsdsdsd",
enableTsl: "0",
defaultTtl: 36000,
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.
- **servers**: servers is an array of memcached servers.
- **namespace**: It is in-memory cache namespace.


#### Redis Example
Following is an example of redis engine config strategy to be used in initializing Cache.
```js
const configStrategy = {
cache: {
engine: "redis",
host: "localhost",
port: "6830",
password: "dsdsdsd",
enableTsl: "0",
defaultTtl: 36000,
consistentBehavior: "1"
}
}
````
#### Memcache Example
Following is an example of memcache engine config strategy to be used in initializing Cache.
#### Memcache Config Strategy
Following is the memcache engine config strategy to be used in initializing UniCache.
```js
const configStrategy = {
cache: {
engine: "memcached",
servers: ["127.0.0.1:11211"],
defaultTtl: 36000,
consistentBehavior: "1"
}
engine: "memcached",
servers: ["127.0.0.1:11211"],
defaultTtl: 36000,
consistentBehavior: "1"
}
````
#### In-memory Example
Following is an example of in-memory engine config strategy to be used in initializing Cache.
- **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.
#### In-Memory Config Strategy
Following is the in-memory engine config strategy to be used in initializing UniCache.
```js
const configStrategy = {
cache: {
engine: "none",
namespace: "A",
defaultTtl: "36000",
consistentBehavior: "1"
}
engine: "none",
namespace: "A",
defaultTtl: "36000",
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.

## `cacheImplementer` methods
Irrespective of the caching engine, the methods exposed in `cacheImplementer` have the consistent signature.
Expand Down Expand Up @@ -161,32 +175,31 @@ cacheImplementer.set('testKey', "testData").then(console.log);
cacheImplementer.touch('testKey', 10).then(resolvePromise);
```

## Running test cases
### Set environment variables of particular cache engine for which you want to run the tests.

* Redis
```shell script
source test/env/redis.sh
```
* Memcached
```shell script
source test/env/memcached.sh
```
* In-memory
```shell script
source test/env/inMemory.sh
```
### Cache engines must be running on the specified ports.
## Test Cases
Test cases cover all the 3 caching engines. Also a coverage report is generated at the end.

* Redis (6380,6381)
### 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
```
* Memcached (11212,11213,11214,11215)
* Start Memcached on 4 ports - 11212,11213,11214 and 11215 as needed by the test cases.
```shell script
memcached -p 11212 -d
memcached -p 11213 -d
memcached -p 11214 -d
memcached -p 11215 -d
```
### Run tests

### Step 2: Run tests
For running tests for all the 3 caching engines, use the following command.
```shell script
./node_modules/.bin/mocha --recursive "./test/*.js"
npm run test
```

## Contribution
We welcome more helping hands to make UniCache better. Feel free to report issues, raise PRs for fixes & enhancements.

<p align="left">Built with :heart: by <a href="https://plgworks.com/" target="_blank">PLG Works</a></p>
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.3
1.0.0-beta.1
Loading

0 comments on commit a28c74f

Please sign in to comment.