From a1d5f79708c1b4f3aad02dbe84f856e1ec3e11a6 Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Fri, 18 Nov 2011 19:52:18 -0500 Subject: [PATCH] Added Redis store support --- README.md | 34 +++++++++++++++++++++++++++++++++ TODO | 6 +++++- config.js | 6 ++++-- lib/redis_document_store.js | 38 +++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 lib/redis_document_store.js diff --git a/README.md b/README.md index 3a21e6a..86b83fe 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,40 @@ Major design objectives: 3. `npm install` 4. `npm start` +## Storage + +## File + +To use file storage (the default) change the storage section in `config.js` to something like: + +``` json +{ + "path": "./data", + "type": "file" +} +``` + +Where `path` represents where you want the files stored + +### Redis + +To use redis storage you must install the redis package in npm globall using + +`npm install redis --global` + +Once you've done that, your config section should look like: + +``` json +{ + "type": "redis", + "host": "localhost", + "port": 6379, + "db": 2 +} +``` + +All of which are optional except `type` with very logical default values. + ## Author John Crepezzi diff --git a/TODO b/TODO index 0feec28..5f71637 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,9 @@ cache headers for static assets -redis storing +chng how file def is written +make redis work without in package +make redis connection into a separate method + +tests # shared version only twitter posting with ^T diff --git a/config.js b/config.js index 502b766..6e2f10b 100644 --- a/config.js +++ b/config.js @@ -14,8 +14,10 @@ ], "storage": { - "type": "file", - "path": "./data" + "type": "redis", + "host": "localhost", + "port": 6379, + "db": 2 } } diff --git a/lib/redis_document_store.js b/lib/redis_document_store.js new file mode 100644 index 0000000..ac09b28 --- /dev/null +++ b/lib/redis_document_store.js @@ -0,0 +1,38 @@ +var redis = require('redis'); +var winston = require('winston'); +var hashlib = require('hashlib'); + +// TODO move to a different method (conn) +var RedisDocumentStore = function(options) { + if (!RedisDocumentStore.client) { + var host = options.host || '127.0.0.1'; + var port = options.port || 6379; + var index = options.db || 0; + RedisDocumentStore.client = redis.createClient(port, host); + RedisDocumentStore.client.select(index, function(err, reply) { + if (err) { + winston.error('error connecting to redis index ' + index, { error: error.message }); + process.exit(1); + } + else { + winston.info('connected to redis on ' + host + ':' + port + '/' + index); + } + }); + } +}; + +// Save file in a key +RedisDocumentStore.prototype.set = function(key, data, callback) { + RedisDocumentStore.client.set(key, data, function(err, reply) { + callback(!err); + }); +}; + +// Get a file from a key +RedisDocumentStore.prototype.get = function(key, callback) { + RedisDocumentStore.client.get(key, function(err, reply) { + callback(err ? false : reply); + }); +}; + +module.exports = RedisDocumentStore; diff --git a/package.json b/package.json index 3dcfc13..06b9d69 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,8 @@ "dependencies": { "winston": "*", - "hashlib": "*" + "hashlib": "*", + "package": "*" }, "devDependencies": {