Guest User

Untitled

a guest
Nov 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. const { promisify } = require('util');
  2. const redis = require('redis');
  3.  
  4. const REDIS_URL = process.env.REDIS_URL || 'redis://localhost:6379';
  5. const CACHE_KEY_PREFIX = process.env.REDIS_GLOBAL_CACHE_KEY_PREFIX || '';
  6. const CACHE_TTL = process.env.REDIS_TTL || 60 * 5; // seconds
  7. const DEFAULT_VERSION = 1;
  8.  
  9. const client = redis.createClient(REDIS_URL);
  10. const redisGet = promisify(client.get).bind(client);
  11. const redisSet = promisify(client.set).bind(client);
  12. const redisDelete = promisify(client.del).bind(client);
  13. const redisHget = promisify(client.hget).bind(client);
  14. const redisHset = promisify(client.hset).bind(client);
  15. const redisExists = promisify(client.exists).bind(client);
  16.  
  17. function getCacheKey({ prefix, version, key }) {
  18. const cachePrefix = prefix || CACHE_KEY_PREFIX;
  19. const cacheVersion = version || DEFAULT_VERSION;
  20. return `${cachePrefix}-${cacheVersion}-${key}`;
  21. }
  22.  
  23. function serialize(value) {
  24. if (typeof value === 'object') {
  25. return JSON.stringify(value);
  26. }
  27. return value;
  28. }
  29.  
  30. function deSerialize(cachedValue) {
  31. try {
  32. return JSON.parse(cachedValue);
  33. } catch (error) {
  34. return cachedValue;
  35. }
  36. }
  37.  
  38. async function get(key, options = {}) {
  39. const version = options.version || DEFAULT_VERSION;
  40. const cacheKey = getCacheKey({ key, version });
  41. return redisGet(cacheKey).then(result => deSerialize(result));
  42. }
  43.  
  44. async function set(key, value, options = {}) {
  45. const ttl = options.ttl || CACHE_TTL;
  46. const version = options.version || DEFAULT_VERSION;
  47. const cacheKey = getCacheKey({ key, version });
  48. const serializedValue = serialize(value);
  49.  
  50. if (!ttl) {
  51. return redisSet(cacheKey, serializedValue);
  52. }
  53. return redisSet(cacheKey, serializedValue, 'EX', ttl);
  54. }
  55.  
  56. async function del(key, options = {}) {
  57. const version = options.version || DEFAULT_VERSION;
  58. const cacheKey = getCacheKey({ key, version });
  59. return redisDelete(cacheKey);
  60. }
  61.  
  62. async function hget(key, field, options = {}) {
  63. const version = options.version || DEFAULT_VERSION;
  64. const cacheKey = getCacheKey({ key, version });
  65. return redisHget(cacheKey, field);
  66. }
  67.  
  68. async function hset(key, field, value, options = {}) {
  69. const version = options.version || DEFAULT_VERSION;
  70. const cacheKey = getCacheKey({ key, version });
  71. return redisHset(cacheKey, field, value);
  72. }
  73.  
  74. async function exists(key, options = {}) {
  75. const version = options.version || DEFAULT_VERSION;
  76. const cacheKey = getCacheKey({ key, version });
  77. return redisExists(cacheKey);
  78. }
  79.  
  80. module.exports = {
  81. get,
  82. set,
  83. del,
  84. hget,
  85. hset,
  86. exists,
  87. };
Add Comment
Please, Sign In to add comment