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

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

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

Cache.putIfAbsent介绍

[英]Maps the specified key to the specified value in this cache, unless a non-expired mapping already exists.

This is equivalent to

if (!cache.containsKey(key)) 
cache.put(key, value); 
return null; 
else 
return cache.get(key);

except that the action is performed atomically.

The value can be retrieved by calling the get method with a key that is equal to the original key.

Neither the key nor the value can be null.
[中]将指定的键映射到此缓存中的指定值,除非已存在未过期的映射。
这相当于

if (!cache.containsKey(key)) 
cache.put(key, value); 
return null; 
else 
return cache.get(key);

除了操作是以原子方式执行之外。
可以通过使用与原始键相等的键调用get方法来检索该值。
键和值都不能为null。

代码示例

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

@Override
 protected void insert(Cache<Number, CharSequence> testCache, Map<Number, CharSequence> entries) {
  for (Map.Entry<Number, CharSequence> entry : entries.entrySet()) {
   testCache.putIfAbsent(entry.getKey(), entry.getValue());
  }
 }
}

代码示例来源:origin: ben-manes/caffeine

@Override
public void record(long key) {
 Object value = cache.putIfAbsent(key, key);
 if (value == null) {
  size++;
  policyStats.recordMiss();
  if (size > maximumSize) {
   policyStats.recordEviction();
   size--;
  }
 } else {
  policyStats.recordHit();
 }
}

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

@Test
public void testPutIfAbsentWithWriterException_should_call_writer() throws Exception {
 doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one"));
 try {
  testCache.putIfAbsent(1, "one");
  fail("expected CacheWritingException");
 } catch (CacheWritingException ex) {
  // expected
 }
 testCache.put(2, "two");
 testCache.putIfAbsent(2, "two#2");
}

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

private void putIfAbsent(Cache<Long, String> cache, String value, boolean addToCacheRecords) {
 cache.putIfAbsent(KEY, value);
 if (addToCacheRecords) {
  cacheRecords.add(new Record(KEY, cache.get(KEY)));
 }
}

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

@Test
public void ttiExpiryPut() {
 Cache<Integer, String> cache = createCache(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMillis(100)));
 cache.put(1, "a");
 assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a");
 timeSource.setTimeMillis(100);
 assertThat(cache.putIfAbsent(1, "c")).isNull();
}

代码示例来源: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 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
@SuppressWarnings("unchecked")
public void testSimplePutIfAbsentWithLoaderAndWriter_existsInStore() throws Exception {
 testCache.put(1, "un");
 reset(cacheLoaderWriter);
 assertThat(testCache.putIfAbsent(1, "one"), Matchers.<CharSequence>equalTo("un"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verifyZeroInteractions(cacheLoaderWriter);
}

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

@Test
public void putIfAbsent() {
 expect(cache.putIfAbsent(1, "a")).isNull();
 changesOf(0, 1, 1, 0);
 expect(cache.putIfAbsent(1, "b")).isEqualTo("a");
 changesOf(1, 0, 0, 0);
}

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

@Test
public void testSimplePutIfAbsent() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 CharSequence one = testCache.putIfAbsent(1, "one");
 assertThat(one, is(nullValue()));
 CharSequence one_2 = testCache.putIfAbsent(1, "one#2");
 assertThat(one_2, Matchers.<CharSequence>equalTo("one"));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
}

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

@Test
public void putIfAbsent() {
 expect(cache.putIfAbsent(1, "a")).isNull();
 changesOf(0, 1, 1, 0);
 expect(cache.putIfAbsent(1, "b")).isEqualTo("a");
 changesOf(1, 0, 0, 0);
}

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

@Test
public void testSimplePutWithExpiry_putIfAbsent() throws Exception {
 insert(testCache, getEntries());
 assertThat(cacheSize(testCache), is(2));
 manualTimeSource.setTimeMillis(1001);
 assertThat(testCache.putIfAbsent(1, "one#2"), is(nullValue()));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one#2"));
 assertThat(testCache.putIfAbsent(2, "two#2"), is(nullValue()));
 assertThat(testCache.get(2), Matchers.<CharSequence>equalTo("two#2"));
}

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

@Test
public void putIfAbsent_absent() {
 createCacheManager();
 cache = createCache();
 assertThat(cache.putIfAbsent(1, "a")).isNull();
 assertThat(cache.get(1)).isEqualTo("a");
 changesOf(1, 1, 1, 0);
}

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

@Test
public void putIfAbsent_present() {
 createCacheManager();
 cache = createCache();
 cache.put(1, "a");
 assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a");
 changesOf(1, 0, 1, 0);
}

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

@Test
public void putIfAbsentLoaderWriter_absentAndLoaded() throws Exception {
 createCacheManager();
 CacheLoaderWriter<Integer, String> loader = mockLoader();
 when(loader.load(1)).thenReturn("a");
 CacheConfigurationBuilder<Integer, String> builder = baseConfig()
  .withLoaderWriter(loader);
 cache = createCache(builder);
 assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a");
 assertThat(cache.get(1)).isEqualTo("a");
 changesOf(2, 0, 0, 0);
}

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

@Test
public void noExpiryPut() {
 Cache<Integer, String> cache = createCache(ExpiryPolicyBuilder.noExpiration());
 cache.put(1, "a");
 timeSource.setTimeMillis(Long.MAX_VALUE);
 assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a");
}

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

@Test
public void putIfAbsentLoaderWriterNotAtomic_absent() throws Exception {
 createNotAtomicCacheManager();
 CacheLoaderWriter<Integer, String> loader = mockLoader();
 CacheConfigurationBuilder<Integer, String> builder = baseConfig()
  .withLoaderWriter(loader);
 cache = createCache(builder);
 assertThat(cache.putIfAbsent(1, "a")).isNull();
 verify(loader).write(1, "a");
 assertThat(cache.get(1)).isEqualTo("a");
 changesOf(1, 1, 1, 0);
}

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

@Test
public void putIfAbsentLoaderWriter_present() throws Exception {
 createCacheManager();
 CacheLoaderWriter<Integer, String> loader = mockLoader();
 CacheConfigurationBuilder<Integer, String> builder = baseConfig()
  .withLoaderWriter(loader);
 cache = createCache(builder);
 cache.put(1, "a");
 assertThat(cache.putIfAbsent(1, "b")).isEqualTo("a");
 verify(loader).write(1, "a");
 changesOf(1, 0, 1, 0);
}

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

@Test
public void putIfAbsent_presentButExpired() {
 createCacheManager();
 CacheConfigurationBuilder<Integer, String> builder = baseConfig()
  .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(10)));
 cache = createCache(builder);
 cache.put(1, "a");
 timeSource.advanceTime(15);
 assertThat(cache.putIfAbsent(1, "b")).isNull();
 assertThat(cache.get(1)).isEqualTo("b");
 changesOf(1, 1, 2, 0);
}

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

@Test
 public void testPutIfAbsentExpirationPropagatedToHigherTiers() throws CachePersistenceException {
  CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder = cacheManagerBuilder(oneSecondExpiration());

  try(PersistentCacheManager cacheManager = clusteredCacheManagerBuilder.build(true)) {
   Cache<Long, String> cache = cacheManager.getCache(CLUSTERED_CACHE, Long.class, String.class);
   cache.put(1L, "value"); // store on the cluster
   cache.putIfAbsent(1L, "newvalue"); // push it up on heap tier
   timeSource.advanceTime(1500); // go after expiration
   assertThat(cache.get(1L)).isEqualTo(null); // the value should have expired
  }
 }
}

相关文章