org.apache.ibatis.cache.impl.PerpetualCache Java Examples

The following examples show how to use org.apache.ibatis.cache.impl.PerpetualCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MapperBuilderAssistant.java    From mybatis with Apache License 2.0 6 votes vote down vote up
public Cache useNewCache(Class<? extends Cache> typeClass,
    Class<? extends Cache> evictionClass,
    Long flushInterval,
    Integer size,
    boolean readWrite,
    boolean blocking,
    Properties props) {
    //这里面又判断了一下是否为null就用默认值,有点和XMLMapperBuilder.cacheElement逻辑重复了
  typeClass = valueOrDefault(typeClass, PerpetualCache.class);
  evictionClass = valueOrDefault(evictionClass, LruCache.class);
  //调用CacheBuilder构建cache,id=currentNamespace
  Cache cache = new CacheBuilder(currentNamespace)
      .implementation(typeClass)
      .addDecorator(evictionClass)
      .clearInterval(flushInterval)
      .size(size)
      .readWrite(readWrite)
      .blocking(blocking)
      .properties(props)
      .build();
  //加入缓存
  configuration.addCache(cache);
  //当前的缓存
  currentCache = cache;
  return cache;
}
 
Example #2
Source File: SoftCacheTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDemonstrateObjectsBeingCollectedAsNeeded() throws Exception {
  final int N = 3000000;
  SoftCache cache = new SoftCache(new PerpetualCache("default"));
  for (int i = 0; i < N; i++) {
    byte[] array = new byte[5001]; //waste a bunch of memory
    array[5000] = 1;
    cache.putObject(i, array);
    Object value = cache.getObject(i);
    if (cache.getSize() < i + 1) {
      //System.out.println("Cache exceeded with " + (i + 1) + " entries.");
      break;
    }
  }
  assertTrue(cache.getSize() < N);
}
 
Example #3
Source File: SuperCacheTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
  public void shouldDemonstrate5LevelSuperCacheHandlesLotsOfEntriesWithoutCrashing() {
    final int N = 100000;
    Cache cache = new PerpetualCache("default");
    cache = new LruCache(cache);
    cache = new FifoCache(cache);
    cache = new SoftCache(cache);
    cache = new WeakCache(cache);
    cache = new ScheduledCache(cache);
    cache = new SerializedCache(cache);
//    cache = new LoggingCache(cache);
    cache = new SynchronizedCache(cache);
    cache = new TransactionalCache(cache);
    for (int i = 0; i < N; i++) {
      cache.putObject(i, i);
      ((TransactionalCache) cache).commit();
      Object o = cache.getObject(i);
      assertTrue(o == null || i == ((Integer) o));
    }
    assertTrue(cache.getSize() < N);
  }
 
Example #4
Source File: BaseCacheTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #5
Source File: SoftCacheTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDemonstrateObjectsBeingCollectedAsNeeded() throws Exception {
  final int N = 3000000;
  SoftCache cache = new SoftCache(new PerpetualCache("default"));
  for (int i = 0; i < N; i++) {
    byte[] array = new byte[5001]; //waste a bunch of memory
    array[5000] = 1;
    cache.putObject(i, array);
    Object value = cache.getObject(i);
    if (cache.getSize() < i + 1) {
      //System.out.println("Cache exceeded with " + (i + 1) + " entries.");
      break;
    }
  }
  assertTrue(cache.getSize() < N);
}
 
Example #6
Source File: SuperCacheTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
  public void shouldDemonstrate5LevelSuperCacheHandlesLotsOfEntriesWithoutCrashing() {
    final int N = 100000;
    Cache cache = new PerpetualCache("default");
    cache = new LruCache(cache);
    cache = new FifoCache(cache);
    cache = new SoftCache(cache);
    cache = new WeakCache(cache);
    cache = new ScheduledCache(cache);
    cache = new SerializedCache(cache);
//    cache = new LoggingCache(cache);
    cache = new SynchronizedCache(cache);
    cache = new TransactionalCache(cache);
    for (int i = 0; i < N; i++) {
      cache.putObject(i, i);
      ((TransactionalCache) cache).commit();
      Object o = cache.getObject(i);
      assertTrue(o == null || i == ((Integer) o));
    }
    assertTrue(cache.getSize() < N);
  }
 
Example #7
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSucceedWhenFullyQualifiedButFailDueToAmbiguity() {
  Configuration c = new Configuration();

  final String name1 = "com.mycache.MyCache";
  final PerpetualCache cache1 = new PerpetualCache(name1);
  c.addCache(cache1);

  final String name2 = "com.other.MyCache";
  final PerpetualCache cache2 = new PerpetualCache(name2);
  c.addCache(cache2);

  final String shortName = "MyCache";

  assertEquals(cache1, c.getCache(name1));
  assertEquals(cache2, c.getCache(name2));

  try {
    c.getCache(shortName);
    fail("Exception expected.");
  } catch (Exception e) {
    assertTrue(e.getMessage().contains("ambiguous"));
  }

}
 
Example #8
Source File: SqlSessionTest.java    From mybaties with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSucceedWhenFullyQualifiedButFailDueToAmbiguity() {
  Configuration c = new Configuration();

  final String name1 = "com.mycache.MyCache";
  final PerpetualCache cache1 = new PerpetualCache(name1);
  c.addCache(cache1);

  final String name2 = "com.other.MyCache";
  final PerpetualCache cache2 = new PerpetualCache(name2);
  c.addCache(cache2);

  final String shortName = "MyCache";

  assertEquals(cache1, c.getCache(name1));
  assertEquals(cache2, c.getCache(name2));

  try {
    c.getCache(shortName);
    fail("Exception expected.");
  } catch (Exception e) {
    assertTrue(e.getMessage().contains("ambiguous"));
  }

}
 
Example #9
Source File: BaseCacheTest.java    From mybatis with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #10
Source File: MapperBuilderAssistant.java    From mybaties with Apache License 2.0 6 votes vote down vote up
public Cache useNewCache(Class<? extends Cache> typeClass,
    Class<? extends Cache> evictionClass,
    Long flushInterval,
    Integer size,
    boolean readWrite,
    boolean blocking,
    Properties props) {
    //这里面又判断了一下是否为null就用默认值,有点和XMLMapperBuilder.cacheElement逻辑重复了
  typeClass = valueOrDefault(typeClass, PerpetualCache.class);
  evictionClass = valueOrDefault(evictionClass, LruCache.class);
  //调用CacheBuilder构建cache,id=currentNamespace
  Cache cache = new CacheBuilder(currentNamespace)
      .implementation(typeClass)
      .addDecorator(evictionClass)
      .clearInterval(flushInterval)
      .size(size)
      .readWrite(readWrite)
      .blocking(blocking)
      .properties(props)
      .build();
  //加入缓存
  configuration.addCache(cache);
  //当前的缓存
  currentCache = cache;
  return cache;
}
 
Example #11
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test(expected=IllegalArgumentException.class)
public void shouldFailOverToMostApplicableSimpleName() {
  Configuration c = new Configuration();
  final String fullName = "com.mycache.MyCache";
  final String invalidName = "unknown.namespace.MyCache";
  final PerpetualCache cache = new PerpetualCache(fullName);
  c.addCache(cache);
  assertEquals(cache, c.getCache(fullName));
  assertEquals(cache, c.getCache(invalidName));
}
 
Example #12
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldResolveBothSimpleNameAndFullyQualifiedName() {
  Configuration c = new Configuration();
  final String fullName = "com.mycache.MyCache";
  final String shortName = "MyCache";
  final PerpetualCache cache = new PerpetualCache(fullName);
  c.addCache(cache);
  assertEquals(cache, c.getCache(fullName));
  assertEquals(cache, c.getCache(shortName));
}
 
Example #13
Source File: BaseExecutor.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public DeferredLoad(MetaObject resultObject,
                    String property,
                    CacheKey key,
                    PerpetualCache localCache,
                    Configuration configuration,
                    Class<?> targetType) {
  this.resultObject = resultObject;
  this.property = property;
  this.key = key;
  this.localCache = localCache;
  this.objectFactory = configuration.getObjectFactory();
  this.resultExtractor = new ResultExtractor(configuration, objectFactory);
  this.targetType = targetType;
}
 
Example #14
Source File: SqlSessionTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFailToAddDueToNameConflict() {
  Configuration c = new Configuration();
  final String fullName = "com.mycache.MyCache";
  final PerpetualCache cache = new PerpetualCache(fullName);
  try {
    c.addCache(cache);
    c.addCache(cache);
    fail("Exception expected.");
  } catch (Exception e) {
    assertTrue(e.getMessage().contains("already contains value"));
  }
}
 
Example #15
Source File: BaseExecutor.java    From mybatis with Apache License 2.0 5 votes vote down vote up
protected BaseExecutor(Configuration configuration, Transaction transaction) {
  this.transaction = transaction;
  this.deferredLoads = new ConcurrentLinkedQueue<DeferredLoad>();
  this.localCache = new PerpetualCache("LocalCache");
  this.localOutputParameterCache = new PerpetualCache("LocalOutputParameterCache");
  this.closed = false;
  this.configuration = configuration;
  this.wrapper = this;
}
 
Example #16
Source File: PerpetualCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #17
Source File: SoftCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDemonstrateCopiesAreEqual() {
  Cache cache = new SoftCache(new PerpetualCache("default"));
  cache = new SerializedCache(cache);
  for (int i = 0; i < 1000; i++) {
    cache.putObject(i, i);
    Object value = cache.getObject(i);
    assertTrue(value == null || value.equals(i));
  }
}
 
Example #18
Source File: SoftCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveItemOnDemand() {
  Cache cache = new SoftCache(new PerpetualCache("default"));
  cache.putObject(0, 0);
  assertNotNull(cache.getObject(0));
  cache.removeObject(0);
  assertNull(cache.getObject(0));
}
 
Example #19
Source File: SoftCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFlushAllItemsOnDemand() {
  Cache cache = new SoftCache(new PerpetualCache("default"));
  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));
}
 
Example #20
Source File: FifoCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveFirstItemInBeyondFiveEntries() {
  FifoCache cache = new FifoCache(new PerpetualCache("default"));
  cache.setSize(5);
  for (int i = 0; i < 5; i++) {
    cache.putObject(i, i);
  }
  assertEquals(0, cache.getObject(0));
  cache.putObject(5, 5);
  assertNull(cache.getObject(0));
  assertEquals(5, cache.getSize());
}
 
Example #21
Source File: FifoCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveItemOnDemand() {
  FifoCache cache = new FifoCache(new PerpetualCache("default"));
  cache.putObject(0, 0);
  assertNotNull(cache.getObject(0));
  cache.removeObject(0);
  assertNull(cache.getObject(0));
}
 
Example #22
Source File: FifoCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFlushAllItemsOnDemand() {
  FifoCache cache = new FifoCache(new PerpetualCache("default"));
  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));
}
 
Example #23
Source File: WeakCacheTest.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFlushAllItemsOnDemand() {
  WeakCache cache = new WeakCache(new PerpetualCache("default"));
  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));
}
 
Example #24
Source File: PerpetualCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDemonstrateCopiesAreEqual() {
  Cache cache = new PerpetualCache("default");
  cache = new SerializedCache(cache);
  for (int i = 0; i < 1000; i++) {
    cache.putObject(i, i);
    assertEquals(i, cache.getObject(i));
  }
}
 
Example #25
Source File: PerpetualCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #26
Source File: PerpetualCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #27
Source File: LruCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveLeastRecentlyUsedItemInBeyondFiveEntries() {
  LruCache cache = new LruCache(new PerpetualCache("default"));
  cache.setSize(5);
  for (int i = 0; i < 5; i++) {
    cache.putObject(i, i);
  }
  assertEquals(0, cache.getObject(0));
  cache.putObject(5, 5);
  assertNull(cache.getObject(1));
  assertEquals(5, cache.getSize());
}
 
Example #28
Source File: LruCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRemoveItemOnDemand() {
  Cache cache = new LruCache(new PerpetualCache("default"));
  cache.putObject(0, 0);
  assertNotNull(cache.getObject(0));
  cache.removeObject(0);
  assertNull(cache.getObject(0));
}
 
Example #29
Source File: LruCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldFlushAllItemsOnDemand() {
  Cache cache = new LruCache(new PerpetualCache("default"));
  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));
}
 
Example #30
Source File: WeakCacheTest.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDemonstrateObjectsBeingCollectedAsNeeded() {
  final int N = 3000000;
  WeakCache cache = new WeakCache(new PerpetualCache("default"));
  for (int i = 0; i < N; i++) {
    cache.putObject(i, i);
    if (cache.getSize() < i + 1) {
      //System.out.println("Cache exceeded with " + (i + 1) + " entries.");
      break;
    }
  }
  assertTrue(cache.getSize() < N);
}