@@ -31,28 +31,56 @@ public class RedisFeatureStore implements FeatureStore {
3131 private static final String INIT_KEY = "$initialized$" ;
3232
3333 /**
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.
3436 *
3537 * @param host the host for the Redis connection
3638 * @param port the port for the Redis connection
3739 * @param prefix a namespace prefix for all keys stored in Redis
3840 * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
3941 */
4042 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 );
4269 setPrefix (prefix );
4370 createCache (cacheTimeSecs );
4471 createInitCache (cacheTimeSecs );
4572 }
4673
4774 /**
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 .
4976 *
5077 * @param uri the URI for the Redis connection
5178 * @param prefix a namespace prefix for all keys stored in Redis
5279 * @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
5381 */
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 );
5684 setPrefix (prefix );
5785 createCache (cacheTimeSecs );
5886 createInitCache (cacheTimeSecs );
@@ -130,10 +158,8 @@ public FeatureRep<?> get(String key) {
130158 */
131159 @ Override
132160 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 ());
137163 Map <String , FeatureRep <?>> result = new HashMap <String , FeatureRep <?>>();
138164 Gson gson = new Gson ();
139165
@@ -144,10 +170,6 @@ public Map<String, FeatureRep<?>> all() {
144170 result .put (entry .getKey (), rep );
145171 }
146172 return result ;
147- } finally {
148- if (jedis != null ) {
149- jedis .close ();
150- }
151173 }
152174
153175 }
@@ -159,10 +181,7 @@ public Map<String, FeatureRep<?>> all() {
159181 */
160182 @ Override
161183 public void init (Map <String , FeatureRep <?>> features ) {
162- Jedis jedis = null ;
163-
164- try {
165- jedis = getJedis ();
184+ try (Jedis jedis = pool .getResource ()) {
166185 Gson gson = new Gson ();
167186 Transaction t = jedis .multi ();
168187
@@ -173,10 +192,6 @@ public void init(Map<String, FeatureRep<?>> features) {
173192 }
174193
175194 t .exec ();
176- } finally {
177- if (jedis != null ) {
178- jedis .close ();
179- }
180195 }
181196 }
182197
@@ -191,9 +206,7 @@ public void init(Map<String, FeatureRep<?>> features) {
191206 */
192207 @ Override
193208 public void delete (String key , int version ) {
194- Jedis jedis = null ;
195- try {
196- jedis = getJedis ();
209+ try (Jedis jedis = pool .getResource ()) {
197210 Gson gson = new Gson ();
198211 jedis .watch (featuresKey ());
199212
@@ -212,13 +225,6 @@ public void delete(String key, int version) {
212225 cache .invalidate (key );
213226 }
214227 }
215- finally {
216- if (jedis != null ) {
217- jedis .unwatch ();
218- jedis .close ();
219- }
220- }
221-
222228 }
223229
224230 /**
@@ -230,9 +236,7 @@ public void delete(String key, int version) {
230236 */
231237 @ Override
232238 public void upsert (String key , FeatureRep <?> feature ) {
233- Jedis jedis = null ;
234- try {
235- jedis = getJedis ();
239+ try (Jedis jedis = pool .getResource ()) {
236240 Gson gson = new Gson ();
237241 jedis .watch (featuresKey ());
238242
@@ -248,12 +252,6 @@ public void upsert(String key, FeatureRep<?> feature) {
248252 cache .invalidate (key );
249253 }
250254 }
251- finally {
252- if (jedis != null ) {
253- jedis .unwatch ();
254- jedis .close ();
255- }
256- }
257255 }
258256
259257 /**
@@ -290,24 +288,15 @@ private String featuresKey() {
290288 }
291289
292290 private Boolean getInit () {
293- Jedis jedis = null ;
294-
295- try {
296- jedis = getJedis ();
291+ try (Jedis jedis = pool .getResource ()) {
297292 return jedis .exists (featuresKey ());
298- } finally {
299- if (jedis != null ) {
300- jedis .close ();
301- }
302293 }
303294 }
304295
305296 private FeatureRep <?> getRedis (String key ) {
306- Jedis jedis = null ;
307- try {
308- jedis = getJedis ();
297+ try (Jedis jedis = pool .getResource ()){
309298 Gson gson = new Gson ();
310- String featureJson = getJedis () .hget (featuresKey (), key );
299+ String featureJson = jedis .hget (featuresKey (), key );
311300
312301 if (featureJson == null ) {
313302 return null ;
@@ -317,21 +306,12 @@ private FeatureRep<?> getRedis(String key) {
317306 FeatureRep <?> f = gson .fromJson (featureJson , type );
318307
319308 return f .deleted ? null : f ;
320- } finally {
321- if (jedis != null ) {
322- jedis .close ();
323- }
324309 }
325310 }
326311
327- private final Jedis getJedis () {
328- return pool .getResource ();
329- }
330-
331- private final JedisPoolConfig getPoolConfig () {
312+ private static final JedisPoolConfig getPoolConfig () {
332313 JedisPoolConfig config = new JedisPoolConfig ();
333- config .setMaxTotal (256 );
334- config .setBlockWhenExhausted (false );
335314 return config ;
336315 }
316+
337317}
0 commit comments