Search in sources :

Example 1 with SynchronizedCache

use of org.apache.ibatis.cache.decorators.SynchronizedCache in project mybatis-3 by mybatis.

the class PerpetualCacheTest method shouldFlushAllItemsOnDemand.

@Test
public void shouldFlushAllItemsOnDemand() {
    Cache cache = new PerpetualCache("default");
    cache = new SynchronizedCache(cache);
    for (int i = 0; i < 5; i++) {
        cache.putObject(i, i);
    }
    assertNotNull(cache.getObject(0));
    assertNotNull(cache.getObject(4));
    cache.clear();
    assertNull(cache.getObject(0));
    assertNull(cache.getObject(4));
}
Also used : PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) Test(org.junit.Test)

Example 2 with SynchronizedCache

use of org.apache.ibatis.cache.decorators.SynchronizedCache in project mybatis-3 by mybatis.

the class PerpetualCacheTest method shouldRemoveItemOnDemand.

@Test
public void shouldRemoveItemOnDemand() {
    Cache cache = new PerpetualCache("default");
    cache = new SynchronizedCache(cache);
    cache.putObject(0, 0);
    assertNotNull(cache.getObject(0));
    cache.removeObject(0);
    assertNull(cache.getObject(0));
}
Also used : PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) Test(org.junit.Test)

Example 3 with SynchronizedCache

use of org.apache.ibatis.cache.decorators.SynchronizedCache in project mybatis-3 by mybatis.

the class CacheBuilder method setStandardDecorators.

private Cache setStandardDecorators(Cache cache) {
    try {
        MetaObject metaCache = SystemMetaObject.forObject(cache);
        if (size != null && metaCache.hasSetter("size")) {
            metaCache.setValue("size", size);
        }
        if (clearInterval != null) {
            cache = new ScheduledCache(cache);
            ((ScheduledCache) cache).setClearInterval(clearInterval);
        }
        if (readWrite) {
            cache = new SerializedCache(cache);
        }
        cache = new LoggingCache(cache);
        cache = new SynchronizedCache(cache);
        if (blocking) {
            cache = new BlockingCache(cache);
        }
        return cache;
    } catch (Exception e) {
        throw new CacheException("Error building standard cache decorators.  Cause: " + e, e);
    }
}
Also used : BlockingCache(org.apache.ibatis.cache.decorators.BlockingCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) SystemMetaObject(org.apache.ibatis.reflection.SystemMetaObject) MetaObject(org.apache.ibatis.reflection.MetaObject) CacheException(org.apache.ibatis.cache.CacheException) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) CacheException(org.apache.ibatis.cache.CacheException)

Example 4 with SynchronizedCache

use of org.apache.ibatis.cache.decorators.SynchronizedCache in project mybatis-3 by mybatis.

the class BaseCacheTest method shouldDemonstrateEqualsAndHashCodeForVariousCacheTypes.

@Test
public void shouldDemonstrateEqualsAndHashCodeForVariousCacheTypes() {
    PerpetualCache cache = new PerpetualCache("test_cache");
    assertTrue(cache.equals(cache));
    assertTrue(cache.equals(new SynchronizedCache(cache)));
    assertTrue(cache.equals(new SerializedCache(cache)));
    assertTrue(cache.equals(new LoggingCache(cache)));
    assertTrue(cache.equals(new ScheduledCache(cache)));
    assertEquals(cache.hashCode(), new SynchronizedCache(cache).hashCode());
    assertEquals(cache.hashCode(), new SerializedCache(cache).hashCode());
    assertEquals(cache.hashCode(), new LoggingCache(cache).hashCode());
    assertEquals(cache.hashCode(), new ScheduledCache(cache).hashCode());
    Set<Cache> caches = new HashSet<Cache>();
    caches.add(cache);
    caches.add(new SynchronizedCache(cache));
    caches.add(new SerializedCache(cache));
    caches.add(new LoggingCache(cache));
    caches.add(new ScheduledCache(cache));
    assertEquals(1, caches.size());
}
Also used : PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) HashSet(java.util.HashSet) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) Test(org.junit.Test)

Example 5 with SynchronizedCache

use of org.apache.ibatis.cache.decorators.SynchronizedCache in project mybatis-3 by mybatis.

the class PerpetualCacheTest method shouldDemonstrateHowAllObjectsAreKept.

@Test
public void shouldDemonstrateHowAllObjectsAreKept() {
    Cache cache = new PerpetualCache("default");
    cache = new SynchronizedCache(cache);
    for (int i = 0; i < 100000; i++) {
        cache.putObject(i, i);
        assertEquals(i, cache.getObject(i));
    }
    assertEquals(100000, cache.getSize());
}
Also used : PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache) Test(org.junit.Test)

Aggregations

SerializedCache (org.apache.ibatis.cache.decorators.SerializedCache)5 SynchronizedCache (org.apache.ibatis.cache.decorators.SynchronizedCache)5 PerpetualCache (org.apache.ibatis.cache.impl.PerpetualCache)4 Test (org.junit.Test)4 LoggingCache (org.apache.ibatis.cache.decorators.LoggingCache)2 ScheduledCache (org.apache.ibatis.cache.decorators.ScheduledCache)2 HashSet (java.util.HashSet)1 CacheException (org.apache.ibatis.cache.CacheException)1 BlockingCache (org.apache.ibatis.cache.decorators.BlockingCache)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 SystemMetaObject (org.apache.ibatis.reflection.SystemMetaObject)1