org.ehcache.Cache.clear()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(13.1k)|赞(0)|评价(0)|浏览(228)

本文整理了Java中org.ehcache.Cache.clear()方法的一些代码示例,展示了Cache.clear()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.clear()方法的具体详情如下:
包路径:org.ehcache.Cache
类名称:Cache
方法名:clear

Cache.clear介绍

[英]Removes all mappings currently present in the Cache.

It does so without invoking the CacheLoaderWriter or any registered org.ehcache.event.CacheEventListener instances. This is not an atomic operation and can potentially be very expensive.
[中]删除缓存中当前存在的所有映射。
它这样做时不调用CacheLoaderWriter或任何已注册的组织。ehcache。事件CacheEventListener实例这不是一个原子操作,可能非常昂贵

代码示例

代码示例来源:origin: ehcache/ehcache3

@Exposed
public void clear() {
 cacheBinding.getCache().clear();
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testClear() throws Exception {
 List<Cache<Long, String>> caches = new ArrayList<>();
 caches.add(CACHE1);
 caches.add(CACHE2);
 Map<Long, String> entriesMap = new HashMap<>();
 entriesMap.put(1L, "one");
 entriesMap.put(2L, "two");
 entriesMap.put(3L, "three");
 entriesMap.put(4L, "four");
 entriesMap.put(5L, "five");
 entriesMap.put(6L, "six");
 caches.forEach(cache -> cache.putAll(entriesMap));
 Set<Long> keySet = entriesMap.keySet();
 caches.forEach(cache -> {
  Map<Long, String> all = cache.getAll(keySet);
  assertThat(all.get(1L), is("one"));
  assertThat(all.get(2L), is("two"));
  assertThat(all.get(3L), is("three"));
  assertThat(all.get(4L), is("four"));
  assertThat(all.get(5L), is("five"));
  assertThat(all.get(6L), is("six"));
 });
 CACHE1.clear();
 CACHE2.clear();
 CLUSTER.getClusterControl().terminateActive();
 keySet.forEach(x -> assertThat(CACHE1.get(x), nullValue()));
 keySet.forEach(x -> assertThat(CACHE2.get(x), nullValue()));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testClusteredWriteBehindLoading() {
 try (CacheManager cacheManager = createCacheManager()) {
  Cache<Long, String> cache = cacheManager.getCache(CACHE_NAME, Long.class, String.class);
  put(cache, "Some value");
  tryFlushingUpdatesToSOR(cache);
  cache.clear();
  assertThat(cache.get(KEY), notNullValue());
  cache.clear();
 }
}

代码示例来源:origin: ehcache/ehcache3

@Test(timeout=180000)
public void testClear() throws Exception {
 List<Cache<Long, BlobValue>> caches = new ArrayList<>();
 caches.add(CACHE1);
 caches.add(CACHE2);
 Map<Long, BlobValue> entriesMap = new HashMap<>();
 Random random = new Random();
 LongStream longStream = random.longs(1000);
 longStream.forEach(x -> entriesMap.put(x, new BlobValue()));
 caches.forEach(cache -> cache.putAll(entriesMap));
 Set<Long> keySet = entriesMap.keySet();
 Set<Long> readKeysByCache2BeforeFailOver = new HashSet<>();
 keySet.forEach(x -> {
  if (CACHE2.get(x) != null) {
   readKeysByCache2BeforeFailOver.add(x);
  }
 });
 CACHE1.clear();
 CLUSTER.getClusterControl().terminateActive();
 if (cacheConsistency == Consistency.STRONG) {
  readKeysByCache2BeforeFailOver.forEach(x -> assertThat(CACHE2.get(x), nullValue()));
 } else {
  readKeysByCache2BeforeFailOver.forEach(x -> assertThat(CACHE1.get(x), nullValue()));
 }
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testBasicClusteredWriteBehind() {
 try (PersistentCacheManager cacheManager = createCacheManager()) {
  Cache<Long, String> cache = cacheManager.getCache(CACHE_NAME, Long.class, String.class);
  for (int i = 0; i < 10; i++) {
   put(cache, String.valueOf(i));
  }
  assertValue(cache, String.valueOf(9));
  verifyRecords(cache);
  cache.clear();
 }
}

代码示例来源:origin: ehcache/ehcache3

@Test
 public void testClusteredWriteBehindLoading() throws Exception {
  cache.put(KEY, "Some value");
  checkValueFromLoaderWriter(cache, "Some value");
  cache.clear();

  assertThat(cache.get(KEY), notNullValue());

  doThreadDump = false;
 }
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testClear() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 testCache.put(1, "one");
 testCache.put(2, "two");
 testCache.clear();
 assertThat(testCache.get(1), is(nullValue()));
 assertThat(testCache.get(2), is(nullValue()));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testBulkOps() {
 TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter();
 CacheConfiguration<Long, String> cacheConfiguration = getCacheConfiguration(loaderWriter);
 CacheManager cacheManager = CacheManagerBuilder
     .newCacheManagerBuilder()
     .with(cluster(CLUSTER_URI).autoCreate())
     .withCache("cache-1", cacheConfiguration)
     .build(true);
 Cache<Long, String> cache = cacheManager.getCache("cache-1", Long.class, String.class);
 Map<Long, String> mappings = new HashMap<>();
 for (int i = 1; i <= 5; i++) {
  mappings.put((long) i, "" + i);
 }
 cache.putAll(mappings);
 assertThat(loaderWriter.storeMap.keySet(), containsInAnyOrder(mappings.keySet().toArray()));
 cache.clear();
 Map<Long, String> loadedData = cache.getAll(mappings.keySet());
 assertThat(mappings.keySet(), containsInAnyOrder(loadedData.keySet().toArray()));
 cache.removeAll(mappings.keySet());
 assertThat(loaderWriter.storeMap.isEmpty(), is(true));
}

代码示例来源:origin: ehcache/ehcache3

@Ignore("This is currently unstable as if the clear does not complete before the failover," +
    "there is no future operation that will trigger the code in ClusterTierActiveEntity.invokeServerStoreOperation" +
    "dealing with in-flight invalidation reconstructed from reconnect data")
@Test(timeout=180000)
public void testClear() throws Exception {
 List<Future<?>> futures = new ArrayList<>();
 Set<Long> universalSet = ConcurrentHashMap.newKeySet();
 caches.forEach(cache -> {
  for (int i = 0; i < NUM_OF_THREADS; i++) {
   Map<Long, BlobValue> map = random.longs().limit(JOB_SIZE).collect(HashMap::new, (hashMap, x) -> hashMap.put(x, new BlobValue()), HashMap::putAll);
   futures.add(executorService.submit(() -> {
    cache.putAll(map);
    universalSet.addAll(map.keySet());
   }));
  }
 });
 drainTasks(futures);
 universalSet.forEach(x -> {
  CACHE1.get(x);
  CACHE2.get(x);
 });
 Future<?> clearFuture = executorService.submit(() -> CACHE1.clear());
 CLUSTER.getClusterControl().terminateActive();
 clearFuture.get();
 universalSet.forEach(x -> assertThat(CACHE2.get(x), nullValue()));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void clear() {
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 cache.clear();
 changesOf(0, 0, 0, 0);
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void clear() {
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 cache.clear();
 changesOf(0, 0, 0, 0);
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testAtomicsWithoutLoaderWriter() throws Exception {
 CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
   newResourcePoolsBuilder()
         .heap(10, EntryUnit.ENTRIES)
         .offheap(10, MemoryUnit.MB)
       )
   .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(1)));
 cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
   .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
   .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
   .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
   .build(true);
 Cache<Long, String> txCache1 = cacheManager.getCache("txCache1", Long.class, String.class);
 putIfAbsentAssertions(transactionManager, txCache1);
 txCache1.clear();
 remove2ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
 replace2ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
 replace3ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
}

代码示例来源:origin: ehcache/ehcache3

assertThat(cache.get(1L), is(nullValue()));
cache.clear();
assertThat(cache.get(1L), is(nullValue()));
assertThat(cache.get(2L), is(nullValue()));

代码示例来源:origin: javalite/activejdbc

private void purgeGroup(String group) {
  final Cache<String, Object> cache = cacheManager.getCache(group, String.class, Object.class);
  if (cache != null) {
    cache.clear();
  }
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testAtomicsWithLoaderWriter() throws Exception {
 SampleLoaderWriter<Long, String> loaderWriter = new SampleLoaderWriter<>();
 CacheConfigurationBuilder<Long, String> cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
   newResourcePoolsBuilder()
         .heap(10, EntryUnit.ENTRIES)
         .offheap(10, MemoryUnit.MB))
   .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(1)))
   .withLoaderWriter(loaderWriter);
 cacheManager = CacheManagerBuilder.newCacheManagerBuilder()
   .withCache("txCache1", cacheConfigurationBuilder.add(new XAStoreConfiguration("txCache1")).build())
   .using(new DefaultTimeSourceService(new TimeSourceConfiguration(testTimeSource)))
   .using(new LookupTransactionManagerProviderConfiguration(BitronixTransactionManagerLookup.class))
   .build(true);
 Cache<Long, String> txCache1 = cacheManager.getCache("txCache1", Long.class, String.class);
 putIfAbsentAssertions(transactionManager, txCache1);
 txCache1.clear();
 loaderWriter.clear();
 remove2ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
 loaderWriter.clear();
 replace2ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
 loaderWriter.clear();
 replace3ArgsAssertions(transactionManager, txCache1);
 txCache1.clear();
 loaderWriter.clear();
}

代码示例来源:origin: ehcache/ehcache3

cache.clear();

代码示例来源:origin: ehcache/ehcache3

@Test
public void updateResourcesAtRuntime() throws InterruptedException {
 ListenerObject listener = new ListenerObject();
 CacheEventListenerConfigurationBuilder cacheEventListenerConfiguration = CacheEventListenerConfigurationBuilder
  .newEventListenerConfiguration(listener, EventType.EVICTED).unordered().synchronous();
 CacheConfiguration<Long, String> cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
  ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10L, EntryUnit.ENTRIES))
  .add(cacheEventListenerConfiguration)
  .build();
 CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().withCache("cache", cacheConfiguration)
  .build(true);
 Cache<Long, String> cache = cacheManager.getCache("cache", Long.class, String.class);
 for(long i = 0; i < 20; i++ ){
  cache.put(i, "Hello World");
 }
 assertThat(listener.evicted(), is(10));
 cache.clear();
 listener.resetEvictionCount();
 // tag::updateResourcesAtRuntime[]
 ResourcePools pools = ResourcePoolsBuilder.newResourcePoolsBuilder().heap(20L, EntryUnit.ENTRIES).build(); // <1>
 cache.getRuntimeConfiguration().updateResourcePools(pools); // <2>
 assertThat(cache.getRuntimeConfiguration().getResourcePools()
  .getPoolForResource(ResourceType.Core.HEAP).getSize(), is(20L));
 // end::updateResourcesAtRuntime[]
 for(long i = 0; i < 20; i++ ){
  cache.put(i, "Hello World");
 }
 assertThat(listener.evicted(), is(0));
 cacheManager.close();
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testWriteBehindMultipleClients() {
 try (PersistentCacheManager cacheManager1 = createCacheManager();
    PersistentCacheManager cacheManager2 = createCacheManager()) {
  Cache<Long, String> client1 = cacheManager1.getCache(CACHE_NAME, Long.class, String.class);
  Cache<Long, String> client2 = cacheManager2.getCache(CACHE_NAME, Long.class, String.class);
  put(client1, "The one from client1");
  put(client2, "The one one from client2");
  assertValue(client1, "The one one from client2");
  remove(client1);
  put(client2, "The one from client2");
  put(client1, "The one one from client1");
  assertValue(client2, "The one one from client1");
  remove(client2);
  assertValue(client1, null);
  put(client1, "The one from client1");
  put(client1, "The one one from client1");
  remove(client2);
  put(client2, "The one from client2");
  put(client2, "The one one from client2");
  remove(client1);
  assertValue(client2, null);
  verifyRecords(client1);
  client1.clear();
 }
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testClusteredWriteBehindCAS() {
 try (PersistentCacheManager cacheManager = createCacheManager()) {
  Cache<Long, String> cache = cacheManager.getCache(CACHE_NAME, Long.class, String.class);
  putIfAbsent(cache, "First value", true);
  assertValue(cache, "First value");
  putIfAbsent(cache, "Second value", false);
  assertValue(cache, "First value");
  put(cache, "First value again");
  assertValue(cache, "First value again");
  replace(cache, "Replaced First value", true);
  assertValue(cache, "Replaced First value");
  replace(cache, "Replaced First value", "Replaced First value again", true);
  assertValue(cache, "Replaced First value again");
  replace(cache, "Replaced First", "Tried Replacing First value again", false);
  assertValue(cache, "Replaced First value again");
  condRemove(cache, "Replaced First value again", true);
  assertValue(cache, null);
  replace(cache, "Trying to replace value", false);
  assertValue(cache, null);
  put(cache, "new value", true);
  assertValue(cache, "new value");
  condRemove(cache, "new value", false);
  verifyRecords(cache);
  cache.clear();
 }
}

代码示例来源:origin: org.javalite/activejdbc

private void purgeGroup(String group) {
  final Cache<String, Object> cache = cacheManager.getCache(group, String.class, Object.class);
  if (cache != null) {
    cache.clear();
  }
}

相关文章