Skip to content

Commit 278822e

Browse files
committed
Initial commit
0 parents  commit 278822e

File tree

9 files changed

+1775
-0
lines changed

9 files changed

+1775
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.idea/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Matthijs Dabroek
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Whitespace-only changes.

dist/index.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict';
2+
3+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4+
5+
var Redis = _interopDefault(require('redis'));
6+
7+
const redisStore = (...args) => {
8+
const redisCache = Redis.createClient(...args);
9+
const storeArgs = redisCache.options;
10+
11+
return {
12+
name: 'redis',
13+
getClient: () => redisCache,
14+
set: (key, value, options, cb) => (
15+
new Promise((resolve, reject) => {
16+
if (typeof options === 'function') {
17+
cb = options;
18+
options = {};
19+
}
20+
options = options || {};
21+
22+
if (!cb) {
23+
cb = (err, result) => (err ? reject(err) : resolve(result));
24+
}
25+
26+
const ttl = (options.ttl || options.ttl === 0) ? options.ttl : storeArgs.ttl;
27+
const val = JSON.stringify(value) || '"undefined"';
28+
29+
if (ttl) {
30+
redisCache.setex(key, ttl, val, handleResponse(cb));
31+
} else {
32+
redisCache.set(key, val, handleResponse(cb));
33+
}
34+
})
35+
),
36+
get: (key, options, cb) => (
37+
new Promise((resolve, reject) => {
38+
if (typeof options === 'function') {
39+
cb = options;
40+
}
41+
42+
if (!cb) {
43+
cb = (err, result) => (err ? reject(err) : resolve(result));
44+
}
45+
46+
redisCache.get(key, handleResponse(cb, { parse: true }));
47+
})
48+
),
49+
del: (key, options, cb) => {
50+
if (typeof options === 'function') {
51+
cb = options;
52+
}
53+
54+
redisCache.del(key, handleResponse(cb));
55+
},
56+
reset: cb => redisCache.flushdb(handleResponse(cb)),
57+
keys: cb => redisCache.keys(handleResponse(cb)),
58+
ttl: (key, cb) => redisCache.ttl(key, handleResponse(cb)),
59+
isCacheableValue: args.isCacheableValue || (value => value !== undefined && value !== null),
60+
};
61+
};
62+
63+
function handleResponse(cb, opts = {}) {
64+
return (err, result) => {
65+
if (err) {
66+
return cb && cb(err);
67+
}
68+
69+
if (opts.parse) {
70+
try {
71+
result = JSON.parse(result);
72+
} catch (e) {
73+
return cb && cb(e);
74+
}
75+
}
76+
77+
return cb && cb(null, result);
78+
};
79+
}
80+
81+
const methods = {
82+
create: (...args) => redisStore(...args),
83+
};
84+
85+
module.exports = methods;

index.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import Redis from 'redis';
2+
3+
const redisStore = (...args) => {
4+
const redisCache = Redis.createClient(...args);
5+
const storeArgs = redisCache.options;
6+
7+
return {
8+
name: 'redis',
9+
getClient: () => redisCache,
10+
set: (key, value, options, cb) => (
11+
new Promise((resolve, reject) => {
12+
if (typeof options === 'function') {
13+
cb = options;
14+
options = {};
15+
}
16+
options = options || {};
17+
18+
if (!cb) {
19+
cb = (err, result) => (err ? reject(err) : resolve(result));
20+
}
21+
22+
const ttl = (options.ttl || options.ttl === 0) ? options.ttl : storeArgs.ttl;
23+
const val = JSON.stringify(value) || '"undefined"';
24+
25+
if (ttl) {
26+
redisCache.setex(key, ttl, val, handleResponse(cb));
27+
} else {
28+
redisCache.set(key, val, handleResponse(cb));
29+
}
30+
})
31+
),
32+
get: (key, options, cb) => (
33+
new Promise((resolve, reject) => {
34+
if (typeof options === 'function') {
35+
cb = options;
36+
}
37+
38+
if (!cb) {
39+
cb = (err, result) => (err ? reject(err) : resolve(result));
40+
}
41+
42+
redisCache.get(key, handleResponse(cb, { parse: true }));
43+
})
44+
),
45+
del: (key, options, cb) => {
46+
if (typeof options === 'function') {
47+
cb = options;
48+
}
49+
50+
redisCache.del(key, handleResponse(cb));
51+
},
52+
reset: cb => redisCache.flushdb(handleResponse(cb)),
53+
keys: cb => redisCache.keys(handleResponse(cb)),
54+
ttl: (key, cb) => redisCache.ttl(key, handleResponse(cb)),
55+
isCacheableValue: args.isCacheableValue || (value => value !== undefined && value !== null),
56+
};
57+
};
58+
59+
function handleResponse(cb, opts = {}) {
60+
return (err, result) => {
61+
if (err) {
62+
return cb && cb(err);
63+
}
64+
65+
if (opts.parse) {
66+
try {
67+
result = JSON.parse(result);
68+
} catch (e) {
69+
return cb && cb(e);
70+
}
71+
}
72+
73+
return cb && cb(null, result);
74+
};
75+
}
76+
77+
const methods = {
78+
create: (...args) => redisStore(...args),
79+
};
80+
81+
export default methods;

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "cache-manager-redis-store",
3+
"author": "Matthijs Dabroek <[email protected]>",
4+
"description": "Redis store for node-cache-manager module",
5+
"version": "1.0.0",
6+
"license": "MIT",
7+
"main": "dist/index.js",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/dabroek/node-cache-manager-redis-store.git"
11+
},
12+
"scripts": {
13+
"test": "node test.js",
14+
"build": "rollup -c"
15+
},
16+
"dependencies": {
17+
"redis": "^2.7.1"
18+
},
19+
"devDependencies": {
20+
"rollup": "^0.41.6"
21+
}
22+
}

rollup.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
entry: 'index.js',
3+
format: 'cjs',
4+
dest: 'dist/index.js',
5+
};

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var redisStore = require('./dist/index');
2+
3+
var store = redisStore.create({
4+
host: '127.0.0.1',
5+
port: 6379,
6+
ttl: 712,
7+
});
8+
9+
console.log(store.getClient().options);
10+
11+
store.set('lala', 'baba').then(() => store.get('lala').then((output) => {
12+
console.log(output);
13+
process.exit();
14+
}));

0 commit comments

Comments
 (0)