@@ -31,28 +31,56 @@ public class RedisFeatureStore implements FeatureStore {
31
31
private static final String INIT_KEY = "$initialized$" ;
32
32
33
33
/**
34
+ * Creates a new store instance that connects to Redis with the provided host, port, prefix, and cache timeout. Uses a default
35
+ * connection pool configuration.
34
36
*
35
37
* @param host the host for the Redis connection
36
38
* @param port the port for the Redis connection
37
39
* @param prefix a namespace prefix for all keys stored in Redis
38
40
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
39
41
*/
40
42
public RedisFeatureStore (String host , int port , String prefix , long cacheTimeSecs ) {
41
- pool = new JedisPool (getPoolConfig (), host , port );
43
+ this (host , port , prefix , cacheTimeSecs , getPoolConfig ());
44
+ }
45
+
46
+ /**
47
+ * Creates a new store instance that connects to Redis with the provided URI, prefix, and cache timeout. Uses a default
48
+ * connection pool configuration.
49
+ *
50
+ * @param uri the URI for the Redis connection
51
+ * @param prefix a namespace prefix for all keys stored in Redis
52
+ * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
53
+ */
54
+ public RedisFeatureStore (URI uri , String prefix , long cacheTimeSecs ) {
55
+ this (uri , prefix , cacheTimeSecs , getPoolConfig ());
56
+ }
57
+
58
+ /**
59
+ * Creates a new store instance that connects to Redis with the provided URI, prefix, cache timeout, and connection pool settings.
60
+ *
61
+ * @param host the host for the Redis connection
62
+ * @param port the port for the Redis connection
63
+ * @param prefix a namespace prefix for all keys stored in Redis
64
+ * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
65
+ * @param poolConfig an optional pool config for the Jedis connection pool
66
+ */
67
+ public RedisFeatureStore (String host , int port , String prefix , long cacheTimeSecs , JedisPoolConfig poolConfig ) {
68
+ pool = new JedisPool (poolConfig , host , port );
42
69
setPrefix (prefix );
43
70
createCache (cacheTimeSecs );
44
71
createInitCache (cacheTimeSecs );
45
72
}
46
73
47
74
/**
48
- * Creates a new store instance that connects to Redis with the provided URI, prefix, and cache timeout.
75
+ * Creates a new store instance that connects to Redis with the provided URI, prefix, cache timeout, and connection pool settings .
49
76
*
50
77
* @param uri the URI for the Redis connection
51
78
* @param prefix a namespace prefix for all keys stored in Redis
52
79
* @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
80
+ * @param poolConfig an optional pool config for the Jedis connection pool
53
81
*/
54
- public RedisFeatureStore (URI uri , String prefix , long cacheTimeSecs ) {
55
- pool = new JedisPool (getPoolConfig () , uri );
82
+ public RedisFeatureStore (URI uri , String prefix , long cacheTimeSecs , JedisPoolConfig poolConfig ) {
83
+ pool = new JedisPool (poolConfig , uri );
56
84
setPrefix (prefix );
57
85
createCache (cacheTimeSecs );
58
86
createInitCache (cacheTimeSecs );
@@ -130,10 +158,8 @@ public FeatureRep<?> get(String key) {
130
158
*/
131
159
@ Override
132
160
public Map <String , FeatureRep <?>> all () {
133
- Jedis jedis = null ;
134
- try {
135
- jedis = getJedis ();
136
- Map <String ,String > featuresJson = getJedis ().hgetAll (featuresKey ());
161
+ try (Jedis jedis = pool .getResource ()) {
162
+ Map <String ,String > featuresJson = jedis .hgetAll (featuresKey ());
137
163
Map <String , FeatureRep <?>> result = new HashMap <String , FeatureRep <?>>();
138
164
Gson gson = new Gson ();
139
165
@@ -144,10 +170,6 @@ public Map<String, FeatureRep<?>> all() {
144
170
result .put (entry .getKey (), rep );
145
171
}
146
172
return result ;
147
- } finally {
148
- if (jedis != null ) {
149
- jedis .close ();
150
- }
151
173
}
152
174
153
175
}
@@ -159,10 +181,7 @@ public Map<String, FeatureRep<?>> all() {
159
181
*/
160
182
@ Override
161
183
public void init (Map <String , FeatureRep <?>> features ) {
162
- Jedis jedis = null ;
163
-
164
- try {
165
- jedis = getJedis ();
184
+ try (Jedis jedis = pool .getResource ()) {
166
185
Gson gson = new Gson ();
167
186
Transaction t = jedis .multi ();
168
187
@@ -173,10 +192,6 @@ public void init(Map<String, FeatureRep<?>> features) {
173
192
}
174
193
175
194
t .exec ();
176
- } finally {
177
- if (jedis != null ) {
178
- jedis .close ();
179
- }
180
195
}
181
196
}
182
197
@@ -191,9 +206,7 @@ public void init(Map<String, FeatureRep<?>> features) {
191
206
*/
192
207
@ Override
193
208
public void delete (String key , int version ) {
194
- Jedis jedis = null ;
195
- try {
196
- jedis = getJedis ();
209
+ try (Jedis jedis = pool .getResource ()) {
197
210
Gson gson = new Gson ();
198
211
jedis .watch (featuresKey ());
199
212
@@ -212,13 +225,6 @@ public void delete(String key, int version) {
212
225
cache .invalidate (key );
213
226
}
214
227
}
215
- finally {
216
- if (jedis != null ) {
217
- jedis .unwatch ();
218
- jedis .close ();
219
- }
220
- }
221
-
222
228
}
223
229
224
230
/**
@@ -230,9 +236,7 @@ public void delete(String key, int version) {
230
236
*/
231
237
@ Override
232
238
public void upsert (String key , FeatureRep <?> feature ) {
233
- Jedis jedis = null ;
234
- try {
235
- jedis = getJedis ();
239
+ try (Jedis jedis = pool .getResource ()) {
236
240
Gson gson = new Gson ();
237
241
jedis .watch (featuresKey ());
238
242
@@ -248,12 +252,6 @@ public void upsert(String key, FeatureRep<?> feature) {
248
252
cache .invalidate (key );
249
253
}
250
254
}
251
- finally {
252
- if (jedis != null ) {
253
- jedis .unwatch ();
254
- jedis .close ();
255
- }
256
- }
257
255
}
258
256
259
257
/**
@@ -290,24 +288,15 @@ private String featuresKey() {
290
288
}
291
289
292
290
private Boolean getInit () {
293
- Jedis jedis = null ;
294
-
295
- try {
296
- jedis = getJedis ();
291
+ try (Jedis jedis = pool .getResource ()) {
297
292
return jedis .exists (featuresKey ());
298
- } finally {
299
- if (jedis != null ) {
300
- jedis .close ();
301
- }
302
293
}
303
294
}
304
295
305
296
private FeatureRep <?> getRedis (String key ) {
306
- Jedis jedis = null ;
307
- try {
308
- jedis = getJedis ();
297
+ try (Jedis jedis = pool .getResource ()){
309
298
Gson gson = new Gson ();
310
- String featureJson = getJedis () .hget (featuresKey (), key );
299
+ String featureJson = jedis .hget (featuresKey (), key );
311
300
312
301
if (featureJson == null ) {
313
302
return null ;
@@ -317,21 +306,12 @@ private FeatureRep<?> getRedis(String key) {
317
306
FeatureRep <?> f = gson .fromJson (featureJson , type );
318
307
319
308
return f .deleted ? null : f ;
320
- } finally {
321
- if (jedis != null ) {
322
- jedis .close ();
323
- }
324
309
}
325
310
}
326
311
327
- private final Jedis getJedis () {
328
- return pool .getResource ();
329
- }
330
-
331
- private final JedisPoolConfig getPoolConfig () {
312
+ private static final JedisPoolConfig getPoolConfig () {
332
313
JedisPoolConfig config = new JedisPoolConfig ();
333
- config .setMaxTotal (256 );
334
- config .setBlockWhenExhausted (false );
335
314
return config ;
336
315
}
316
+
337
317
}
0 commit comments