redis.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Connects to Redis using the node_redis and hiredis drivers
  2. // Handles related routes
  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 h = require('../helper');
  7. var Promise = require('bluebird');
  8. // Can treat redis library as one that supports Promises
  9. // these methods will then have "-Async" appended to them.
  10. var redis = Promise.promisifyAll(require('redis'));
  11. var client = redis.createClient(6379, '127.0.0.1', {});
  12. client.on('error', function (err) {
  13. console.log('Redis Error: ' + err);
  14. // Do nothing further if Redis errors/is unavailable
  15. });
  16. function redisWorldId(id) {
  17. return 'world:' + id;
  18. }
  19. function randomWorldPromise() {
  20. var id = h.randomTfbNumber();
  21. var redisId = redisWorldId(id);
  22. var promise = client.getAsync(redisId)
  23. .then(function (worldValue) {
  24. return {
  25. id: id,
  26. randomNumber: worldValue
  27. }
  28. })
  29. .catch(function (err) {
  30. process.exit(1);
  31. });
  32. return promise;
  33. }
  34. function redisSetWorld(world) {
  35. var redisId = redisWorldId(world.id);
  36. var promise = client
  37. .setAsync(redisId, world.randomNumber)
  38. .then(function (result) {
  39. return world;
  40. })
  41. .catch(function (err) {
  42. process.exit(1);
  43. });
  44. return promise;
  45. }
  46. function redisGetAllFortunes() {
  47. var promise = client
  48. .lrangeAsync('fortunes', 0, -1)
  49. .then(function (fortuneMessages) {
  50. var fortunes = fortuneMessages.map(function (e, i) {
  51. return { id: i + 1, message: e }
  52. });
  53. return fortunes;
  54. })
  55. .catch(function (err) {
  56. if (err) { return process.exit(1); }
  57. });
  58. return promise;
  59. }
  60. module.exports = {
  61. SingleQuery: function(req, reply) {
  62. randomWorldPromise()
  63. .then(function (world) {
  64. reply(world)
  65. .header('Server', 'hapi');
  66. })
  67. .catch(function (err) {
  68. if (err) { return process.exit(1); }
  69. })
  70. },
  71. MultipleQueries: function(req, reply) {
  72. var queries = h.getQueries(req);
  73. var worldPromises = h.fillArray(randomWorldPromise(), queries);
  74. Promise
  75. .all(worldPromises)
  76. .then(function (worlds) {
  77. reply(worlds)
  78. .header('Server', 'hapi');
  79. });
  80. },
  81. Fortunes: function(req, reply) {
  82. redisGetAllFortunes()
  83. .then(function (fortunes) {
  84. fortunes.push(h.ADDITIONAL_FORTUNE);
  85. fortunes.sort(function (a, b) {
  86. return a.message.localeCompare(b.message);
  87. });
  88. reply.view('fortunes', {
  89. fortunes: fortunes
  90. })
  91. .header('Content-Type', 'text/html')
  92. .header('Server', 'hapi');
  93. })
  94. .catch(function (err) {
  95. process.exit(1);
  96. })
  97. },
  98. Updates: function(req, reply) {
  99. var queries = h.getQueries(req)
  100. var worldPromises = h.fillArray(randomWorldPromise(), queries);
  101. Promise
  102. .all(worldPromises)
  103. .map(function (world) {
  104. world.randomNumber = h.randomTfbNumber();
  105. return redisSetWorld(world);
  106. })
  107. .then(function (updated) {
  108. reply(updated)
  109. .header('Server', 'hapi');
  110. })
  111. .catch(function (err) {
  112. process.exit(1);
  113. });
  114. }
  115. };