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

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

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

Cache.containsKey介绍

[英]Checks whether a mapping for the given key is present, without retrieving the associated value.
[中]检查给定键的映射是否存在,而不检索关联的值。

代码示例

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

@Override
 Boolean runTask() throws Exception {
  return cache.containsKey(2L);
 }
}.run();

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

@Test
public void testSimpleRemove2ArgsWithLoaderAndWriter_absent() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null);
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.remove(1, "one"), is(false));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_absent() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null);
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "one"), is(nullValue()));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_absent() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> null);
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "un", "one"), is(false));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimpleRemove2ArgsWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.remove(1, "un"), is(true));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).delete(eq(1));
}

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

@Test
public void testSimpleRemove2ArgsWithLoaderAndWriter_existsInSor_notEquals() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.remove(1, "one"), is(false));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimplePutIfAbsentWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimplePutIfAbsentWithLoaderAndWriter_absent() throws Exception {
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.putIfAbsent(1, "one"), is(nullValue()));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
}

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

@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "one"), Matchers.<CharSequence>equalTo("un"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
}

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

@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "un", "one"), is(true));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
}

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

@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor_notEquals() throws Exception {
 when(cacheLoaderWriter.load(eq(1))).thenAnswer((Answer) invocation -> "un");
 assertThat(testCache.containsKey(1), is(false));
 assertThat(testCache.replace(1, "uno", "one"), is(false));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verify(cacheLoaderWriter, times(1)).load(eq(1));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

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

@Test
public void testSimpleContainsKey() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 testCache.put(1, "one");
 assertThat(testCache.containsKey(1), is(true));
 assertThat(testCache.containsKey(2), is(false));
}

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

@Test
public void containsKey() {
 expect(cache.containsKey(1)).isFalse();
 changesOf(0, 0, 0, 0);
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 expect(cache.containsKey(1)).isTrue();
 changesOf(0, 0, 0, 0);
}

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

@Test
public void containsKey() {
 expect(cache.containsKey(1)).isFalse();
 changesOf(0, 0, 0, 0);
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 expect(cache.containsKey(1)).isTrue();
 changesOf(0, 0, 0, 0);
}

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

@Test
public void testContainsKeyExpiredTwoClients() {
 TestTimeSource timeSource = new TestTimeSource();
 TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(timeSource);
 final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
   commonClusteredCacheManagerBuilder.using(timeSourceConfiguration);
 final PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true);
 final PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true);
 final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class);
 final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class);
 assertThat(cache2.get(1L), nullValue());
 cache1.put(1L, "value1");
 assertThat(cache1.containsKey(1L), is(true));
 timeSource.advanceTime(1L);
 assertThat(cache1.containsKey(1L), is(false));
 assertThat(cache2.containsKey(1L), is(false));
 cacheManager2.close();
 cacheManager1.close();
}

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

assertThat(cache.containsKey(2L), is(false));
cache.put(2L, "The two");
assertThat(cache.containsKey(2L), is(true));
cache.put(1L, "Another one");
cache.put(3L, "The three");

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

assertEquals(1, listener2.removed.get());
cache.containsKey(1L);
assertEquals(3, listener1.created.get());
assertEquals(3, listener1.updated.get());

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

@Test
public void testTerminationThenContainsKey() throws Exception {
 CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
   CacheManagerBuilder.newCacheManagerBuilder()
     .with(ClusteringServiceConfigurationBuilder.cluster(cluster.getConnectionURI().resolve("/MyCacheManagerName"))
       .timeouts(TimeoutsBuilder.timeouts().read(Duration.of(1, ChronoUnit.SECONDS)).build())
       .autoCreate()
       .defaultServerResource("primary-server-resource"))
   .withCache("simple-cache",
     CacheConfigurationBuilder.newCacheConfigurationBuilder(Long.class, String.class,
       ResourcePoolsBuilder.newResourcePoolsBuilder()
         .with(ClusteredResourcePoolBuilder.clusteredDedicated(4, MemoryUnit.MB))));
 PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(false);
 cacheManager.init();
 Cache<Long, String> cache = cacheManager.getCache("simple-cache", Long.class, String.class);
 cache.put(1L, "un");
 cache.put(2L, "deux");
 cache.put(3L, "trois");
 assertThat(cache.containsKey(2L)).isTrue();
 cluster.getClusterControl().terminateAllServers();
 boolean value = new TimeLimitedTask<Boolean>(5, TimeUnit.SECONDS) {
  @Override
  Boolean runTask() throws Exception {
   return cache.containsKey(2L);
  }
 }.run();
 assertThat(value).isFalse();
}

代码示例来源:origin: nuls-io/nuls

public boolean containsKey(K key) {
  Cache cache = this.cacheManager.getCache(cacheName);
  if (cache == null) {
    return false;
  }
  return cache.containsKey(key);
}

代码示例来源:origin: yggdrash/yggdrash

@Override
public boolean contains(Sha3Hash key) {
  return pendingPool.containsKey(key) || db.get(key.getBytes()) != null;
}

相关文章