use of org.exoplatform.services.cache.ExoCacheConfig in project kernel by exoplatform.
the class TestCacheService method testCacheFactory.
public void testCacheFactory() throws Exception {
InitParams params = new InitParams();
ObjectParameter param = new ObjectParameter();
param.setName("NoImpl");
ExoCacheConfig config = new ExoCacheConfig();
config.setName(param.getName());
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("KnownImpl");
config = new ExoCacheConfig();
config.setName(param.getName());
config.setImplementation("org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache");
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("UnKnownImpl");
config = new ExoCacheConfig();
config.setName(param.getName());
config.setImplementation("fakeImpl");
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("UnKnownImplButCorrectFQN");
config = new ExoCacheConfig();
config.setName(param.getName());
config.setImplementation("java.lang.String");
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("NoImpl-MyExoCacheConfig");
config = new MyExoCacheConfig();
config.setName(param.getName());
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("KnownImpl-MyExoCacheConfig");
config = new MyExoCacheConfig();
config.setName(param.getName());
config.setImplementation("org.exoplatform.services.cache.FIFOExoCache");
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("UnKnownImpl-MyExoCacheConfig");
config = new MyExoCacheConfig();
config.setName(param.getName());
config.setImplementation("fakeImpl");
param.setObject(config);
params.addParameter(param);
param = new ObjectParameter();
param.setName("UnKnownImplButCorrectFQN-MyExoCacheConfig");
config = new MyExoCacheConfig();
config.setName(param.getName());
config.setImplementation("java.lang.String");
param.setObject(config);
params.addParameter(param);
CacheService cs = new CacheServiceImpl(params, new MyExoCacheFactory());
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("NoImpl").getClass(), cs.getCacheInstance("NoImpl") instanceof MyExoCache);
assertTrue("Expected type ConcurrentFIFOExoCache found " + cs.getCacheInstance("KnownImpl").getClass(), cs.getCacheInstance("KnownImpl") instanceof ConcurrentFIFOExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("UnKnownImpl").getClass(), cs.getCacheInstance("UnKnownImpl") instanceof MyExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("UnKnownImplButCorrectFQN").getClass(), cs.getCacheInstance("UnKnownImplButCorrectFQN") instanceof MyExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("NoImpl-MyExoCacheConfig").getClass(), cs.getCacheInstance("NoImpl-MyExoCacheConfig") instanceof MyExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("KnownImpl-MyExoCacheConfig").getClass(), cs.getCacheInstance("KnownImpl-MyExoCacheConfig") instanceof MyExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("UnKnownImpl-MyExoCacheConfig").getClass(), cs.getCacheInstance("UnKnownImpl-MyExoCacheConfig") instanceof MyExoCache);
assertTrue("Expected type MyExoCache found " + cs.getCacheInstance("UnKnownImplButCorrectFQN-MyExoCacheConfig").getClass(), cs.getCacheInstance("UnKnownImplButCorrectFQN-MyExoCacheConfig") instanceof MyExoCache);
}
use of org.exoplatform.services.cache.ExoCacheConfig in project kernel by exoplatform.
the class CacheServiceImpl method createCacheInstance.
@SuppressWarnings({ "rawtypes", "unchecked" })
private ExoCache<? extends Serializable, ?> createCacheInstance(String region) throws Exception {
ExoCacheConfig config = configs_.get(region);
if (config == null)
config = defaultConfig_;
// Ensure the configuration integrity
final ExoCacheConfig safeConfig = config.clone();
// Set the region as name
safeConfig.setName(region);
ExoCache simple = null;
if (// NOSONAR
factory_ != DEFAULT_FACTORY && safeConfig.getClass().isAssignableFrom(ExoCacheConfig.class) && safeConfig.getImplementation() != null) {
// we assume that we expect to use the default cache factory
try {
// We check if the given implementation is a known class
Class<?> implClass = ClassLoading.loadClass(safeConfig.getImplementation(), this);
// Implementation is an existing class
if (ExoCache.class.isAssignableFrom(implClass)) {
// The implementation is a sub class of eXo Cache so we use the default factory
simple = DEFAULT_FACTORY.createCache(safeConfig);
}
} catch (ClassNotFoundException e) {
if (LOG.isTraceEnabled()) {
LOG.trace("An exception occurred: " + e.getMessage());
}
}
}
if (simple == null) {
// We use the configured cache factory
simple = factory_.createCache(safeConfig);
}
if (managed != null) {
managed.registerCache(simple);
}
// to enable the invalidation
return safeConfig.avoidValueReplication() && (safeConfig.isRepicated() || safeConfig.isDistributed()) ? new InvalidationExoCache(simple) : simple;
}
use of org.exoplatform.services.cache.ExoCacheConfig in project kernel by exoplatform.
the class TestDistributedExoCache method testDistributedCache.
/**
* WARNING: For Linux distributions the following JVM parameter must be set to true: java.net.preferIPv4Stack.
*
* @throws Exception
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testDistributedCache() throws Exception {
PortalContainer pc = PortalContainer.getInstance();
ExoCacheConfig config = new ExoCacheConfig();
config.setName("MyCacheDistributed");
config.setMaxSize(5);
config.setLiveTime(1);
config.setImplementation("LRU");
config.setDistributed(true);
Map<String, String> params = new HashMap<String, String>();
params.put("infinispan-num-owners", "1");
ConfigurationManager cm = (ConfigurationManager) pc.getComponentInstanceOfType(ConfigurationManager.class);
DistributedCacheManager dcm2 = new DistributedCacheManager("jar:/conf/portal/distributed-cache-configuration.xml", params, cm);
DistributedExoCache<Serializable, Object> cache1 = (DistributedExoCache<Serializable, Object>) ((ExoCacheFactory) pc.getComponentInstanceOfType(ExoCacheFactory.class)).createCache(config);
DistributionManager dm = cache1.getCache().getDistributionManager();
DistributedExoCache<Serializable, Object> cache2 = (DistributedExoCache<Serializable, Object>) new ExoCacheFactoryImpl((ExoContainerContext) pc.getComponentInstanceOfType(ExoContainerContext.class), "jar:/conf/portal/cache-configuration-template.xml", cm, dcm2).createCache(config);
KeyAffinityService kas1 = KeyAffinityServiceFactory.newLocalKeyAffinityService(cache1.getCache(), new MyKeyGenerator(cache1.getFullName()), Executors.newSingleThreadExecutor(), 100);
KeyAffinityService kas2 = KeyAffinityServiceFactory.newLocalKeyAffinityService(cache2.getCache(), new MyKeyGenerator(cache1.getFullName()), Executors.newSingleThreadExecutor(), 100);
try {
Object a, b, c;
for (int i = 0; i < 2; i++) {
if (i == 0) {
a = new MyKey("a", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
} else {
a = new MyKey("a", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
}
for (int j = 0; j < 2; j++) {
if (j == 0) {
b = new MyKey("b", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
} else {
b = new MyKey("b", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
}
for (int k = 0; k < 2; k++) {
if (k == 0) {
c = new MyKey("c", ((DistributedExoCache.CacheKey<MyKey>) kas1.getKeyForAddress(cache1.getCache().getRpcManager().getAddress())).getKey().value);
} else {
c = new MyKey("c", ((DistributedExoCache.CacheKey<MyKey>) kas2.getKeyForAddress(cache2.getCache().getRpcManager().getAddress())).getKey().value);
}
checkUseCase(cache1, cache2, dm, a, b, c);
}
}
}
} finally {
dcm2.stop();
}
}
use of org.exoplatform.services.cache.ExoCacheConfig in project kernel by exoplatform.
the class TestAbstractExoCache method testDistributedCache.
/**
* WARNING: For Linux distributions the following JVM parameter must be set to true: java.net.preferIPv4Stack
*/
@SuppressWarnings("unchecked")
public void testDistributedCache() throws Exception {
// If the cache is still alive this test fails due to a TimeoutException.
// cache.cache.getCacheManager().stop();
ExoCacheConfig config = new ExoCacheConfig();
config.setName("MyCacheDistributed");
config.setMaxSize(8);
config.setLiveTime(1);
config.setImplementation("LRU");
config.setReplicated(true);
ExoCacheConfig config2 = new ExoCacheConfig();
config2.setName("MyCacheDistributed2");
config2.setMaxSize(8);
config2.setLiveTime(1);
config2.setImplementation("LRU");
config2.setReplicated(true);
AbstractExoCache<Serializable, Object> cache1 = (AbstractExoCache<Serializable, Object>) getExoCacheFactoryInstance().createCache(config);
MyCacheListener listener1 = new MyCacheListener();
cache1.addCacheListener(listener1);
AbstractExoCache<Serializable, Object> cache2 = (AbstractExoCache<Serializable, Object>) getExoCacheFactoryInstance().createCache(config);
MyCacheListener listener2 = new MyCacheListener();
cache2.addCacheListener(listener2);
// cache3.addCacheListener(listener3);
try {
cache1.put(new MyKey("a"), "b");
assertEquals(1, cache1.getCacheSize());
assertEquals("b", cache2.get(new MyKey("a")));
assertEquals(1, cache2.getCacheSize());
// assertEquals(0, cache3.getCacheSize());
assertEquals(1, listener1.put);
assertEquals(1, listener2.put);
// assertEquals(0, listener3.put);
assertEquals(0, listener1.get);
assertEquals(1, listener2.get);
// assertEquals(0, listener3.get);
cache2.put(new MyKey("b"), "c");
assertEquals(2, cache1.getCacheSize());
assertEquals(2, cache2.getCacheSize());
assertEquals("c", cache1.get(new MyKey("b")));
// assertEquals(0, cache3.getCacheSize());
assertEquals(2, listener1.put);
assertEquals(2, listener2.put);
// assertEquals(0, listener3.put);
assertEquals(1, listener1.get);
assertEquals(1, listener2.get);
// assertEquals(0, listener3.get);
// cache3.put(new MyKey("c"), "d");
assertEquals(2, cache1.getCacheSize());
assertEquals(2, cache2.getCacheSize());
// assertEquals(1, cache3.getCacheSize());
// assertEquals("d", cache3.get(new MyKey("c")));
assertEquals(2, listener1.put);
assertEquals(2, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(1, listener1.get);
assertEquals(1, listener2.get);
// assertEquals(1, listener3.get);
cache2.put(new MyKey("a"), "a");
assertEquals(2, cache1.getCacheSize());
assertEquals(2, cache2.getCacheSize());
assertEquals("a", cache1.get(new MyKey("a")));
assertEquals(3, listener1.put);
assertEquals(3, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(2, listener1.get);
assertEquals(1, listener2.get);
// assertEquals(1, listener3.get);
cache2.remove(new MyKey("a"));
assertEquals(1, cache1.getCacheSize());
assertEquals(1, cache2.getCacheSize());
assertEquals(3, listener1.put);
assertEquals(3, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(2, listener1.get);
assertEquals(1, listener2.get);
// assertEquals(1, listener3.get);
assertEquals(1, listener1.remove);
assertEquals(1, listener2.remove);
// assertEquals(0, listener3.remove);
cache1.put(new MyKey("c"), "c");
cache1.clearCache();
assertEquals(0, cache1.getCacheSize());
assertEquals(null, cache1.get(new MyKey("b")));
assertEquals("c", cache2.get(new MyKey("b")));
assertEquals("c", cache2.get(new MyKey("c")));
assertEquals(2, cache2.getCacheSize());
assertEquals(4, listener1.put);
assertEquals(4, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(3, listener1.get);
assertEquals(3, listener2.get);
// assertEquals(1, listener3.get);
assertEquals(1, listener1.remove);
assertEquals(1, listener2.remove);
// assertEquals(0, listener3.remove);
assertEquals(1, listener1.clearCache);
assertEquals(0, listener2.clearCache);
// assertEquals(0, listener3.clearCache);
Map<Serializable, Object> values = new HashMap<Serializable, Object>();
values.put(new MyKey("a"), "a");
values.put(new MyKey("b"), "b");
cache1.putMap(values);
assertEquals(2, cache1.getCacheSize());
Thread.sleep(40);
assertEquals("a", cache2.get(new MyKey("a")));
assertEquals("b", cache2.get(new MyKey("b")));
assertEquals(3, cache2.getCacheSize());
assertEquals(6, listener1.put);
assertEquals(6, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(3, listener1.get);
assertEquals(5, listener2.get);
// assertEquals(1, listener3.get);
assertEquals(1, listener1.remove);
assertEquals(1, listener2.remove);
// assertEquals(0, listener3.remove);
assertEquals(1, listener1.clearCache);
assertEquals(0, listener2.clearCache);
// assertEquals(0, listener3.clearCache);
values = new HashMap<Serializable, Object>() {
private static final long serialVersionUID = 1L;
public Set<Entry<Serializable, Object>> entrySet() {
Set<Entry<Serializable, Object>> set = new LinkedHashSet<Entry<Serializable, Object>>(super.entrySet());
set.add(new Entry<Serializable, Object>() {
public Object setValue(Object paramV) {
return null;
}
public Object getValue() {
throw new RuntimeException("An exception");
}
public Serializable getKey() {
return "c";
}
});
return set;
}
};
values.put(new MyKey("e"), "e");
values.put(new MyKey("d"), "d");
cache1.putMap(values);
assertEquals(2, cache1.getCacheSize());
assertEquals(3, cache2.getCacheSize());
// assertEquals(1, cache3.getCacheSize());
assertEquals(6, listener1.put);
assertEquals(6, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(3, listener1.get);
assertEquals(5, listener2.get);
// assertEquals(1, listener3.get);
assertEquals(1, listener1.remove);
assertEquals(1, listener2.remove);
// assertEquals(0, listener3.remove);
assertEquals(1, listener1.clearCache);
assertEquals(0, listener2.clearCache);
// assertEquals(0, listener3.clearCache);
assertEquals(0, listener1.expire);
assertEquals(0, listener2.expire);
// assertEquals(0, listener3.expire);
Thread.sleep(5600);
// The values are evicted lazily when we call it
cache1.get(new MyKey("a"));
cache1.get(new MyKey("b"));
cache2.get(new MyKey("a"));
cache2.get(new MyKey("b"));
cache2.get(new MyKey("c"));
assertEquals(0, cache1.getCacheSize());
assertEquals(0, cache2.getCacheSize());
// assertEquals(0, cache3.getCacheSize());
assertEquals(6, listener1.put);
assertEquals(6, listener2.put);
// assertEquals(1, listener3.put);
assertEquals(5, listener1.get);
assertEquals(8, listener2.get);
// assertEquals(1, listener3.get);
assertEquals(1, listener1.remove);
assertEquals(1, listener2.remove);
// assertEquals(0, listener3.remove);
assertEquals(1, listener1.clearCache);
assertEquals(0, listener2.clearCache);
// assertEquals(0, listener3.clearCache);
// Expiration events are not triggered in infinispan
// assertEquals(2, listener1.expire);
// assertEquals(3, listener2.expire);
// assertEquals(1, listener3.expire);
} finally {
cache1.cache.getCacheManager().stop();
cache2.cache.getCacheManager().stop();
// cache3.cache.getCacheManager().stop();
}
}
Aggregations