Search in sources :

Example 1 with ExoCache

use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.

the class TestCacheService method testCacheService.

public void testCacheService() throws Exception {
    assertNotNull(service_.getAllCacheInstances());
    int size = service_.getAllCacheInstances().size();
    // -----nocache info is retrived from test-configuration(nocache 0bject)
    ExoCache<String, Object> nocache = service_.getCacheInstance("nocache");
    assertTrue("expect find nocache configuaration", nocache instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of nocache is", 5, nocache.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 0, nocache.getLiveTime());
    nocache.put("key1", "object 1");
    assertEquals("expect 'nocache' is not lived(LiveTime=0)", 0, nocache.getCacheSize());
    // -----cacheLiveTime2s's info is retrived from test-configuration
    // (cacheLiveTime2s object)
    ExoCache<String, Object> cacheLiveTime2s = service_.getCacheInstance("cacheLiveTime2s");
    assertTrue("expect find cacheLiveTime2s configuaration", cacheLiveTime2s instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 5, cacheLiveTime2s.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 2, cacheLiveTime2s.getLiveTime());
    cacheLiveTime2s.put("key", "object2s");
    String obj2s = (String) cacheLiveTime2s.get("key");
    assertTrue("expect found 'object' in cache", obj2s != null && obj2s.equals("object2s"));
    assertEquals("expect found object in this cache", 1, cacheLiveTime2s.getCacheSize());
    Thread.sleep(2500);
    assertTrue("expect no found 'object' in this cache", cacheLiveTime2s.get("key") == null);
    assertEquals("expect cache size is ", 0, cacheLiveTime2s.getCacheSize());
    // -----cacheMaxSize0's info retrived from test-configuration (cacheMaxSize0
    // object)
    ExoCache<String, Object> cacheMaxSize0 = service_.getCacheInstance("cacheMaxSize0");
    assertTrue("expect find cacheMaxSize0 configuaration", cacheMaxSize0 instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 0, cacheMaxSize0.getMaxSize());
    assertEquals("expect 'liveTime' of nocache' is", 4, cacheMaxSize0.getLiveTime());
    cacheMaxSize0.put("mkey", "maxsize object");
    assertTrue("expect can't put any object to  cache", cacheMaxSize0.get("mkey") == null);
    // -----default cache's info is retrived if no cache's info is found
    ExoCache<String, Object> cache = service_.getCacheInstance("exo");
    assertTrue("expect find defaul cache configuaration", cache instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 100, cache.getMaxSize());
    assertEquals("expect 'liveTime' of this cache' is", 300, cache.getLiveTime());
    cache.put("test", "this is a test");
    String ret = (String) cache.get("test");
    assertTrue("expect object is cached", ret != null);
    /* ----------FIFOExoCache--------------- */
    ExoCache<String, Object> fifoCache = service_.getCacheInstance("fifocache");
    assertTrue("expect find fifo cache configuration", fifoCache instanceof FIFOExoCache);
    assertEquals("expect 'maxsize' of this cache is", 3, fifoCache.getMaxSize());
    assertEquals("expect 'liveTime' of this cache' is", 4, fifoCache.getLiveTime());
    fifoCache.put("key1", "object 1");
    fifoCache.put("key2", "object 2");
    assertEquals("expect FIFOExoCache size is:", 2, fifoCache.getCacheSize());
    String obj1 = (String) fifoCache.get("key1");
    String obj2 = (String) fifoCache.get("key2");
    assertTrue("expect found 'key1' object", obj1 != null && obj1.equals("object 1"));
    assertTrue("expect found 'key2' object", obj2 != null && obj2.equals("object 2"));
    fifoCache.put("skey", "serializable object");
    assertEquals("expect FIFOExoCache size is:", 3, fifoCache.getCacheSize());
    String sobj = (String) fifoCache.get("skey");
    assertTrue("expect found serializable key and it's value", sobj != null && sobj.equals("serializable object"));
    fifoCache.put("key4", "object 4");
    // because maxsize of cache is 3, 'object 1' associated with 'key1' is
    // remove form FIFOExoCache
    assertEquals("expect cache size is still:", 3, fifoCache.getCacheSize());
    String obj4 = (String) fifoCache.get("key4");
    assertTrue("expect object has 'key4' is put in cache", obj4 != null && obj4.equals("object 4"));
    assertTrue("expect object has key is 'key1' is remove automatically", fifoCache.get("key1") == null);
    // -------remove a object in cache by key
    fifoCache.remove("key2");
    assertEquals("now, expect cache size is", 2, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 2, fifoCache.getCachedObjects().size());
    assertTrue("expect object has 'key2' is removed", fifoCache.get("key2") == null);
    // -------remove a object in cache by serializable name
    fifoCache.remove(new String("skey"));
    assertEquals("now, expect cache size is", 1, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 1, fifoCache.getCachedObjects().size());
    assertTrue("expect serializable object with name 'skey' is remove", fifoCache.get(new String("skey")) == null);
    // --------------clear cache
    fifoCache.clearCache();
    assertEquals("now, expect cache is clear", 0, fifoCache.getCacheSize());
    assertEquals("now, expect number of object in cache is:", 0, fifoCache.getCachedObjects().size());
    /* --------------test cache service with add extenal component plugin------ */
    ExoCache simpleCachePlugin = service_.getCacheInstance("simpleCachePlugin");
    assertTrue("expect found simpleCache from extenal plugin", simpleCachePlugin instanceof SimpleExoCache);
    assertEquals("expect 'maxsize' of this cache is", 8, simpleCachePlugin.getMaxSize());
    assertEquals("expect 'LiveTime' of this cache is", 5, simpleCachePlugin.getLiveTime());
    ExoCache fifoCachePlugin = service_.getCacheInstance("fifoCachePlugin");
    assertTrue("expect found fifoCache from extenal plugin", fifoCachePlugin instanceof FIFOExoCache);
    assertEquals("expect 'maxsize' of this cache is", 6, fifoCachePlugin.getMaxSize());
    assertEquals("expect 'LiveTime' of this cache is", 10, fifoCachePlugin.getLiveTime());
    // ----all cache instances---
    Collection<ExoCache<? extends Serializable, ?>> caches = service_.getAllCacheInstances();
    assertEquals("expect number of cache instanse is ", size + 7, caches.size());
    hasObjectInCollection(nocache, caches, new ExoCacheComparator());
    hasObjectInCollection(cacheLiveTime2s, caches, new ExoCacheComparator());
    hasObjectInCollection(cacheMaxSize0, caches, new ExoCacheComparator());
    hasObjectInCollection(fifoCache, caches, new ExoCacheComparator());
    hasObjectInCollection(cache, caches, new ExoCacheComparator());
    hasObjectInCollection(simpleCachePlugin, caches, new ExoCacheComparator());
    hasObjectInCollection(fifoCachePlugin, caches, new ExoCacheComparator());
    // Managed tests
    MBeanServerLocator locator = (MBeanServerLocator) PortalContainer.getInstance().getComponentInstanceOfType(MBeanServerLocator.class);
    MBeanServer server = locator.server;
    assertNotNull(locator.server);
    ObjectName name = new ObjectName("exo:service=cache,name=cacheLiveTime2s,portal=portal");
    MBeanInfo info = server.getMBeanInfo(name);
    assertNotNull(info);
    Map<String, MBeanAttributeInfo> infoMap = new HashMap<String, MBeanAttributeInfo>();
    for (MBeanAttributeInfo attributeInfo : info.getAttributes()) {
        infoMap.put(attributeInfo.getName(), attributeInfo);
    }
    assertTrue(infoMap.containsKey("Name"));
    assertTrue(infoMap.containsKey("Size"));
    assertTrue(infoMap.containsKey("Capacity"));
    assertTrue(infoMap.containsKey("TimeToLive"));
    assertTrue(infoMap.containsKey("HitCount"));
    assertTrue(infoMap.containsKey("MissCount"));
    assertEquals(6, infoMap.size());
    assertEquals(5, server.getAttribute(name, "Capacity"));
    assertEquals(size + 7, service_.getAllCacheInstances().size());
}
Also used : FIFOExoCache(org.exoplatform.services.cache.FIFOExoCache) ExoCache(org.exoplatform.services.cache.ExoCache) SimpleExoCache(org.exoplatform.services.cache.SimpleExoCache) ConcurrentFIFOExoCache(org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache) Serializable(java.io.Serializable) MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) SimpleExoCache(org.exoplatform.services.cache.SimpleExoCache) ObjectName(javax.management.ObjectName) FIFOExoCache(org.exoplatform.services.cache.FIFOExoCache) ConcurrentFIFOExoCache(org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache) MBeanServer(javax.management.MBeanServer)

Example 2 with ExoCache

use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.

the class TestAbstractExoCache method testSelect.

public void testSelect() throws Exception {
    cache.put(new MyKey("a"), 1);
    cache.put(new MyKey("b"), 2);
    cache.put(new MyKey("c"), 3);
    final AtomicInteger count = new AtomicInteger();
    CachedObjectSelector<Serializable, Object> selector = new CachedObjectSelector<Serializable, Object>() {

        public void onSelect(ExoCache<? extends Serializable, ? extends Object> cache, Serializable key, ObjectCacheInfo<? extends Object> ocinfo) throws Exception {
            assertTrue(key.equals(new MyKey("a")) || key.equals(new MyKey("b")) || key.equals(new MyKey("c")));
            assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) || ocinfo.get().equals(3));
            count.incrementAndGet();
        }

        public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
            return true;
        }
    };
    cache.select(selector);
    assertEquals(3, count.intValue());
    cache.clearCache();
}
Also used : Serializable(java.io.Serializable) ExoCache(org.exoplatform.services.cache.ExoCache) CachedObjectSelector(org.exoplatform.services.cache.CachedObjectSelector) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ObjectCacheInfo(org.exoplatform.services.cache.ObjectCacheInfo)

Example 3 with ExoCache

use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.

the class TestExoCacheFactoryImpl method testSameCacheManager.

public void testSameCacheManager() {
    ExoCache cache1 = service_.getCacheInstance("myCustomCache");
    assertTrue("expect an instance of AbstractExoCache", cache1 instanceof AbstractExoCache);
    AbstractExoCache aCache1 = (AbstractExoCache) cache1;
    CacheContainer cacheContainer1 = aCache1.cache.getCacheManager();
    ExoCache cache2 = service_.getCacheInstance("myCustomCache-Bis");
    assertTrue("expect an instance of AbstractExoCache", cache2 instanceof AbstractExoCache);
    AbstractExoCache aCache2 = (AbstractExoCache) cache2;
    CacheContainer cacheContainer2 = aCache2.cache.getCacheManager();
    assertTrue("The CacheContainer should be the same", cacheContainer1 == cacheContainer2);
    ExoCache cache3 = service_.getCacheInstance("myCustomCache-Bis2");
    assertTrue("expect an instance of AbstractExoCache", cache3 instanceof AbstractExoCache);
    AbstractExoCache aCache3 = (AbstractExoCache) cache3;
    CacheContainer cacheContainer3 = aCache3.cache.getCacheManager();
    assertTrue("The CacheContainer should be the same", cacheContainer1 == cacheContainer3);
    aCache1.cache.stop();
    aCache2.cache.stop();
}
Also used : ExoCache(org.exoplatform.services.cache.ExoCache) TestExoCache(org.exoplatform.services.cache.impl.infinispan.TestExoCacheCreator.TestExoCache) CacheContainer(org.infinispan.manager.CacheContainer)

Example 4 with ExoCache

use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.

the class TestExoCacheFactoryImpl method testExoCacheCreator.

public void testExoCacheCreator() {
    ExoCache cache = service_.getCacheInstance("test-default-impl");
    assertTrue("expect an instance of AbstractExoCache", cache instanceof AbstractExoCache);
    AbstractExoCache aCache = (AbstractExoCache) cache;
    aCache.cache.stop();
    cache = service_.getCacheInstance("test-custom-impl-with-old-config");
    assertTrue("expect an instance of TestExoCache", cache instanceof TestExoCache);
    cache = service_.getCacheInstance("test-custom-impl-with-new-config");
    assertTrue("expect an instance of TestExoCache", cache instanceof TestExoCache);
}
Also used : TestExoCache(org.exoplatform.services.cache.impl.infinispan.TestExoCacheCreator.TestExoCache) ExoCache(org.exoplatform.services.cache.ExoCache) TestExoCache(org.exoplatform.services.cache.impl.infinispan.TestExoCacheCreator.TestExoCache)

Example 5 with ExoCache

use of org.exoplatform.services.cache.ExoCache in project kernel by exoplatform.

the class TestExoCacheFactoryImpl method testCacheFactory.

public void testCacheFactory() {
    CacheService service_ = (CacheService) pc.getComponentInstanceOfType(CacheService.class);
    @SuppressWarnings("rawtypes") ExoCache cache = service_.getCacheInstance("myCache");
    assertTrue("expect an instance of MCExoCache but was " + cache, cache instanceof MCExoCache);
}
Also used : ExoCache(org.exoplatform.services.cache.ExoCache) CacheService(org.exoplatform.services.cache.CacheService)

Aggregations

ExoCache (org.exoplatform.services.cache.ExoCache)9 Serializable (java.io.Serializable)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CachedObjectSelector (org.exoplatform.services.cache.CachedObjectSelector)3 ObjectCacheInfo (org.exoplatform.services.cache.ObjectCacheInfo)3 TestExoCache (org.exoplatform.services.cache.impl.infinispan.TestExoCacheCreator.TestExoCache)3 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 HashMap (java.util.HashMap)1 Callable (java.util.concurrent.Callable)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanInfo (javax.management.MBeanInfo)1 MBeanServer (javax.management.MBeanServer)1 ObjectName (javax.management.ObjectName)1 CacheService (org.exoplatform.services.cache.CacheService)1 ExoCacheInitException (org.exoplatform.services.cache.ExoCacheInitException)1 FIFOExoCache (org.exoplatform.services.cache.FIFOExoCache)1 SimpleExoCache (org.exoplatform.services.cache.SimpleExoCache)1 ConcurrentFIFOExoCache (org.exoplatform.services.cache.concurrent.ConcurrentFIFOExoCache)1 DistributedExoCache (org.exoplatform.services.cache.impl.infinispan.distributed.DistributedExoCache)1