README
Small Orange RXJS Cache Driver
Simple pluggable reactive cache driver powered 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. It is pluggable with your custom logic via get, set, del, and clear operations.
Sample (with DynamoDB)
const {
Observable
} = require('rxjs');
const CacheDriver = require('smallorange-rxjs-cache-driver');
const {
DynamoDB
} = require('smallorange-dynamodb-client');
const dynamodb = new DynamoDB();
const namespace = 'someNamespace';
const cacheDriver = new CacheDriver({
ttr: 7200 * 1000, // default time to refresh
set: dynamodb.insertOrReplace,
get: dynamodb.get,
del: dynamodb.del,
clear: args => dynamodb.fetch(args)
.mergeMap(::dynamodb.del)
});
const fallback = args => Observable.of('value'); // fallback will be subscribed if id doesn't exists or is expired, this value will be attached to this id with provided ttr
cacheDriver.get({
namespace,
id: 'inexistentId'
}, fallback, {
ttr: 100 // custom ttr
})
.subscribe(response => {
console.log(response); // will print "value" from fallback
});
cacheDriver.get({
namespace,
id: 'someId'
}, fallback, {
ttr: 100
})
.subscribe(response => {
console.log(response); // will print "value" from cache
});
cacheDriver.get({
namespace,
id: 'someId',
forceRefresh: true
}, fallback, {
ttr: 100
})
.subscribe(response => {
console.log(response); // will print "value" from fallback and refresh cache with it
});
// IF EXPIRED
cacheDriver.get({
namespace,
id: 'someId'
}, 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 IDS WHICH CONTAIN "id-"
cacheDriver.markToRefresh({
namespace,
ids: ['id-']
})
.subscribe(response => {
console.log(response);
});
// UNSET id MANUALLY
cacheDriver.del({
namespace,
id: 'id-1'
})
.subscribe(response => {
console.log(response);
});
// CLEAR WHOLE NAMESPACE MANUALLY
cacheDriver.clear({
namespace
})
.subscribe(response => {
console.log(response);
});