77import com .google .gson .reflect .TypeToken ;
88import org .apache .http .util .EntityUtils ;
99import redis .clients .jedis .Jedis ;
10+ import redis .clients .jedis .JedisPool ;
11+ import redis .clients .jedis .JedisPoolConfig ;
1012import redis .clients .jedis .Transaction ;
1113
14+ import java .io .IOException ;
1215import java .lang .reflect .Type ;
1316import java .net .URI ;
1417import java .util .HashMap ;
2225 */
2326public class RedisFeatureStore implements FeatureStore {
2427 private static final String DEFAULT_PREFIX = "launchdarkly" ;
25- private final Jedis jedis ;
28+ private final JedisPool pool ;
2629 private LoadingCache <String , FeatureRep <?>> cache ;
2730 private LoadingCache <String , Boolean > initCache ;
2831 private String prefix ;
@@ -36,7 +39,7 @@ public class RedisFeatureStore implements FeatureStore {
3639 * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
3740 */
3841 public RedisFeatureStore (String host , int port , String prefix , long cacheTimeSecs ) {
39- jedis = new Jedis ( host , port );
42+ pool = new JedisPool ( new JedisPoolConfig (), host , port );
4043 setPrefix (prefix );
4144 createCache (cacheTimeSecs );
4245 createInitCache (cacheTimeSecs );
@@ -50,7 +53,7 @@ public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSec
5053 * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
5154 */
5255 public RedisFeatureStore (URI uri , String prefix , long cacheTimeSecs ) {
53- jedis = new Jedis ( uri );
56+ pool = new JedisPool ( new JedisPoolConfig (), uri );
5457 setPrefix (prefix );
5558 createCache (cacheTimeSecs );
5659 createInitCache (cacheTimeSecs );
@@ -61,7 +64,7 @@ public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) {
6164 *
6265 */
6366 public RedisFeatureStore () {
64- jedis = new Jedis ( "localhost" );
67+ pool = new JedisPool ( new JedisPoolConfig (), "localhost" );
6568 this .prefix = DEFAULT_PREFIX ;
6669 }
6770
@@ -128,7 +131,7 @@ public FeatureRep<?> get(String key) {
128131 */
129132 @ Override
130133 public Map <String , FeatureRep <?>> all () {
131- Map <String ,String > featuresJson = jedis .hgetAll (featuresKey ());
134+ Map <String ,String > featuresJson = jedis () .hgetAll (featuresKey ());
132135 Map <String , FeatureRep <?>> result = new HashMap <String , FeatureRep <?>>();
133136 Gson gson = new Gson ();
134137
@@ -149,6 +152,7 @@ public Map<String, FeatureRep<?>> all() {
149152 */
150153 @ Override
151154 public void init (Map <String , FeatureRep <?>> features ) {
155+ Jedis jedis = jedis ();
152156 Gson gson = new Gson ();
153157 Transaction t = jedis .multi ();
154158
@@ -172,6 +176,7 @@ public void init(Map<String, FeatureRep<?>> features) {
172176 */
173177 @ Override
174178 public void delete (String key , int version ) {
179+ Jedis jedis = jedis ();
175180 try {
176181 Gson gson = new Gson ();
177182 jedis .watch (featuresKey ());
@@ -206,6 +211,7 @@ public void delete(String key, int version) {
206211 */
207212 @ Override
208213 public void upsert (String key , FeatureRep <?> feature ) {
214+ Jedis jedis = jedis ();
209215 try {
210216 Gson gson = new Gson ();
211217 jedis .watch (featuresKey ());
@@ -245,18 +251,28 @@ public boolean initialized() {
245251 return getInit ();
246252 }
247253
254+ /**
255+ * Releases all resources associated with the store. The store must no longer be used once closed.
256+ * @throws IOException
257+ */
258+ public void close () throws IOException
259+ {
260+ pool .destroy ();
261+ }
262+
263+
248264
249265 private String featuresKey () {
250266 return prefix + ":features" ;
251267 }
252268
253269 private Boolean getInit () {
254- return jedis .exists (featuresKey ());
270+ return jedis () .exists (featuresKey ());
255271 }
256272
257273 private FeatureRep <?> getRedis (String key ) {
258274 Gson gson = new Gson ();
259- String featureJson = jedis .hget (featuresKey (), key );
275+ String featureJson = jedis () .hget (featuresKey (), key );
260276
261277 if (featureJson == null ) {
262278 return null ;
@@ -267,4 +283,8 @@ private FeatureRep<?> getRedis(String key) {
267283
268284 return f .deleted ? null : f ;
269285 }
286+
287+ private final Jedis jedis () {
288+ return pool .getResource ();
289+ }
270290}
0 commit comments