-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
197 lines (166 loc) · 5.2 KB
/
index.js
File metadata and controls
197 lines (166 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
let redis = require('redis')
, bluebird = require('bluebird')
, _ = require('lodash')
, defaultDBIndex = 1
, defaultTTL = 60 * 60 // 1 hour
;
// pass in redis so prototype can be extended with LUA script to delete by pattern
require('redis-delete-wildcard')(redis);
// Async promises with bluebird
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
class Client{
/**
* [constructor description]
* @param {[object]} options [
* options object to instantiate the client with
* @param {[integer]} db [ redis db to select ]
* @param {[integer || false/null ]} ttl [ ttl value to expire after (in seconds). null, false, and 0 will be interpreted as no expiration/ttl ]
* ]
* @return {[Client]}
*/
constructor(options={}){
console.log('redis-api-cacher instantiated with options:', options);
this.client = redis.createClient(null, null, { detect_buffers: true });
// this.client = redis.createClient(null, null, { detect_buffers: true , 'return_buffers': true});
this._keys = [];
this.ttl = typeof options.ttl !== 'undefined' ? options.ttl : defaultTTL
this.setDB(options.db || defaultDBIndex)
}
setDB(db){
this._dbIndex = db
this.client.select(db, (err, msg)=>console.log(`\n\n cache_client is connected to ${this._dbIndex}. errors: ${err}. messages: ${msg} \n\n` ))
return this
}
get dbKeys(){
return new Promise((resolve, reject)=>{
this.client.keys('*', (err,keys)=>{
if(err)
return reject(err)
return resolve(keys)
})
})
}
get cachedKeys(){
return this._keys || []
}
set cachedKeys(keys){
this._keys
}
addCachedKey(key){
this.cachedKeys = this._keys.push(key)
return this
}
removeCachedKey(key){
_.pull(this.cachedKeys, key)
return this
}
set ttl(ttl){
this._ttl = ttl
}
get ttl(){
// set the default ttl property to be used with
// EXPIRE
// http://redis.io/commands/expire
// Set a key to expire after `n` seconds
return this._ttl
}
set(key, val, timeout){
// if(typeof key !== 'string')
// key = JSON.stringify(key)
this.client.set(key, val);
let ttl = typeof timeout !== 'undefined' ? timeout : this.ttl;
// only set key to expire if Client has a ttl value set
if(this.ttl)
this.expire(key, ttl);
// store the key
// for easy deletion later
this.addCachedKey(key);
// console.log(`the cache_client now has ${this.cachedKeys.length} keys`)
// this.numkeys.then(num=>console.log(`redis now has ${num} keys`))
return this
}
get numkeys(){
return new Promise((resolve, reject)=>{
this.client.keys('*', (err, keys)=> resolve(err ? 'error': keys.length ) )
})
}
/**
* [get description]
* @param {[string]} key [key string to search for]
* @return {[Promise]} [Promise resolved to found value]
*/
get(key){
return new Promise((resolve, reject)=>{
// if(typeof key !== 'string')
// key = JSON.stringify(key);
// console.log('cache_client searching for:', key)
this.client.getAsync(key)
.catch(err=>{
console.trace(err)
resolve()
})
.then( data=>{
// console.log(`data found with key: ${key} - ${data}`);
// console.log('typeof found data is:', typeof data);
// console.log('data is an Array:', data instanceof Array);
// parse the data back to JSON if it's a json object
// could either be the result of:
// JSON.stringify(Array)
// or
// JSON.stringify(Object)
try{
data = typeof data == 'string'
? (/^[\[|\{](\s|.*|\w)*[\]|\}]$/.test(data))
? JSON.parse(data)
: data
: data;
// data = typeof data == 'string' ? (/^\{.+\}$/.test(data) || /^\[.+\]$/.test(data)) ? JSON.parse(data) : data : data;
}catch(err){
console.log(`error trying to parse cached string back to JSON for key: ${key}, data: ${data}`, err)
}
// console.log('resolved', key)
resolve(data)
})
})
}
expire(key, ttl){
this.client.expire(key, ttl)
return this
}
flushKeys(keys){
if(keys instanceof Array){
for(let key of keys){
this.removeCachedKey(key)
.client.del(key)
}
}
return this
}
flushCachedKeys(){
return this.flushKeys(this.cachedKeys)
}
flushAllDBKeys(){
this.client.keys('*', (err,keys)=>this.flushKeys(keys))
return this
}
flushKeysLike(pattern){
// https://www.npmjs.com/package/redis-delete-wildcard
// this.client.delwild('pattern:*', function(error, numberDeletedKeys) {
return new Promise((resolve, reject)=>{
this.client.delwild(pattern, function(err, numberDeletedKeys) {
if(err)
return reject(err)
return resolve(numberDeletedKeys)
})
})
}
buildCache(){
console.log('cache_client building cache');
// TODO - create this method
console.log('cache_client - buildCache not active yet');s
}
}
// module.exports = options => new Client( options )
module.exports = Client;