Search in sources :

Example 1 with LoggingCache

use of org.apache.ibatis.cache.decorators.LoggingCache 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 2 with LoggingCache

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

the class CacheBuilder method build.

public Cache build() {
    setDefaultImplementations();
    Cache cache = newBaseCacheInstance(implementation, id);
    setCacheProperties(cache);
    // issue #352, do not apply decorators to custom caches
    if (PerpetualCache.class.equals(cache.getClass())) {
        for (Class<? extends Cache> decorator : decorators) {
            cache = newCacheDecoratorInstance(decorator, cache);
            setCacheProperties(cache);
        }
        cache = setStandardDecorators(cache);
    } else if (!LoggingCache.class.isAssignableFrom(cache.getClass())) {
        cache = new LoggingCache(cache);
    }
    return cache;
}
Also used : LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) SynchronizedCache(org.apache.ibatis.cache.decorators.SynchronizedCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) Cache(org.apache.ibatis.cache.Cache) BlockingCache(org.apache.ibatis.cache.decorators.BlockingCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) LruCache(org.apache.ibatis.cache.decorators.LruCache) SerializedCache(org.apache.ibatis.cache.decorators.SerializedCache)

Example 3 with LoggingCache

use of org.apache.ibatis.cache.decorators.LoggingCache 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 4 with LoggingCache

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

the class ScheduledCacheTest method shouldDemonstrateHowAllObjectsAreFlushedAfterBasedOnTime.

@Test
public void shouldDemonstrateHowAllObjectsAreFlushedAfterBasedOnTime() throws Exception {
    Cache cache = new PerpetualCache("DefaultCache");
    cache = new ScheduledCache(cache);
    ((ScheduledCache) cache).setClearInterval(2500);
    cache = new LoggingCache(cache);
    for (int i = 0; i < 100; i++) {
        cache.putObject(i, i);
        assertEquals(i, cache.getObject(i));
    }
    Thread.sleep(5000);
    assertEquals(0, cache.getSize());
}
Also used : PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) Test(org.junit.Test)

Example 5 with LoggingCache

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

the class ScheduledCacheTest method shouldFlushAllItemsOnDemand.

@Test
public void shouldFlushAllItemsOnDemand() {
    Cache cache = new PerpetualCache("DefaultCache");
    cache = new ScheduledCache(cache);
    ((ScheduledCache) cache).setClearInterval(60000);
    cache = new LoggingCache(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) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) LoggingCache(org.apache.ibatis.cache.decorators.LoggingCache) PerpetualCache(org.apache.ibatis.cache.impl.PerpetualCache) ScheduledCache(org.apache.ibatis.cache.decorators.ScheduledCache) Test(org.junit.Test)

Aggregations

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