Created
January 22, 2016 20:51
-
-
Save naholyr/a5d25b9d5e56c4e96623 to your computer and use it in GitHub Desktop.
Redis cache client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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