README
Small Orange Cache Driver
Simple reactive cache driver powered by Redis and RxJS, it uses cache first strategy. Once expired, it returns cached value and feed cache in background to deliver fresh result at next request.
Sample
const {
Observable
} = require('rxjs');
const Redis = require('smallorange-redis-client');
const CacheDriver = require('smallorange-cache-driver');
const redis = new Redis({
port: 6380
});
const namespace = 'someNamespace';
const cacheDriver = new CacheDriver({
logError: console.error,
ttl: 7200 * 1000,
redis
});
const fallback = args => Observable.of('value'); // fallback will be subscribed if key doesn't exists or is expired, this value will be attached to this key with provided ttl
cacheDriver.get({
namespace,
key: 'someKey'
}, fallback)
.subscribe(response => {
console.log(response); // will print "value" from fallback
});
cacheDriver.get({
namespace,
key: 'someKey'
}, fallback)
.subscribe(response => {
console.log(response); // will print "value" from cache
});
// IF EXPIRED
cacheDriver.get({
namespace,
key: 'someKey'
}, fallback)
.subscribe(response => {
console.log(response); // will print "value" from cache, and run fallback in background to feed cache to the next request gets the fresh result
});
// FORCE CACHE REFRESH AFTER NEXT REQUEST ALL KEYS WHICH CONTAIN "key-"
cacheDriver.markToRefresh({
namespace,
keys: ['key-']
})
.subscribe(response => {
console.log(response); // will print 1 for all keys found
});
// UNSET KEY MANUALLY
cacheDriver.unset({
namespace,
key: 'key-1'
})
.subscribe(response => {
console.log(response); // will print 1 if found key
});
// UNSET MANY KEYS MANUALLY
cacheDriver.unsetLike({
namespace,
keys: ['key-']
})
.subscribe(response => {
console.log(response); // will print 1 for all keys found
});
// CLEAR WHOLE NAMESPACE MANUALLY
cacheDriver.clear({
namespace
})
.subscribe(response => {
console.log(response); // will print 1 for all keys found
});