easy-redis-cache

Easy node redis caching with promises/async interface and ttl

Usage no npm install needed!

<script type="module">
  import easyRedisCache from 'https://cdn.skypack.dev/easy-redis-cache';
</script>

README

A really simple wrapper around node-redis to make cache expiry easier and provide a promise/async interface.

Table of Contents

Getting Started

To get up and running quickly, follow these simple steps.

Installation

npm install easy-redis-cache -s

You need to already have an existing installed and configured instance of node-redis in your project:

const redis = require('redis');
const redisClient = redis.createClient(options);

const EasyRedisCache = require('easy-redis-cache');
const cache = new EasyRedisCache(redisClient);

Usage

There are only two methods:

Set a value: Both key and value are required. Value must be capable of being JSON.stringify'd. Otherwise, an error will be thrown.

let ttl = 3600; //optional: seconds until your cached item will expire.
let value = {foo: 'bar'};
await cache.set('key', value, ttl);

If you don't set a ttl value, your key will not expire. ttl must be an integer (representing seconds).

In many cases, you won't want or need to await setting cache values.

Get a value:

let result = await cache.get('key');
console.log(result);
//{hello: 'world'}

Errors:

This is a very thin wrapper on top of node-redis. For information on error handling, please refer to that documentation.

If in doubt, check out this source code. It's is very short and should be easy to read.

Roadmap

See the open issues for a list of proposed features (and known issues).

If you have any issues, please submit a PR.

Contributing

This is a pretty simple package, but any contributions you make are greatly appreciated. Just fork and submit a PR :)

License

Distributed under the MIT License.