GitHub PullRequestReviewCommentEvent at 2014-03-25 01:55:40
Repository: cache-redis
Actor: Raynos
Payload: {"comment":{"url":"https://api.github.com/repos/uber/cache-redis/pulls/comments/10915191","id":10915191,"diff_hunk":"@@ -1,5 +1,89 @@\n-function cacheRedis() {\n-    \n+var redisManager = require('redis-manager');\n+var EventEmitter = require('events').EventEmitter;\n+var util = require('util');\n+\n+function CacheRedis(config) {\n+    EventEmitter.call(this);\n+    this.host = config.host || 'localhost';\n+    this.port = config.port || 6379;\n+    this.redisConfig = config.redisConfig || {};\n+    this.namespace = config.namespace;\n+    this.lazy = config.lazy || false;\n+    this.redisClient = redisManager.getClient(this.port, this.host, this.redisConfig);\n+    this.redisClient.on('error', this.emit.bind(this, 'error'));\n+    this.cache = {};\n+\n+    if (!this.lazy) {\n+        this.redisClient.hgetall(this.namespace, function(err, cache) {\n+            if (err) this.emit('error', err);\n+            if (cache) {\n+                Object.keys(cache).forEach(function(key) {\n+                    this.cache[key] = JSON.parse(cache[key]);\n+                }.bind(this));\n+            }\n+        }.bind(this));\n+    }\n }\n \n-module.exports = cacheRedis;\n+util.inherits(CacheRedis, EventEmitter);\n+\n+CacheRedis.prototype.close = function() {\n+    redisManager.freeClient(this.redisClient);\n+};\n+\n+CacheRedis.prototype.set = function(key, val, callback) {\n+    this.cache[key] = val;\n+    this.redisClient.hset(this.namespace, key, JSON.stringify(val), callback);\n+};\n+\n+CacheRedis.prototype.getLocal = function(key) {\n+    return this.cache[key];\n+};\n+\n+CacheRedis.prototype.get = function(key, callback) {\n+    if (this.cache.hasOwnProperty(key)) return process.nextTick(callback.bind(this, null, this.cache[key]));\n+    this.redisClient.hget(this.namespace, key, function(err, val) {\n+        if (err) return callback(err);\n+        console.log(val, JSON.parse(val));\n+        this.cache[key] = JSON.parse(val) || undefined;\n+        callback(null, this.cache[key]);\n+    }.bind(this));\n+};\n+\n+CacheRedis.prototype.keysLocal = function() {\n+    return Object.keys(this.cache);\n+};\n+\n+CacheRedis.prototype.keys = function(callback) {\n+    this.redisClient.hkeys(this.namespace, callback);\n+};\n+\n+CacheRedis.prototype.valuesLocal = function() {\n+    return Object.keys(this.cache).map(function(key) { return this.cache[key]; }.bind(this));","path":"index.js","position":65,"original_position":65,"commit_id":"13eb30a6e7a635909aacd525a021349e07f6ac19","original_commit_id":"13eb30a6e7a635909aacd525a021349e07f6ac19","user":{"login":"Raynos","id":479538,"avatar_url":"https://avatars.githubusercontent.com/u/479538?","gravatar_id":"d840cb1fb7e828284011cc08f40a1015","url":"https://api.github.com/users/Raynos","html_url":"https://github.com/Raynos","followers_url":"https://api.github.com/users/Raynos/followers","following_url":"https://api.github.com/users/Raynos/following{/other_user}","gists_url":"https://api.github.com/users/Raynos/gists{/gist_id}","starred_url":"https://api.github.com/users/Raynos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Raynos/subscriptions","organizations_url":"https://api.github.com/users/Raynos/orgs","repos_url":"https://api.github.com/users/Raynos/repos","events_url":"https://api.github.com/users/Raynos/events{/privacy}","received_events_url":"https://api.github.com/users/Raynos/received_events","type":"User","site_admin":false},"body":"`Array.prototype.map` takes a second argument, namely `this` so you don't need `.bind(this)`\r\n\r\nSlightly more idiomatic, slightly faster.","created_at":"2014-03-25T01:55:40Z","updated_at":"2014-03-25T01:55:40Z","html_url":"https://github.com/uber/cache-redis/pull/1#discussion_r10915191","pull_request_url":"https://api.github.com/repos/uber/cache-redis/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/uber/cache-redis/pulls/comments/10915191"},"html":{"href":"https://github.com/uber/cache-redis/pull/1#discussion_r10915191"},"pull_request":{"href":"https://api.github.com/repos/uber/cache-redis/pulls/1"}}}}