From 553165ad961af2a86559d94ae52933698f5ff191 Mon Sep 17 00:00:00 2001 From: Vladimir Dzhuvinov Date: Thu, 10 Nov 2016 18:10:54 +0200 Subject: [PATCH] Adds static RedisStore.getInstances method (iss #10) --- .../persistence/redis/RedisStore.java | 30 +++++++++++++-- .../redis/RedisServerStoreFunctionalTest.java | 38 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/infinispan/persistence/redis/RedisStore.java b/src/main/java/org/infinispan/persistence/redis/RedisStore.java index 093dd15..78f6ba9 100644 --- a/src/main/java/org/infinispan/persistence/redis/RedisStore.java +++ b/src/main/java/org/infinispan/persistence/redis/RedisStore.java @@ -11,9 +11,7 @@ import org.infinispan.util.logging.Log; import org.infinispan.util.logging.LogFactory; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; @@ -23,6 +21,22 @@ @ConfiguredBy(RedisStoreConfiguration.class) final public class RedisStore implements AdvancedLoadWriteStore { + /** + * The instances of this class, keyed by Infinispan cache name. + */ + private static Map instances = new Hashtable<>(); + + + /** + * Returns the {@link #init initialised} instances of this class. + * + * @return The instances of this class as an unmodifiable map, keyed by + * Infinispan cache name. + */ + public static Map getInstances() { + return Collections.unmodifiableMap(instances); + } + private static final Log log = LogFactory.getLog(RedisStore.class, Log.class); private InitializationContext ctx = null; @@ -39,6 +53,11 @@ public void init(InitializationContext ctx) { RedisStore.log.info("Initialising Redis store for cache " + ctx.getCache().getName()); this.ctx = ctx; + + // Register store + if (ctx.getCache().getName() != null) { + RedisStore.instances.put(ctx.getCache().getName(), this); + } } /** @@ -69,6 +88,11 @@ public void stop() if (null != this.connectionPool) { this.connectionPool.shutdown(); } + + // Unregister store + if (ctx.getCache().getName() != null) { + RedisStore.instances.remove(ctx.getCache().getName()); + } } /** diff --git a/src/test/java/org/infinispan/persistence/redis/RedisServerStoreFunctionalTest.java b/src/test/java/org/infinispan/persistence/redis/RedisServerStoreFunctionalTest.java index 4bd1256..00ce643 100644 --- a/src/test/java/org/infinispan/persistence/redis/RedisServerStoreFunctionalTest.java +++ b/src/test/java/org/infinispan/persistence/redis/RedisServerStoreFunctionalTest.java @@ -99,4 +99,42 @@ public void testTwoCachesSameCacheStore() Assert.assertEquals("val2", this.unwrap(second.get("key2"))); Assert.assertNull(first.get("key2")); } + + public void testGetRedisStoreInstance() + { + ConfigurationBuilder cb1 = new ConfigurationBuilder(); + cb1.read(this.cacheManager.getDefaultCacheConfiguration()); + this.createCacheStoreConfig(cb1.persistence(), false, 0); + Configuration c1 = cb1.build(); + + ConfigurationBuilder cb2 = new ConfigurationBuilder(); + cb2.read(this.cacheManager.getDefaultCacheConfiguration()); + this.createCacheStoreConfig(cb2.persistence(), false, 1); + Configuration c2 = cb2.build(); + + Assert.assertTrue(RedisStore.getInstances().isEmpty()); + + this.cacheManager.defineConfiguration("testTwoCachesSameCacheStore-1", c1); + this.cacheManager.defineConfiguration("testTwoCachesSameCacheStore-2", c2); + Cache first = this.cacheManager.getCache("testTwoCachesSameCacheStore-1"); + Cache second = this.cacheManager.getCache("testTwoCachesSameCacheStore-2"); + + first.start(); + second.start(); + + RedisStore firstRedisStore = RedisStore.getInstances().get("testTwoCachesSameCacheStore-1"); + RedisStore secondRedisStore = RedisStore.getInstances().get("testTwoCachesSameCacheStore-2"); + Assert.assertEquals(2, RedisStore.getInstances().size()); + + first.put("key1", "val2"); + second.put("key2", "val2"); + + Assert.assertTrue(firstRedisStore.contains("key1")); + Assert.assertTrue(secondRedisStore.contains("key2")); + + first.stop(); + second.stop(); + + Assert.assertTrue(RedisStore.getInstances().isEmpty()); + } }