Java Code Examples for org.cache2k.Cache#close()

The following examples show how to use org.cache2k.Cache#close() . 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: DateFormattingBenchmark.java    From cache2k-benchmark with Apache License 2.0 6 votes vote down vote up
/**
 * Work with cache key object and one big cache.
 */
@Test
public void testWithCacheAndKeyObject() {
  Cache<CacheKey, String> c =
    Cache2kBuilder.of(CacheKey.class, String.class)
      .eternal(true)
      .loader(new CacheLoader<CacheKey, String>() {
        @Override
        public String load(CacheKey o) {
          DateFormat df = DateFormat.getDateInstance(o.format, o.locale);
          return df.format(o.date);
        }
      })
      .build();
  PrintWriter w = new PrintWriter(new CharArrayWriter());
  List<Date> l = provideListWith3MillionDates();
  for (Date d : l) {
    w.print(c.get(new CacheKey(Locale.FRANCE, DateFormat.LONG, d)));
  }
  c.close();
}
 
Example 2
Source File: ListenerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void customExecutor() {
  final AtomicInteger _counter = new AtomicInteger();
  Cache<Integer, Integer> c =
    Cache2kBuilder.of(Integer.class, Integer.class)
      .addAsyncListener(new CacheEntryCreatedListener<Integer, Integer>() {
        @Override
        public void onEntryCreated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> entry) {
        }
      })
      .asyncListenerExecutor(new Executor() {
        @Override
        public void execute(final Runnable command) {
          _counter.incrementAndGet();
        }
      })
      .build();
  c.put(1,2);
  c.close();
  assertEquals(1, _counter.get());
}
 
Example 3
Source File: OsgiIT.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Simple test to see whether event package is exported.
 */
@Test
public void testEventPackage() {
  CacheManager m = CacheManager.getInstance("testEventPackage");
  final AtomicInteger _count = new AtomicInteger();
  Cache<String, String> c =
    new Cache2kBuilder<String, String>() {}
      .manager(m)
      .eternal(true)
      .addListener(new CacheEntryCreatedListener<String, String>() {
        @Override
        public void onEntryCreated(final Cache<String, String> cache, final CacheEntry<String, String> entry) {
          _count.incrementAndGet();
        }
      })
      .build();
  c.put("abc", "123");
  assertTrue(c.containsKey("abc"));
  assertEquals("123", c.peek("abc"));
  assertEquals(1, _count.get());
  c.close();
}
 
Example 4
Source File: CacheTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetWithSource() {
  CacheLoader<String,Integer> _lengthCountingSource = new CacheLoader<String, Integer>() {
    @Override
    public Integer load(String o) {
      return o.length();
    }
  };
  Cache<String,Integer> c =
    Cache2kBuilder.of(String.class, Integer.class)
      .loader(_lengthCountingSource)
      .eternal(true)
      .build();
  int v = c.get("hallo");
  assertEquals(5, v);
  v = c.get("long string");
  assertEquals(11, v);
  c.close();
}
 
Example 5
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void legalCharacterInCacheName() {
  String _legalChars = ".~,@ ()";
  _legalChars += "$-_abcABC0123";
  Cache c = Cache2kBuilder.forUnknownTypes()
    .name(_legalChars)
    .build();
  c.close();
}
 
Example 6
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void collectionValueType() {
  Cache<Long, List<String>> c =
    new Cache2kBuilder<Long, List<String>>() {}
      .eternal(true)
      .build();
  c.close();
}
 
Example 7
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheNameForAnnotationDefault() {
  Cache<Long, List<String>> c =
    (Cache<Long, List<String>>)
      Cache2kBuilder.forUnknownTypes()
        .eternal(true)
        .name("package.name.ClassName.methodName(package.ParameterType,package.ParameterType")
        .build();
  c.close();
}
 
Example 8
Source File: CacheManagerAndCacheLifeCycleTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void testToStringAnon() {
  Cache<Integer, Integer> c =
    Cache2kBuilder.of(Integer.class, Integer.class)
      .eternal(true)
      .build();
  c.toString();
  c.close();
}
 
Example 9
Source File: IntegrationTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void notPresent() {
  Cache c = new Cache2kBuilder<String, String>() { }
    .manager(CacheManager.getInstance("notPresent"))
    .name("anyCache")
    .build();
  c.close();
}
 
Example 10
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * When using classes for the type information, there is no generic type information.
 * There is an ugly cast to (Object) needed to strip the result and be able to cast
 * to the correct one.
 */
@Test
public void collectionValueClass() {
  Cache<Long, List<String>> c =
    (Cache<Long, List<String>>) (Object) Cache2kBuilder.of(Long.class, List.class).eternal(true).build();
  c.put(123L, new ArrayList<String>());
  c.close();
}
 
Example 11
Source File: XmlConfigurationTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void sectionIsThereViaStandardElementName() {
  Cache2kBuilder<String, String> b =
    new Cache2kBuilder<String, String>(){}
      .manager(CacheManager.getInstance(MANAGER_NAME))
      .name("withJCacheSection");
  Cache2kConfiguration<String, String> cfg = b.toConfiguration();
  Cache<String, String> c = b.build();
  assertEquals("default is false", false, new JCacheConfiguration().isCopyAlwaysIfRequested());
  assertNotNull("section present", cfg.getSections().getSection(JCacheConfiguration.class));
  assertEquals("config applied", true, cfg.getSections().getSection(JCacheConfiguration.class).isCopyAlwaysIfRequested());
  c.close();
}
 
Example 12
Source File: IntegrationTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected = ConfigurationException.class)
public void illegalBoolean() {
  Cache c =
    new Cache2kBuilder<String, String>() { }
      .manager(CacheManager.getInstance("specialCases"))
      .name("illegalBoolean")
      .build();
  c.close();
}
 
Example 13
Source File: CacheManagerInitTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * After the cache manager is closed, creating a cache, will create a new cache manager.
 */
@Test
public void closeAll() {
  CacheManager cm1 = CacheManager.getInstance();
  CacheManager.closeAll();
  Cache c = Cache2kBuilder.forUnknownTypes().name("xy").build();
  assertNotSame(cm1, c.getCacheManager());
  c.close();
}
 
Example 14
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void illegalCharacterInCacheName_unsafeSet() {
  for (char c : ILLEGAL_CHARACTERS_IN_NAME.toCharArray()) {
    try {
      Cache _cache = Cache2kBuilder.forUnknownTypes()
        .name("illegalCharName" + c)
        .build();
      _cache.close();
      fail("Expect exception for illegal name in character '" + c + "', code " + ((int) c));
    } catch (IllegalArgumentException ex) {
    }
  }
}
 
Example 15
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void refreshAheadButNoExpiry() {
  Cache c = Cache2kBuilder.forUnknownTypes()
    .loader(new FunctionalCacheLoader() {
      @Override
      public Object load(final Object key) throws Exception {
        return null;
      }
    })
    .refreshAhead(true).build();
  c.close();
}
 
Example 16
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void cacheNameInClassConstructor0() {
  Cache c = BuildCacheInClassConstructor0.cache;
  assertThat(c.getName(),
    startsWith("_" + CLASSNAME + "$BuildCacheInClassConstructor0.CLINIT"));
  c.close();
}
 
Example 17
Source File: BasicCacheTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public void testPeekAndPut() {
  Cache<String,String> c =
    Cache2kBuilder.of(String.class, String.class)
      .eternal(true)
      .build();
  String val = c.peek("something");
  c.put("something", "hello");
  val = c.get("something");
  c.close();
}
 
Example 18
Source File: Cache2kBuilderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Check that long is getting through completely.
 */
@Test
public void cacheCapacityUnlimitedLongMaxValue() {
  Cache c0 = Cache2kBuilder.forUnknownTypes()
    .entryCapacity(Long.MAX_VALUE)
    .build();
  assertEquals(Long.MAX_VALUE, latestInfo(c0).getHeapCapacity());
  c0.close();
}
 
Example 19
Source File: IntegrationTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void noManagerConfigurationAndBuild() {
  Cache c = new Cache2kBuilder<String, String>() { }
    .manager(CacheManager.getInstance("noManager"))
    .entryCapacity(1234)
    .build();
  c.close();
}
 
Example 20
Source File: JmxSupportTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void testInitialProperties() throws Exception {
  Date _beforeCreateion = new Date();
  String _name = getClass().getName() + ".testInitialProperties";
  Cache c = new Cache2kBuilder<Long, List<Collection<Long>>> () {}
    .name(_name)
    .eternal(true)
    .enableJmx(true)
    .build();
  objectName = constructCacheObjectName(_name);
  checkAttribute("KeyType", "Long");
  checkAttribute("ValueType", "java.util.List<java.util.Collection<Long>>");
  checkAttribute("Size", 0L);
  checkAttribute("EntryCapacity", 2000L);
  checkAttribute("MaximumWeight", -1L);
  checkAttribute("CurrentWeight", 0L);
  checkAttribute("InsertCount", 0L);
  checkAttribute("MissCount", 0L);
  checkAttribute("RefreshCount", 0L);
  checkAttribute("RefreshFailedCount", 0L);
  checkAttribute("RefreshedHitCount", 0L);
  checkAttribute("ExpiredCount", 0L);
  checkAttribute("EvictedCount", 0L);
  checkAttribute("PutCount", 0L);
  checkAttribute("RemoveCount", 0L);
  checkAttribute("ClearedEntriesCount", 0L);
  checkAttribute("ClearCount", 0L);
  checkAttribute("KeyMutationCount", 0L);
  checkAttribute("LoadExceptionCount", 0L);
  checkAttribute("SuppressedLoadExceptionCount", 0L);
  checkAttribute("HitRate", 0.0);
  checkAttribute("HashQuality", 100);
  checkAttribute("MillisPerLoad", 0.0);
  checkAttribute("TotalLoadMillis", 0L);
  checkAttribute("Implementation", "LongHeapCache");
  checkAttribute("ClearedTime", null);
  checkAttribute("Alert", 0);
  assertTrue("reasonable CreatedTime", ((Date) retrieve("CreatedTime")).compareTo(_beforeCreateion) >= 0);
  assertTrue("reasonable InfoCreatedTime", ((Date) retrieve("InfoCreatedTime")).compareTo(_beforeCreateion) >= 0);
  assertTrue("reasonable InfoCreatedDeltaMillis", ((Integer) retrieve("InfoCreatedDeltaMillis")) >= 0);
  assertTrue("reasonable EvictionStatistics", retrieve("EvictionStatistics").toString().contains("impl="));
  assertTrue("reasonable IntegrityDescriptor", retrieve("IntegrityDescriptor").toString().startsWith("0."));
  c.close();
}