Skip to content

Commit

Permalink
Merge pull request #18 from pearlshare/master
Browse files Browse the repository at this point in the history
store json
  • Loading branch information
pasupulaphani committed Jan 14, 2017
2 parents 01d0bc6 + a9bf0ea commit 82c6fdb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
16 changes: 14 additions & 2 deletions lib/redis_store.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const RedisPool = require("./redis_pool");
const pick = require("lodash.pick");
const isArray = require("lodash.isarray");
const isJSON = require("is-json");

const debug = require("debug")("simpleRedisStore");

Expand Down Expand Up @@ -118,7 +120,15 @@ RedisStore.prototype.ping = function (str) {
* @returns {string} value or null when the key is missing
*/
RedisStore.prototype.get = function (key) {
return this.sendCommand("get", key);
return this.sendCommand("get", key)
.then(function (value) {
try {
value = JSON.parse(value);
} catch (e) {
// do nothing
}
return value;
});
};

/**
Expand All @@ -131,6 +141,8 @@ RedisStore.prototype.get = function (key) {
*/
RedisStore.prototype.set = function (key, value, ttlInSeconds) {

value = isArray(value) || isJSON(value, true) ? JSON.stringify(value) : value;

if (ttlInSeconds) {
return this.sendCommand("setex", [key, ttlInSeconds, value]);
} else {
Expand Down Expand Up @@ -211,7 +223,7 @@ RedisStore.prototype.deleteAll = function (pattern) {
/**
* Returns pool status and stats
*
* @returns {object} store status and stats
* @returns {object} pool status and stats
*/
RedisStore.prototype.status = function () {
return this.pool.status();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"debug": "^2.6.0",
"es6-promisify": "^5.0.0",
"generic-pool": "3.1.6",
"is-json": "^2.0.1",
"lodash.pick": "^4.4.0",
"redis": "^2.6.4",
"retry-as-promised": "^2.2.0"
Expand Down
53 changes: 46 additions & 7 deletions test/redis_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,15 @@ describe("redisStore", () => {

it("should set given name", () => {

const name = "testStore";
const store = new RedisStore({
name: name,
redisOptions: redisOptions
name: name
});

store.getName().should.be.equal(name);
});

it("should set random name if not set", () => {

const store = new RedisStore({
redisOptions: redisOptions
});
const store = new RedisStore();

store.getName().should.not.be.empty();
});
Expand Down Expand Up @@ -127,6 +122,7 @@ describe("redisStore", () => {
});
});


describe("get", () => {

const store = new RedisStore({
Expand All @@ -147,6 +143,21 @@ describe("redisStore", () => {
.should.eventually.be.equal(value);
});

it("should retrieve parsed json", () => {

const key = "chuck-norris";
const value = {
type: "superman"
};

return store.set(key, value)
.then(test => {
test.should.be.ok();
})
.then(() => store.get(key))
.should.eventually.be.eql(value);
});

it("should return null if key doesn't exist", () => {

return store.get("unknownKey")
Expand Down Expand Up @@ -174,6 +185,34 @@ describe("redisStore", () => {
.should.eventually.be.equal(value);
});

it("should store json", () => {

const key = "key";
const value = {
type: "json"
};

return store.set(key, value)
.then(test => {
test.should.be.ok();
})
.then(() => store.get(key))
.should.eventually.be.eql(value);
});

it("should store array", () => {

const key = "key";
const value = ["json", "node"];

return store.set(key, value)
.then(test => {
test.should.be.ok();
})
.then(() => store.get(key))
.should.eventually.be.eql(value);
});

it("should store with an expiry if ttl set", () => {

const key = "shortLivedKey";
Expand Down
18 changes: 6 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ bl@~1.1.2:
dependencies:
readable-stream "~2.0.5"

bluebird@^3.4.6, bluebird@~3.4.6:
version "3.4.6"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.6.tgz#01da8d821d87813d158967e743d5fe6c62cf8c0f"

bluebird@^3.4.7:
bluebird@^3.4.6, bluebird@^3.4.7, bluebird@~3.4.6:
version "3.4.7"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.4.7.tgz#f72d760be09b7f76d08ed8fae98b289a8d05fab3"

Expand Down Expand Up @@ -302,18 +298,12 @@ debug@2.2.0:
dependencies:
ms "0.7.1"

debug@^2.1.1, debug@^2.6.0:
debug@^2.1.1, debug@^2.2.0, debug@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.0.tgz#bc596bcabe7617f11d9fa15361eded5608b8499b"
dependencies:
ms "0.7.2"

debug@^2.2.0:
version "2.3.3"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.3.3.tgz#40c453e67e6e13c901ddec317af8986cda9eff8c"
dependencies:
ms "0.7.2"

decamelize@^1.0.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down Expand Up @@ -764,6 +754,10 @@ is-fullwidth-code-point@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-2.0.0.tgz#a3b30a5c4f199183167aaab93beefae3ddfb654f"

is-json@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/is-json/-/is-json-2.0.1.tgz#6be166d144828a131d686891b983df62c39491ff"

is-my-json-valid@^2.10.0, is-my-json-valid@^2.12.4:
version "2.15.0"
resolved "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.15.0.tgz#936edda3ca3c211fd98f3b2d3e08da43f7b2915b"
Expand Down

0 comments on commit 82c6fdb

Please sign in to comment.