123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- var h = require('../helper');
- var async = require('async');
- // "If hiredis [pure C library] is installed, node_redis will use it by default.
- // Otherwise, a pure JavaScript parser will be used."
- // >> Previous versions of this project installed hiredis; however, hiredis now has
- // >> installation issues and redis is no longer tested, so the hiredis dependency was taken out.
- // >> If redis tests are re-instated, try to re-add hiredis dependency; however, if it does
- // >> not install correctly, the tests will still work with the redis default JS parser
- var redis = require('redis');
- var client = redis.createClient(6379, '127.0.0.1', {});
- client.on('error', function (err) {
- console.log('Redis Error: ' + err);
- // Do nothing further if Redis errors/is unavailable
- });
- function redisWorldId(id) {
- return 'world:' + id;
- }
- function redisRandomWorld(callback) {
- var id = h.randomTfbNumber();
- var redisId = redisWorldId(id);
- client.get(redisId, function (err, worldValue) {
- var world = {
- id: id,
- randomNumber: worldValue
- }
- callback(err, world);
- });
- }
- function redisSetWorld(world, callback) {
- var redisId = redisWorldId(world.id);
- client.set(redisId, world.randomNumber, function (err, result) {
- callback(err, world);
- });
- }
- function redisGetAllFortunes(callback) {
- client.lrange('fortunes', 0, -1, function (err, fortuneMessages) {
- if (err) { return process.exit(1); }
- var fortunes = fortuneMessages.map(function (e, i) {
- return { id: i + 1, message: e }
- });
- callback(err, fortunes)
- });
- }
- module.exports = {
-
- SingleQuery: function(req, res) {
- redisRandomWorld(function (err, world) {
- if (err) { return process.exit(1); }
- h.addTfbHeaders(res, 'json');
- res.end(JSON.stringify(world));
- })
- },
- MultipleQueries: function(queries, req, res) {
- var queryFunctions = h.fillArray(redisRandomWorld, queries);
- async.parallel(queryFunctions, function (err, worlds) {
- if (err) { return process.exit(1); }
- h.addTfbHeaders(res, 'json');
- res.end(JSON.stringify(worlds));
- })
- },
- Fortunes: function(req, res) {
- redisGetAllFortunes(function (err, fortunes) {
- if (err) { return process.exit(1); }
- h.addTfbHeaders(res, 'html');
- fortunes.push(h.ADDITIONAL_FORTUNE);
- fortunes.sort(function (a, b) {
- return a.message.localeCompare(b.message);
- });
- res.end(h.fortunesTemplate({
- fortunes: fortunes
- }));
- });
- },
- Updates: function(queries, req, res) {
- var getFunctions = h.fillArray(redisRandomWorld, queries);
- async.parallel(getFunctions, function (err, worlds) {
- if (err) { return process.exit(1); }
- var updateFunctions = [];
- worlds.forEach(function (w) {
- w.id = h.randomTfbNumber();
- updateFunctions.push(function (callback) {
- if (err) { return process.exit(1); }
- return redisSetWorld(w, callback);
- });
- });
- async.parallel(updateFunctions, function (err, updated) {
- if (err) { return process.exit(1); }
- h.addTfbHeaders(res, 'json');
- res.end(JSON.stringify(updated));
- });
- });
- }
- };
|