/** * Remove all keys associated with the store. * <p> * This is mainly used for tests; the Redis command used to get the associated keys (KEYS) is rather expensive and * shouldn't be run frequently. */ public void removeAllKeys() { removeAllKeys(storeName); }
/** * Factory for singleton instances by name. * <p> * Only a single instance can exist for each name. * * @param storeName Name for the singleton instance * * @return The singleton instance for the given name */ public static synchronized RedisStore getInstance(String storeName) { RedisStore redisStore = REDIS_STORES.get(storeName); if (redisStore == null) { redisStore = new RedisStore(storeName, POOL, REDIS_NAMESPACE); REDIS_STORES.put(storeName, redisStore); } return redisStore; }
/** * Build a connection to a Redis provider. * * @param storeName The second part of the prefix to keyspace names for this store * @param pool A pool of Jedis connection instances * @param redisNamespace The first part of the prefix to the keyspace names for this store */ public RedisStore(String storeName, JedisPool pool, String redisNamespace) { this.pool = pool; this.storeName = storeName; this.redisIsHealthy = true; this.redisNamespace = redisNamespace; open(); }
@Override public String get(@NotNull String key) { if (key == null) { throw new IllegalArgumentException("Cannot get null key"); } String rKey = redisKey(storeName, key); try (Jedis jedis = pool.getResource()) { return jedis.get(rKey); } catch (JedisException e) { redisIsHealthy = false; String msg = String.format("Unable to get key %s (Redis key %s)", key, rKey); LOG.error(msg); throw new RuntimeException(e); } }
@Override public boolean isHealthy() { // If we know we're not healthy, don't bother pinging. if (!redisIsHealthy) { return false; } ping(); return redisIsHealthy; }
@Override public String put(@NotNull String key, String value) { // Put our single key as a map, and get the value of that one key out of the returned map return putAll(Collections.singletonMap(key, value)).get(key); }
@Override public String remove(@NotNull String key) { if (key == null) { throw new IllegalArgumentException("Cannot remove null key"); } String rKey = redisKey(storeName, key); try (Jedis jedis = pool.getResource()) { String previousValue = jedis.get(rKey); jedis.del(rKey); return previousValue; } catch (JedisException e) { redisIsHealthy = false; String msg = String.format("Unable to remove key %s (Redis key %s)", key, rKey); LOG.error(msg); throw new RuntimeException(msg, e); } }
@Override public void open() { ping(); }
/** * Delete the named singleton instance. * <p> * Also deletes all keys created by the instance. * * @param storeName Name of the singleton instance to delete */ public static synchronized void removeInstance(String storeName) { if (storeName != null) { RedisStore redisStore = REDIS_STORES.get(storeName); if (redisStore != null) { redisStore.removeAllKeys(); REDIS_STORES.remove(storeName); } } } }
String rKey = redisKey(storeName, key);
/** * Get the default KeyValueStore for the given store name. * * @param storeName The store name for which to get the default key value store * * @return the default KeyValueStore */ public static KeyValueStore getDefaultKeyValueStore(TestApiDimensionName storeName) { switch (DimensionBackend.getBackend()) { case REDIS: RedisStore store = RedisStoreManager.getInstance(storeName.asName()); // Key/values stored in Redis persist between tests, so remove // them to give the test a clean environment. store.removeAllKeys(); return store; case MEMORY: default: return MapStoreManager.getInstance(storeName.asName()); } }
escapedStoreName = escapedStoreName.replace("]", "\\]"); // escape ']' escapedStoreName = escapedStoreName.replace("?", "\\?"); // escape '?' String pattern = redisKey(escapedStoreName, "*"); // e.g., bard-escaped_dimension-*
/** * Get the default KeyValueStore for the given store name. * * @param storeName The store name for which to get the default key value store * * @return the default KeyValueStore */ public static KeyValueStore getDefaultKeyValueStore(TestApiDimensionName storeName) { switch (DimensionBackend.getBackend()) { case REDIS: RedisStore store = RedisStoreManager.getInstance(storeName.asName()); // Key/values stored in Redis persist between tests, so remove // them to give the test a clean environment. store.removeAllKeys(); return store; case MEMORY: default: return MapStoreManager.getInstance(storeName.asName()); } }