index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 'use strict';
  2. const redis = require('redis');
  3. // Lightweight / convenience wrapper around redis module for our needs
  4. const pubs = {}, subs = {};
  5. let initialized = false;
  6. let callbacks = [];
  7. const lib = {
  8. client: null,
  9. url: '',
  10. schemas: {
  11. session: require('./schemas/session'),
  12. station: require('./schemas/station'),
  13. playlist: require('./schemas/playlist'),
  14. song: require('./schemas/song')
  15. },
  16. /**
  17. * Initializes the cache module
  18. *
  19. * @param {String} url - the url of the redis server
  20. * @param {Function} cb - gets called once we're done initializing
  21. */
  22. init: (url, cb) => {
  23. lib.url = url;
  24. lib.client = redis.createClient({ url: lib.url });
  25. lib.client.on('error', (err) => console.error(err));
  26. initialized = true;
  27. callbacks.forEach((callback) => {
  28. callback();
  29. });
  30. cb();
  31. },
  32. /**
  33. * Gracefully closes all the Redis client connections
  34. */
  35. quit: () => {
  36. lib.client.quit();
  37. Object.keys(pubs).forEach((channel) => pubs[channel].quit());
  38. Object.keys(subs).forEach((channel) => subs[channel].client.quit());
  39. },
  40. /**
  41. * Sets a single value in a table
  42. *
  43. * @param {String} table - name of the table we want to set a key of (table === redis hash)
  44. * @param {String} key - name of the key to set
  45. * @param {*} value - the value we want to set
  46. * @param {Function} cb - gets called when the value has been set in Redis
  47. * @param {Boolean} [stringifyJson=true] - stringify 'value' if it's an Object or Array
  48. */
  49. hset: (table, key, value, cb, stringifyJson = true) => {
  50. // automatically stringify objects and arrays into JSON
  51. if (stringifyJson && ['object', 'array'].includes(typeof value)) value = JSON.stringify(value);
  52. lib.client.hset(table, key, value, err => {
  53. if (cb !== undefined) {
  54. if (err) return cb(err);
  55. cb(null);
  56. }
  57. });
  58. },
  59. /**
  60. * Gets a single value from a table
  61. *
  62. * @param {String} table - name of the table to get the value from (table === redis hash)
  63. * @param {String} key - name of the key to fetch
  64. * @param {Function} cb - gets called when the value is returned from Redis
  65. * @param {Boolean} [parseJson=true] - attempt to parse returned data as JSON
  66. */
  67. hget: (table, key, cb, parseJson = true) => {
  68. lib.client.hget(table, key, (err, value) => {
  69. if (err) return typeof cb === 'function' ? cb(err) : null;
  70. if (parseJson) try { value = JSON.parse(value); } catch (e) {}
  71. if (typeof cb === 'function') cb(null, value);
  72. });
  73. },
  74. /**
  75. * Deletes a single value from a table
  76. *
  77. * @param {String} table - name of the table to delete the value from (table === redis hash)
  78. * @param {String} key - name of the key to delete
  79. * @param {Function} cb - gets called when the value has been deleted from Redis or when it returned an error
  80. */
  81. hdel: (table, key, cb) => {
  82. lib.client.hdel(table, key, (err) => {
  83. if (err) return typeof cb === 'function' ? cb(err) : null;
  84. if (typeof cb === 'function') cb(null);
  85. });
  86. },
  87. /**
  88. * Returns all the keys for a table
  89. *
  90. * @param {String} table - name of the table to get the values from (table === redis hash)
  91. * @param {Function} cb - gets called when the values are returned from Redis
  92. * @param {Boolean} [parseJson=true] - attempts to parse all values as JSON by default
  93. */
  94. hgetall: (table, cb, parseJson = true) => {
  95. lib.client.hgetall(table, (err, obj) => {
  96. if (err) return typeof cb === 'function' ? cb(err) : null;
  97. if (parseJson && obj) Object.keys(obj).forEach((key) => { try { obj[key] = JSON.parse(obj[key]); } catch (e) {} });
  98. cb(null, obj);
  99. });
  100. },
  101. /**
  102. * Publish a message to a channel, caches the redis client connection
  103. *
  104. * @param {String} channel - the name of the channel we want to publish a message to
  105. * @param {*} value - the value we want to send
  106. * @param {Boolean} [stringifyJson=true] - stringify 'value' if it's an Object or Array
  107. */
  108. pub: (channel, value, stringifyJson = true) => {
  109. /*if (pubs[channel] === undefined) {
  110. pubs[channel] = redis.createClient({ url: lib.url });
  111. pubs[channel].on('error', (err) => console.error);
  112. }*/
  113. if (stringifyJson && ['object', 'array'].includes(typeof value)) value = JSON.stringify(value);
  114. //pubs[channel].publish(channel, value);
  115. lib.client.publish(channel, value);
  116. },
  117. /**
  118. * Subscribe to a channel, caches the redis client connection
  119. *
  120. * @param {String} channel - name of the channel to subscribe to
  121. * @param {Function} cb - gets called when a message is received
  122. * @param {Boolean} [parseJson=true] - parse the message as JSON
  123. */
  124. sub: (channel, cb, parseJson = true) => {
  125. if (initialized) {
  126. func();
  127. } else {
  128. callbacks.push(() => {
  129. func();
  130. });
  131. }
  132. function func() {
  133. if (subs[channel] === undefined) {
  134. subs[channel] = { client: redis.createClient({ url: lib.url }), cbs: [] };
  135. subs[channel].client.on('error', (err) => console.error(err));
  136. subs[channel].client.on('message', (channel, message) => {
  137. if (parseJson) try { message = JSON.parse(message); } catch (e) {}
  138. subs[channel].cbs.forEach((cb) => cb(message));
  139. });
  140. subs[channel].client.subscribe(channel);
  141. }
  142. subs[channel].cbs.push(cb);
  143. }
  144. }
  145. };
  146. module.exports = lib;