Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit 9402591

Browse files
committed
Merge pull request #35 from launchdarkly/jko/init-cache
Add caching to the Redis initialization check
2 parents 9b8587b + bc95d63 commit 9402591

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/main/java/com/launchdarkly/client/RedisFeatureStore.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ public class RedisFeatureStore implements FeatureStore {
2424
private static final String DEFAULT_PREFIX = "launchdarkly";
2525
private final Jedis jedis;
2626
private LoadingCache<String, FeatureRep<?>> cache;
27+
private LoadingCache<String, Boolean> initCache;
2728
private String prefix;
29+
private static final String INIT_KEY = "$initialized$";
2830

2931
/**
3032
*
@@ -37,6 +39,7 @@ public RedisFeatureStore(String host, int port, String prefix, long cacheTimeSec
3739
jedis = new Jedis(host, port);
3840
setPrefix(prefix);
3941
createCache(cacheTimeSecs);
42+
createInitCache(cacheTimeSecs);
4043
}
4144

4245
/**
@@ -50,6 +53,7 @@ public RedisFeatureStore(URI uri, String prefix, long cacheTimeSecs) {
5053
jedis = new Jedis(uri);
5154
setPrefix(prefix);
5255
createCache(cacheTimeSecs);
56+
createInitCache(cacheTimeSecs);
5357
}
5458

5559
/**
@@ -82,6 +86,18 @@ public FeatureRep<?> load(String key) throws Exception {
8286
}
8387
}
8488

89+
private void createInitCache(long cacheTimeSecs) {
90+
if (cacheTimeSecs > 0) {
91+
initCache = CacheBuilder.newBuilder().expireAfterWrite(cacheTimeSecs, TimeUnit.SECONDS).build(new CacheLoader<String, Boolean>() {
92+
93+
@Override
94+
public Boolean load(String key) throws Exception {
95+
return getInit();
96+
}
97+
});
98+
}
99+
}
100+
85101
/**
86102
*
87103
* Returns the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
@@ -218,14 +234,26 @@ public void upsert(String key, FeatureRep<?> feature) {
218234
*/
219235
@Override
220236
public boolean initialized() {
221-
return jedis.exists(featuresKey());
237+
if (initCache != null) {
238+
Boolean initialized = initCache.getUnchecked(INIT_KEY);
239+
240+
if (initialized != null && initialized.booleanValue()) {
241+
return true;
242+
}
243+
}
244+
245+
return getInit();
222246
}
223247

224248

225249
private String featuresKey() {
226250
return prefix + ":features";
227251
}
228252

253+
private Boolean getInit() {
254+
return jedis.exists(featuresKey());
255+
}
256+
229257
private FeatureRep<?> getRedis(String key) {
230258
Gson gson = new Gson();
231259
String featureJson = jedis.hget(featuresKey(), key);

0 commit comments

Comments
 (0)