1.1.0 • Published 7 years ago

smallorange-cache-driver v1.1.0

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

CircleCI

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	
	});

	
1.1.0

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago