redis.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var h = require('../helper');
  2. var async = require('async');
  3. // "If hiredis [pure C library] is installed, node_redis will use it by default.
  4. // Otherwise, a pure JavaScript parser will be used."
  5. // >> Previous versions of this project installed hiredis; however, hiredis now has
  6. // >> installation issues and redis is no longer tested, so the hiredis dependency was taken out.
  7. // >> If redis tests are re-instated, try to re-add hiredis dependency; however, if it does
  8. // >> not install correctly, the tests will still work with the redis default JS parser
  9. var redis = require('redis');
  10. var client = redis.createClient(6379, '127.0.0.1', {});
  11. client.on('error', function (err) {
  12. console.log('Redis Error: ' + err);
  13. // Do nothing further if Redis errors/is unavailable
  14. });
  15. function redisWorldId(id) {
  16. return 'world:' + id;
  17. }
  18. function redisRandomWorld(callback) {
  19. var id = h.randomTfbNumber();
  20. var redisId = redisWorldId(id);
  21. client.get(redisId, function (err, worldValue) {
  22. var world = {
  23. id: id,
  24. randomNumber: worldValue
  25. }
  26. callback(err, world);
  27. });
  28. }
  29. function redisSetWorld(world, callback) {
  30. var redisId = redisWorldId(world.id);
  31. client.set(redisId, world.randomNumber, function (err, result) {
  32. callback(err, world);
  33. });
  34. }
  35. function redisGetAllFortunes(callback) {
  36. client.lrange('fortunes', 0, -1, function (err, fortuneMessages) {
  37. if (err) { return process.exit(1); }
  38. var fortunes = fortuneMessages.map(function (e, i) {
  39. return { id: i + 1, message: e }
  40. });
  41. callback(err, fortunes)
  42. });
  43. }
  44. module.exports = {
  45. SingleQuery: function(req, res) {
  46. redisRandomWorld(function (err, world) {
  47. if (err) { return process.exit(1); }
  48. h.addTfbHeaders(res, 'json');
  49. res.end(JSON.stringify(world));
  50. })
  51. },
  52. MultipleQueries: function(queries, req, res) {
  53. var queryFunctions = h.fillArray(redisRandomWorld, queries);
  54. async.parallel(queryFunctions, function (err, worlds) {
  55. if (err) { return process.exit(1); }
  56. h.addTfbHeaders(res, 'json');
  57. res.end(JSON.stringify(worlds));
  58. })
  59. },
  60. Fortunes: function(req, res) {
  61. redisGetAllFortunes(function (err, fortunes) {
  62. if (err) { return process.exit(1); }
  63. h.addTfbHeaders(res, 'html');
  64. fortunes.push(h.ADDITIONAL_FORTUNE);
  65. fortunes.sort(function (a, b) {
  66. return a.message.localeCompare(b.message);
  67. });
  68. res.end(h.fortunesTemplate({
  69. fortunes: fortunes
  70. }));
  71. });
  72. },
  73. Updates: function(queries, req, res) {
  74. var getFunctions = h.fillArray(redisRandomWorld, queries);
  75. async.parallel(getFunctions, function (err, worlds) {
  76. if (err) { return process.exit(1); }
  77. var updateFunctions = [];
  78. worlds.forEach(function (w) {
  79. w.id = h.randomTfbNumber();
  80. updateFunctions.push(function (callback) {
  81. if (err) { return process.exit(1); }
  82. return redisSetWorld(w, callback);
  83. });
  84. });
  85. async.parallel(updateFunctions, function (err, updated) {
  86. if (err) { return process.exit(1); }
  87. h.addTfbHeaders(res, 'json');
  88. res.end(JSON.stringify(updated));
  89. });
  90. });
  91. }
  92. };