Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created January 22, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naholyr/a5d25b9d5e56c4e96623 to your computer and use it in GitHub Desktop.
Save naholyr/a5d25b9d5e56c4e96623 to your computer and use it in GitHub Desktop.
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