@appfarm/ttl-cache-redis
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

ttl-cache-redis

This is a supersimple abstraction for storing strings and JSON-objects in redis.

Installing

Using npm:

$ npm install @appfarm/ttl-cache-redis

Example

const Cache = require('@appfarm/ttl-cache-redis')
const myCache = new Cache({
	redisConnectionString: 'redis://localhost:6379',
	cacheKeyPrefix: 'MY-CACHE-', // prefix any key with this value
})

// Setting "foo" to MY-CACHE-newkey with a
// lifetime of 10 minutes
myCache
	.setStringValue('newkey', 'foo', 600)
	.then(() => console.log('Successfully set string value'))
	.catch(err => console.error(err))

myCache
	.setObjectValue('myobject', { foo: 'bar' }, 300)
	.then(() => console.log('Successfully set object value'))
	.catch(err => console.error(err))

// Retrieve string value from cache
myCache
	.getStringValue(key)
	.then(value => {
		console.log('Found value:', value)
	})
	.catch(err => {
		console.log('Unable to find value or key has expired')
	})

// Retrieve object value from store
myCache
	.getObectValue(key)
	.then(objectValue => {
		console.log('Found object:', objectValue)
	})
	.catch(err => {
		console.log('Unable to find value or key has expired')
	})

// Fire and forget - delete value from store immediately
// Returns nothing.
myCache.invalidate('newkey')

Readme

Keywords

Package Sidebar

Install

npm i @appfarm/ttl-cache-redis

Weekly Downloads

1

Version

1.1.1

License

MIT

Unpacked Size

7.43 kB

Total Files

7

Last publish

Collaborators

  • timkurvers
  • olebor