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

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

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

Cache.replace介绍

[英]Replaces the entry for a key only if currently mapped to some value and the entry is not expired.

This is equivalent to

V oldValue = cache.get(key); 
if (oldValue != null) { 
cache.put(key, value); 
} 
return oldValue;

except that the action is performed atomically.

Neither the key nor the value can be null.
[中]仅当当前映射到某个值且条目未过期时,才替换密钥的条目。
这相当于

V oldValue = cache.get(key); 
if (oldValue != null) { 
cache.put(key, value); 
} 
return oldValue;

除了操作是以原子方式执行之外。
键和值都不能为null。

代码示例

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

@Test
public void testReplace2ArgsWithNoCacheEntry_should_not_call_writer() throws Exception {
 doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2"));
 testCache.replace(1, "one#2");
}

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

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

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

@Test
public void testReplace3ArgsWithNotMatchingCacheEntry_should_not_call_writer() throws Exception {
 doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2"));
 testCache.put(1, "un");
 testCache.replace(1, "one", "one#2");
}

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

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

代码示例来源: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 testReplace2ArgsWithWriterException_should_call_writer() throws Exception {
 doThrow(new Exception("Mock Exception: cannot write 1")).when(cacheLoaderWriter).write(eq(1), eq("one#2"));
 testCache.put(1, "one");
 try {
  testCache.replace(1, "one#2");
  fail("expected CacheWritingException");
 } catch (CacheWritingException ex) {
  // expected
 }
}

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

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

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

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

@Test
@SuppressWarnings("unchecked")
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore() throws Exception {
 testCache.put(1, "un");
 reset(cacheLoaderWriter);
 assertThat(testCache.replace(1, "un", "one"), is(true));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
 verify(cacheLoaderWriter, times(1)).write(eq(1), eq("one"));
 verifyNoMoreInteractions(cacheLoaderWriter);
}

代码示例来源: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 replaceKON() {
 expect(cache.replace(1, "a", "b")).isFalse();
 changesOf(0, 1, 0, 0);
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 expect(cache.replace(1, "xxx", "b")).isFalse();
 changesOf(1, 0, 0, 0);
 expect(cache.replace(1, "a", "b")).isTrue();
 changesOf(1, 0, 1, 0);
}

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

@Test
@SuppressWarnings("unchecked")
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception {
 testCache.put(1, "un");
 reset(cacheLoaderWriter);
 assertThat(testCache.replace(1, "uno", "one"), is(false));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
 verifyZeroInteractions(cacheLoaderWriter);
}

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

@Test
public void replaceKON() {
 expect(cache.replace(1, "a", "b")).isFalse();
 changesOf(0, 1, 0, 0);
 cache.put(1, "a");
 changesOf(0, 0, 1, 0);
 expect(cache.replace(1, "xxx", "b")).isFalse();
 changesOf(0, 1, 0, 0); // FIXME: We have a hit on the cache but a miss on the store. Why?
 expect(cache.replace(1, "a", "b")).isTrue();
 changesOf(1, 0, 1, 0);
}

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

@Test
public void testSimpleReplace3Args() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 testCache.put(1, "one");
 assertThat(testCache.replace(1, "one_", "one@"), is(false));
 assertThat(testCache.replace(1, "one", "one#"), is(true));
 assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one#"));
 assertThat(testCache.replace(2, "two", "two#"), is(false));
}

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

@Test
public void testSimplePutWithExpiry_replace2Args() throws Exception {
 insert(testCache, getEntries());
 assertThat(cacheSize(testCache), is(2));
 manualTimeSource.setTimeMillis(1001);
 assertThat(testCache.replace(1, "one#2"), is(nullValue()));
 assertThat(testCache.get(1), is(nullValue()));
 assertThat(testCache.replace(2, "two#2"), is(nullValue()));
 assertThat(testCache.get(2), is(nullValue()));
}

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

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

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

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

相关文章