Skip to content

Instantly share code, notes, and snippets.

@naholyr naholyr/cache.js
Created Jan 22, 2016

Embed
What would you like to do?
Redis cache client
import config from ''
import Redis from 'ioredis'
const client = new Redis(config.redis)
const cachePrefix = config.cache.prefix
const cacheDefaultTTL = config.cache.defaultTTL
// (string) => Promise<any>
export function get (key /* string */) {
return client.get(cachePrefix + key)
.then(JSON.parse)
}
// (string, any, number) => Promise<any>
export function set (key, value, ttl = cacheDefaultTTL) {
return client.set(cachePrefix + key, JSON.stringify(value))
.then(() => client.expire(cachePrefix + key, ttl))
.then(() => value)
}
// (string) => Promise<number>
export function del (key) {
return client.del(cachePrefix + key)
}
// (string) => Promise<number>
// Example: delMulti('*') to clear cache
export function delMulti (mask) {
return client.keys(cachePrefix + mask)
.then((keys) => client.del(keys))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.