README
node-easy-cache
A cache abstraction for Node.js that supports both Redis and memcached using async
/await
(Javascript Promises). No plugins or configuration needed - supports Redis and memcached out of the box!
Use case:
- for developers to easily get started with Redis or memcached
- for developers to easily switch between Redis and memcached
- for developers to use Redis and memcached in the same codebase
Inspired by Shopify's go-cache
Uses redis and memjs under the hood
Test cases written with Jest and continuous integration with Docker, Docker Compose and Circle CI
Get started
npm install node-easy-cache
Check out the Example Project
Supported operations
All operations run the exact same on on Redis and memcached clients. Supported operations:
Function | Return type (if not void) |
---|---|
client.get(key) |
string or null if missed |
client.set(key, value, _ttl) |
|
client.delete(key) |
boolean |
client.increment(key, delta) |
Number |
client.decrement(key, delta) |
Number |
client.flushDB() |
|
client.closeClient() |
Note on integer behaviour in Redis vs memcached
Memcached does not allow storing negative integers, so the increment and decrement behaviour between Redis and memcached is different. However, this API makes them behave the same by using get and set operations for memcached using strings. Therefore, using increment
and decrement
for memcached is slower, but it will have the same behaviour as Redis.
If you want to use the default memcached behaviour, you can access the underlying client by accessing client.memcachedClient
Testing
Before testing, MAKE SURE THAT YOU HAVE NO IMPORTANT DATA IN REDIS/MEMCACHE! The test cases flush the database frequently.
Make sure you have
redis-server
running andmemcached
running. If you have Docker installed, you can rundocker-compose up
to run Dockerized containers for testingIf you're not running Redis or memcached locally on the default port, set the
REDIS_CONNECTION
orMEMCACHED_CONNECTION
environment variableRun
npm test
Contributing
Please report any bugs using the GitHub issue tracker.
If you want to contribute features, please add test cases and run prettier --write .
(code formatter) before submitting. Notice that the test cases for both Redis and memcached should be the exact same. You can run the validateTestCases.sh script to ensure this.