1515import java .util .Map ;
1616import java .util .concurrent .TimeUnit ;
1717
18+ /**
19+ * A thread-safe, versioned store for {@link com.launchdarkly.client.FeatureRep} objects backed by Redis. Also
20+ * supports an optional in-memory cache configuration that can be used to improve performance.
21+ *
22+ */
1823public class RedisFeatureStore implements FeatureStore {
1924 private static final String DEFAULT_PREFIX = "launchdarkly" ;
2025 private final Jedis jedis ;
2126 private LoadingCache <String , FeatureRep <?>> cache ;
2227 private String prefix ;
2328
29+ /**
30+ *
31+ * @param host the host for the Redis connection
32+ * @param port the port for the Redis connection
33+ * @param prefix a namespace prefix for all keys stored in Redis
34+ * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
35+ */
2436 public RedisFeatureStore (String host , int port , String prefix , long cacheTimeSecs ) {
2537 jedis = new Jedis (host , port );
2638 setPrefix (prefix );
2739 createCache (cacheTimeSecs );
2840 }
2941
42+ /**
43+ * Creates a new store instance that connects to Redis with the provided URI, prefix, and cache timeout.
44+ *
45+ * @param uri the URI for the Redis connection
46+ * @param prefix a namespace prefix for all keys stored in Redis
47+ * @param cacheTimeSecs an optional timeout for the in-memory cache. If set to 0, no in-memory caching will be performed
48+ */
3049 public RedisFeatureStore (URI uri , String prefix , long cacheTimeSecs ) {
3150 jedis = new Jedis (uri );
3251 setPrefix (prefix );
3352 createCache (cacheTimeSecs );
3453 }
3554
55+ /**
56+ * Creates a new store instance that connects to Redis with a default connection (localhost port 6379) and no in-memory cache.
57+ *
58+ */
3659 public RedisFeatureStore () {
3760 jedis = new Jedis ("localhost" );
3861 this .prefix = DEFAULT_PREFIX ;
@@ -59,7 +82,17 @@ public FeatureRep<?> load(String key) throws Exception {
5982 }
6083 }
6184
62-
85+ /**
86+ *
87+ * Returns the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
88+ * null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
89+ * been deleted.
90+ *
91+ * @param key the key whose associated {@link com.launchdarkly.client.FeatureRep} is to be returned
92+ * @return the {@link com.launchdarkly.client.FeatureRep} to which the specified key is mapped, or
93+ * null if the key is not associated or the associated {@link com.launchdarkly.client.FeatureRep} has
94+ * been deleted.
95+ */
6396 @ Override
6497 public FeatureRep <?> get (String key ) {
6598 if (cache != null ) {
@@ -69,6 +102,14 @@ public FeatureRep<?> get(String key) {
69102 }
70103 }
71104
105+
106+ /**
107+ * Returns a {@link java.util.Map} of all associated features. This implementation does not take advantage
108+ * of the in-memory cache, so fetching all features will involve a fetch from Redis.
109+ *
110+ *
111+ * @return a map of all associated features.
112+ */
72113 @ Override
73114 public Map <String , FeatureRep <?>> all () {
74115 Map <String ,String > featuresJson = jedis .hgetAll (featuresKey ());
@@ -84,7 +125,12 @@ public Map<String, FeatureRep<?>> all() {
84125
85126 return result ;
86127 }
87-
128+ /**
129+ * Initializes (or re-initializes) the store with the specified set of features. Any existing entries
130+ * will be removed.
131+ *
132+ * @param features the features to set the store
133+ */
88134 @ Override
89135 public void init (Map <String , FeatureRep <?>> features ) {
90136 Gson gson = new Gson ();
@@ -99,6 +145,15 @@ public void init(Map<String, FeatureRep<?>> features) {
99145 t .exec ();
100146 }
101147
148+
149+ /**
150+ *
151+ * Deletes the feature associated with the specified key, if it exists and its version
152+ * is less than or equal to the specified version.
153+ *
154+ * @param key the key of the feature to be deleted
155+ * @param version the version for the delete operation
156+ */
102157 @ Override
103158 public void delete (String key , int version ) {
104159 try {
@@ -126,6 +181,13 @@ public void delete(String key, int version) {
126181
127182 }
128183
184+ /**
185+ * Update or insert the feature associated with the specified key, if its version
186+ * is less than or equal to the version specified in the argument feature.
187+ *
188+ * @param key
189+ * @param feature
190+ */
129191 @ Override
130192 public void upsert (String key , FeatureRep <?> feature ) {
131193 try {
@@ -149,6 +211,11 @@ public void upsert(String key, FeatureRep<?> feature) {
149211 }
150212 }
151213
214+ /**
215+ * Returns true if this store has been initialized
216+ *
217+ * @return true if this store has been initialized
218+ */
152219 @ Override
153220 public boolean initialized () {
154221 return jedis .exists (featuresKey ());
0 commit comments