Skip to content

Commit

Permalink
Update README to reflect latest interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dabroek committed Oct 16, 2022
1 parent f32a895 commit 04d04cb
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
[![npm version](https://badge.fury.io/js/cache-manager-redis-store.svg)](https://badge.fury.io/js/cache-manager-redis-store)
[![GitHub issues](https://img.shields.io/github/issues/dabroek/node-cache-manager-redis-store.svg)](https://github.com/dabroek/node-cache-manager-redis-store/issues)
[![codecov](https://codecov.io/github/dabroek/node-cache-manager-redis-store/branch/master/graph/badge.svg?token=QmCNGyCLlD)](https://codecov.io/github/dabroek/node-cache-manager-redis-store)

Redis store for node cache manager
==================================

Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager).
Redis cache store for [node-cache-manager](https://github.com/BryanDonovan/node-cache-manager).

How is this package different from `node-cache-manager-redis`?
----------------------------------------------------------------------------------
This is a **completely different version** than the earlier [node-cache-manager-redis](https://github.com/dial-once/node-cache-manager-redis). This package does not use `redis-pool` which is unnecessary and not actively maintained.

This package aims to provide **the most simple wrapper possible** by just passing the configuration to the underlying `node_redis` package.

Installation
Expand All @@ -32,15 +33,20 @@ See examples below on how to implement the Redis cache store.

```js
var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');

var redisCache = cacheManager.caching({
store: redisStore,
host: 'localhost', // default value
port: 6379, // default value
auth_pass: 'XXXXX',
var redisStore = require('cache-manager-redis-store').redisStore;

var config = {
socket: {
host: 'localhost', // default value
port: 6379, // default value
},
password: 'XXXXX',
db: 0,
ttl: 600
};

var redisCache = cacheManager.caching({
store: await redisStore(config),
});

// listen for redis connection error event
Expand All @@ -53,19 +59,24 @@ redisClient.on('error', (error) => {

var ttl = 5;

redisCache.set('foo', 'bar', { ttl: ttl }, (err) => {
await redisCache.set('foo', 'bar', { ttl: ttl });

// You can use either a Promise...
var result = await redisCache.get('foo');
console.log(result);

// ...or a callback
redisCache.get('foo', (err, result) => {
if (err) {
throw err;
// handle error here
}

redisCache.get('foo', (err, result) => {
console.log(result);
// >> 'bar'
redisCache.del('foo', (err) => {
});
});
console.log(result);
});

// >> 'bar'
console.log(await redisCache.del('foo'));
// >> 1

function getUser(id, cb) {
setTimeout(() => {
console.log("Returning user from slow database.");
Expand Down Expand Up @@ -96,9 +107,9 @@ redisCache.wrap(key, (cb) => {

```js
var cacheManager = require('cache-manager');
var redisStore = require('cache-manager-redis-store');
var redisStore = require('cache-manager-redis-store').redisStore;

var redisCache = cacheManager.caching({ store: redisStore, db: 0, ttl: 600 });
var redisCache = cacheManager.caching({ store: await redisStore({ ...config, db: 0, ttl: 600 }) });
var memoryCache = cacheManager.caching({ store: 'memory', max: 100, ttl: 60 });

var multiCache = cacheManager.multiCaching([memoryCache, redisCache]);
Expand All @@ -107,19 +118,14 @@ var userId2 = 456;
var key2 = `user_${userId2}`;

// Set value in all caches
multiCache.set('foo2', 'bar2', { ttl: ttl }, (err) => {
if (err) {
throw err;
}

// Fetches from highest priority cache that has the key
multiCache.get('foo2', (err, result) => {
console.log(result);

// Delete from all caches
multiCache.del('foo2');
});
});
await multiCache.set('foo2', 'bar2', { ttl: ttl });
// Fetches from highest priority cache that has the key
var result = await multiCache.get('foo2');
console.log(result);
// >> 'bar2'

// Delete from all caches
await multiCache.del('foo2');

// Note: ttl is optional in wrap
multiCache.wrap(key2, (cb) => {
Expand Down

0 comments on commit 04d04cb

Please sign in to comment.