forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_feature_store.js
175 lines (152 loc) · 4.16 KB
/
redis_feature_store.js
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
var redis = require('redis'),
NodeCache = require( "node-cache" );
function RedisFeatureStore(redis_opts, cache_ttl) {
var client = redis.createClient(redis_opts),
store = {},
features_key = ":features",
cache = new NodeCache({ stdTTL: cache_ttl});
// Allow driver programs to exit, even if the Redis
// socket is active
client.unref();
// A helper that performs a get with either the redis client
// itself, or a multi object from a redis transaction
function do_get(mclient, key, cb) {
var flag = cache.get(key);
cb = cb || noop;
if (flag) {
if (flag.deleted) {
cb(null);
} else {
cb(flag);
}
} else {
mclient.hget(features_key, key, function(err, obj) {
if (err) {
config.logger.error("[LaunchDarkly] Error fetching flag from redis", err)
cb(null);
} else {
flag = JSON.parse(obj);
cb(flag.deleted ? null : flag);
}
});
}
}
store.get = function(key, cb) {
do_get(client, key, cb);
}
store.all = function(cb) {
cb = cb || noop;
client.hgetall(features_key, function(err, obj) {
if (err) {
config.logger.error("[LaunchDarkly] Error fetching flag from redis", err)
cb(null);
} else {
var results = {},
flags = obj;
for (var key in flags) {
if (Object.hasOwnProperty.call(flags,key)) {
var flag = JSON.parse(flags[key]);
if (!flag.deleted) {
results[key] = flag;
}
}
}
cb(results);
}
});
}
store.init = function(flags, cb) {
var stringified = {};
var multi = client.multi();
cb = cb || noop;
multi.del(features_key);
cache.flushAll();
for (var key in flags) {
if (Object.hasOwnProperty.call(flags,key)) {
stringified[key] = JSON.stringify(flags[key]);
}
if (cache_ttl) {
cache.set(key, flags[key]);
}
}
multi.hmset(features_key, stringified);
multi.exec(function(err, replies) {
if (err) {
config.logger.error("[LaunchDarkly] Error initializing redis feature store", err);
}
cb();
});
}
store.delete = function(key, version, cb) {
var multi;
cb = cb || noop;
client.watch(features_key);
multi = client.multi();
// We need to run the get() code in a multi txn
do_get(multi, key, function(flag) {
if (flag) {
if (flag.version >= version) {
cb();
return;
} else {
flag.deleted = true;
flag.version = version;
multi.hset(features_key, key, JSON.stringify(flag));
multi.exec(function(err, replies) {
if (err) {
config.logger.error("[LaunchDarkly] Error deleting feature flag", err);
} else if (cache_ttl) {
cache.set(key, flag);
}
cb();
})
}
}
});
}
store.upsert = function(key, flag, cb) {
var multi;
cb = cb || noop;
client.watch(features_key);
multi = client.multi();
do_get(multi, key, function(original) {
if (original) {
if (original.version >= version) {
cb();
return;
} else {
multi.hset(features_key, key, JSON.stringify(flag));
multi.exec(function(err, replies) {
if (err) {
config.logger.error("[LaunchDarkly] Error upserting feature flag", err);
} else {
if (cache_ttl) {
cache.put(key, flag);
}
}
cb();
})
}
}
})
}
store.initialized = function(cb) {
var init = cache.get('$initialized$');
cb = cb || noop;
if (init) {
return true;
}
client.exists('$initialized$', function(err, obj) {
if (!err && obj) {
cache.set('$initialized$', true);
}
cb(!err && obj);
});
}
store.close = function() {
client.quit();
cache.close();
}
return store;
}
module.exports = RedisFeatureStore