Skip to content

Commit e4f4530

Browse files
Merge pull request #3 from pearlshare/master
code improvements
2 parents 6f15c88 + df8e41f commit e4f4530

File tree

5 files changed

+78
-111
lines changed

5 files changed

+78
-111
lines changed

lib/redis_cache.js

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,61 @@ const RedisStore = require("./redis_store");
44

55
const debug = require("debug")("simpleRedisCache");
66

7-
function set (store, key, value, ttlInSeconds) {
8-
if (!store || ttlInSeconds === 0) return Promise.resolve(value);
9-
10-
if (ttlInSeconds) {
11-
return store.setex(key, value, ttlInSeconds);
12-
} else {
13-
return store.set(key, value);
14-
}
15-
}
16-
177
function RedisCache (name, redisOptions, poolOptions) {
18-
let store;
8+
9+
this.name = name;
10+
this.redisOptions = redisOptions;
11+
this.poolOptions = poolOptions;
1912

2013
try {
21-
store = new RedisStore(name, redisOptions, poolOptions);
14+
this.store = new RedisStore(name, redisOptions, poolOptions);
15+
debug("Success in connecting to Redis", redisOptions);
2216
} catch (e) {
23-
debug("Redis cache connection failed", e);
24-
return;
17+
debug("Failed in connecting to Redis", redisOptions);
2518
}
19+
}
2620

27-
return {
28-
wrap: (key, promise, ttlInSeconds) => {
21+
RedisCache.prototype.set = function (key, value, ttlInSeconds) {
22+
if (!this.store || ttlInSeconds === 0) return Promise.resolve(value);
2923

30-
if (!store || ttlInSeconds === 0) {
31-
return Promise.resolve(promise);
32-
}
24+
return this.store.set(key, value, ttlInSeconds);
25+
};
3326

34-
return store.get(key)
35-
.then(value => {
36-
if (!value) {
37-
debug("MISS", {key: key});
38-
return Promise.resolve(promise)
39-
.then(value => set(store, key, value, ttlInSeconds).then(() => value));
40-
} else {
41-
debug("HIT", {key: key});
42-
return Promise.resolve(value);
43-
}
44-
});
45-
},
27+
RedisCache.prototype.get = function (key) {
28+
if (!this.store) return;
4629

47-
set: (key, value, ttlInSeconds) => set(store, key, value, ttlInSeconds),
30+
return this.store.get(key);
31+
};
4832

49-
get: (key) => {
50-
if (!store) return;
33+
RedisCache.prototype.keys = function () {
34+
if (!this.store) return;
5135

52-
return store.get(key);
53-
},
36+
return this.store.keys();
37+
};
5438

55-
keys: () => {
56-
if (!store) return;
39+
RedisCache.prototype.deleteAll = function () {
40+
if (!this.store) return;
5741

58-
return store.keys();
59-
},
42+
return this.store.deleteAll();
43+
};
6044

61-
deleteAll: () => {
62-
if (!store) return;
45+
RedisCache.prototype.wrap = function (key, promise, ttlInSeconds) {
6346

64-
return store.deleteAll();
65-
}
66-
};
67-
}
47+
if (!this.store || ttlInSeconds === 0) {
48+
return Promise.resolve(promise);
49+
}
50+
51+
return this.store.get(key)
52+
.then(value => {
53+
if (!value) {
54+
debug("MISS", {key: key});
55+
return Promise.resolve(promise)
56+
.then(value => this.set(key, value, ttlInSeconds).then(() => value));
57+
} else {
58+
debug("HIT", {key: key});
59+
return Promise.resolve(value);
60+
}
61+
});
62+
};
6863

6964
module.exports = RedisCache;

lib/redis_store.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const RedisPool = require("./redis_pool");
2-
const bind = require("./util/bind");
32

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

@@ -21,22 +20,27 @@ function RedisStore (name, redisOptions, poolOptions) {
2120
if (!poolOptions) {
2221
poolOptions = {};
2322
}
23+
2424
poolOptions = Object.assign({
2525
acquireTimeoutMillis: 50
2626
}, poolOptions);
2727

2828
const pool = new RedisPool(name, redisOptions, poolOptions);
2929
debug("Redis store created.", pool.status());
3030

31-
this.getName = bind(pool.getName, pool);
32-
this.getRedisDB = bind(pool.getDB, pool);
33-
this.getPoolStatus = bind(pool.status, pool);
31+
this.getName = pool.getName.bind(pool);
32+
this.getRedisDB = pool.getDB.bind(pool);
33+
this.getPoolStatus = pool.status.bind(pool);
3434

3535
this.get = (key) => wrapFunc(pool, (conn) => conn.getAsync(key));
3636

37-
this.set = (key, value) => wrapFunc(pool, (conn) => conn.setAsync(key, value));
38-
39-
this.setex = (key, value, ttlInSeconds) => wrapFunc(pool, (conn) => conn.setexAsync(key, ttlInSeconds, value));
37+
this.set = (key, value, ttlInSeconds) => {
38+
if (ttlInSeconds) {
39+
return wrapFunc(pool, (conn) => conn.setexAsync(key, ttlInSeconds, value));
40+
} else {
41+
return wrapFunc(pool, (conn) => conn.setAsync(key, value));
42+
}
43+
};
4044

4145
this.del = (key) => wrapFunc(pool, (conn) => conn.delAsync(key));
4246

lib/util/bind.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/redis_cache.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const should = require("should");
22
const RedisCache = require("../lib/redis_cache");
33

4-
describe("cache", function () {
4+
describe("redisCache", function () {
55

66
const redisOptions = Object.assign({
77
host: process.env.REDIS_HOST || "127.0.0.1"
@@ -93,9 +93,7 @@ describe("cache", function () {
9393
.then(function (v) {
9494
v.should.be.equal(value);
9595
})
96-
.then(function () {
97-
return cache.get(key);
98-
})
96+
.then(() => cache.get(key))
9997
.then(function (v) {
10098
v.should.be.equal(value);
10199
done();
@@ -108,9 +106,7 @@ describe("cache", function () {
108106
.then(function (v) {
109107
v.should.be.ok();
110108
})
111-
.then(function () {
112-
return cache.wrap(key);
113-
})
109+
.then(() => cache.wrap(key))
114110
.then(function (v) {
115111
v.should.be.equal(value);
116112
done();
@@ -177,11 +173,12 @@ describe("cache", function () {
177173
.then(function (v) {
178174
v.should.be.ok();
179175
})
180-
.then(cache.keys)
181-
.then(function (keys) {
176+
.then(() => cache.keys())
177+
.then(keys => {
182178
keys.should.be.empty();
183179
done();
184-
});
180+
})
181+
.catch(e => console.log(e));
185182
});
186183
});
187184
});

test/redis_store.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ describe("redisStore", function () {
1616
const value = "superman";
1717

1818
store.set(key, value)
19-
.then(function (test) {
19+
.then(test => {
2020
test.should.be.ok();
21-
});
22-
23-
store.get(key)
21+
})
22+
.then(() => store.get(key))
2423
.then(function (v) {
2524
v.should.be.equal(value);
2625
done();
@@ -46,29 +45,25 @@ describe("redisStore", function () {
4645
store.set(key, value)
4746
.then(function (test) {
4847
test.should.be.ok();
49-
});
50-
51-
store.get(key)
48+
})
49+
.then(() => store.get(key))
5250
.then(function (v) {
5351
v.should.be.equal(value);
5452
done();
5553
});
5654
});
57-
});
5855

59-
describe("setex", function () {
60-
it("should store with an expiry", function (done) {
56+
it("should store with an expiry if ttl set", function (done) {
6157

6258
const key = "shortLivedKey";
63-
const value = "expireIn10ms";
59+
const value = "expireIn1s";
6460
const ttlInSeconds = 1;
6561

66-
store.setex(key, value, ttlInSeconds)
62+
store.set(key, value, ttlInSeconds)
6763
.then(function (test) {
6864
test.should.be.ok();
69-
});
70-
71-
store.get(key)
65+
})
66+
.then(() => store.get(key))
7267
.then(function (v) {
7368
v.should.be.equal(value);
7469
});
@@ -93,14 +88,12 @@ describe("redisStore", function () {
9388
store.set(key, value)
9489
.then(function (test) {
9590
test.should.be.ok();
96-
});
97-
98-
store.del(key)
91+
})
92+
.then(() => store.del(key))
9993
.then(function (v) {
10094
v.should.be.ok();
101-
});
102-
103-
store.get(key)
95+
})
96+
.then(() => store.get(key))
10497
.then(function (v) {
10598
should(v).be.null;
10699
done();
@@ -126,9 +119,8 @@ describe("redisStore", function () {
126119
store.set(key, value)
127120
.then(function (test) {
128121
test.should.be.ok();
129-
});
130-
131-
store.expire(key, ttlInSeconds)
122+
})
123+
.then(() => store.expire(key, ttlInSeconds))
132124
.then(function (v) {
133125
v.should.be.ok();
134126
});
@@ -159,12 +151,11 @@ describe("redisStore", function () {
159151
const value = "make it expire";
160152
const ttlInSeconds = 10;
161153

162-
store.setex(key, value, ttlInSeconds)
154+
store.set(key, value, ttlInSeconds)
163155
.then(function (test) {
164156
test.should.be.ok();
165-
});
166-
167-
store.ttlInSeconds(key)
157+
})
158+
.then(() => store.ttlInSeconds(key))
168159
.then(function (v) {
169160

170161
// it should be same as the time elapsed is very vvery small

0 commit comments

Comments
 (0)