redis.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // >> hiredis is installed for these tests
  6. var redis = require('redis');
  7. var client = redis.createClient(6379, '127.0.0.1', {});
  8. client.on('error', function (err) {
  9. console.log('Redis Error: ' + err);
  10. // Do nothing further if Redis errors/is unavailable
  11. });
  12. function redisWorldId(id) {
  13. return 'world:' + id;
  14. }
  15. function redisRandomWorld(callback) {
  16. var id = h.randomTfbNumber();
  17. var redisId = redisWorldId(id);
  18. client.get(redisId, function (err, worldValue) {
  19. var world = {
  20. id: id,
  21. randomNumber: worldValue
  22. }
  23. callback(err, world);
  24. });
  25. }
  26. function redisSetWorld(world, callback) {
  27. var redisId = redisWorldId(world.id);
  28. client.set(redisId, world.randomNumber, function (err, result) {
  29. callback(err, world);
  30. });
  31. }
  32. function redisGetAllFortunes(callback) {
  33. client.lrange('fortunes', 0, -1, function (err, fortuneMessages) {
  34. if (err) { return process.exit(1); }
  35. var fortunes = fortuneMessages.map(function (e, i) {
  36. return { id: i + 1, message: e }
  37. });
  38. callback(err, fortunes)
  39. });
  40. }
  41. module.exports = {
  42. SingleQuery: function(req, res) {
  43. redisRandomWorld(function (err, world) {
  44. if (err) { return process.exit(1); }
  45. h.addTfbHeaders(res, 'json');
  46. res.end(JSON.stringify(world));
  47. })
  48. },
  49. MultipleQueries: function(queries, req, res) {
  50. var queryFunctions = h.fillArray(redisRandomWorld, queries);
  51. async.parallel(queryFunctions, function (err, worlds) {
  52. if (err) { return process.exit(1); }
  53. h.addTfbHeaders(res, 'json');
  54. res.end(JSON.stringify(worlds));
  55. })
  56. },
  57. Fortunes: function(req, res) {
  58. redisGetAllFortunes(function (err, fortunes) {
  59. if (err) { return process.exit(1); }
  60. h.addTfbHeaders(res, 'html');
  61. fortunes.push(h.ADDITIONAL_FORTUNE);
  62. fortunes.sort(function (a, b) {
  63. return a.message.localeCompare(b.message);
  64. });
  65. res.end(h.fortunesTemplate({
  66. fortunes: fortunes
  67. }));
  68. });
  69. },
  70. Updates: function(queries, req, res) {
  71. var getFunctions = h.fillArray(redisRandomWorld, queries);
  72. async.parallel(getFunctions, function (err, worlds) {
  73. if (err) { return process.exit(1); }
  74. var updateFunctions = [];
  75. worlds.forEach(function (w) {
  76. w.id = h.randomTfbNumber();
  77. updateFunctions.push(function (callback) {
  78. if (err) { return process.exit(1); }
  79. return redisSetWorld(w, callback);
  80. });
  81. });
  82. async.parallel(updateFunctions, function (err, updated) {
  83. if (err) { return process.exit(1); }
  84. h.addTfbHeaders(res, 'json');
  85. res.end(JSON.stringify(updated));
  86. });
  87. });
  88. }
  89. };