Skip to content

Commit

Permalink
Adds static RedisStore.getInstances method (iss infinispan#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdzhuvinov committed Nov 10, 2016
1 parent acda16a commit 553165a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/main/java/org/infinispan/persistence/redis/RedisStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String,RedisStore> 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<String,RedisStore> getInstances() {
return Collections.unmodifiableMap(instances);
}

private static final Log log = LogFactory.getLog(RedisStore.class, Log.class);

private InitializationContext ctx = null;
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -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());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 553165a

Please sign in to comment.