本文整理了Java中javax.cache.Cache.replace()
方法的一些代码示例,展示了Cache.replace()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.replace()
方法的具体详情如下:
包路径:javax.cache.Cache
类名称:Cache
方法名:replace
[英]Atomically replaces the entry for a key only if currently mapped to some value.
This is equivalent to
if (cache.containsKey(key)) {
cache.put(key, value);
return true;
} else {
return false;
}
except that the action is performed atomically.
[中]仅当当前映射到某个值时,才会自动替换键的条目。
这相当于
if (cache.containsKey(key)) {
cache.put(key, value);
return true;
} else {
return false;
}
除了操作是以原子方式执行之外。
代码示例来源:origin: killbill/killbill
@Override
public V put(final K key, final V value) throws CacheException {
V previousValue;
while (true) {
previousValue = cache.get(key);
if (previousValue == null) {
if (cache.putIfAbsent(key, value)) {
break;
}
} else {
if (cache.replace(key, value)) {
break;
}
}
}
return previousValue;
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_absent() throws Exception {
when(cacheLoader.load(eq(1))).thenAnswer(invocation -> null);
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.replace(1, "un", "one"), is(false));
verifyZeroInteractions(cacheLoader);
verifyZeroInteractions(cacheWriter);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_absent() throws Exception {
when(cacheLoader.load(eq(1))).thenAnswer(invocation -> null);
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.replace(1, "one"), is(false));
assertThat(testCache.containsKey(1), is(false));
verifyZeroInteractions(cacheLoader);
verifyZeroInteractions(cacheWriter);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInSor() throws Exception {
when(cacheLoader.load(eq(1))).thenAnswer(invocation -> "un");
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.replace(1, "one"), is(false));
assertThat(testCache.containsKey(1), is(false));
verifyZeroInteractions(cacheLoader);
verifyZeroInteractions(cacheWriter);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInSor() throws Exception {
when(cacheLoader.load(eq(1))).thenAnswer(invocation -> "un");
assertThat(testCache.containsKey(1), is(false));
assertThat(testCache.replace(1, "un", "one"), is(false));
assertThat(testCache.containsKey(1), is(false));
verifyZeroInteractions(cacheLoader);
verifyZeroInteractions(cacheWriter);
}
代码示例来源: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
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore_notEquals() throws Exception {
testCache.put(1, "un");
reset(cacheWriter);
assertThat(testCache.replace(1, "uno", "one"), is(false));
assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("un"));
verifyZeroInteractions(cacheLoader);
verifyZeroInteractions(cacheWriter);
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void replaceKV() {
expect(cache.replace(1, "a")).isFalse();
changesOf(0, 1, 0, 0);
cache.put(1, "a");
changesOf(0, 0, 1, 0);
expect(cache.replace(1, "b")).isTrue();
changesOf(1, 0, 1, 0);
}
代码示例来源:origin: apache/ignite
/**
* Tries to set the global cleanup node id to current node.
*
* @param grid Grid.
* @param metaCache Meta cache.
*
* @return True if successfully set the flag indicating that current node performs the cleanup; otherwise false.
*/
private boolean trySetGlobalCleanupFlag(Ignite grid, final Cache<CleanupNodeId, UUID> metaCache) {
final UUID localNodeId = grid.cluster().localNode().id();
while (true) {
// Get the node performing cleanup.
UUID nodeId = metaCache.get(CLEANUP_NODE_ID);
if (nodeId == null) {
if (metaCache.putIfAbsent(CLEANUP_NODE_ID, localNodeId))
return true; // Successfully reserved cleanup to local node.
// Failed putIfAbsent: someone else may have started cleanup. Retry the check.
continue;
}
if (nodeId.equals(localNodeId))
return false; // Current node already performs cleanup.
if (grid.cluster().node(nodeId) != null)
return false; // Another node already performs cleanup and is alive.
// Node that performs cleanup has disconnected.
if (metaCache.replace(CLEANUP_NODE_ID, nodeId, localNodeId))
return true; // Successfully replaced disconnected node id with our id.
// Replace failed: someone else started cleanup.
return false;
}
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace3ArgsWithLoaderAndWriter_existsInStore() throws Exception {
testCache.put(1, "un");
reset(cacheWriter);
assertThat(testCache.replace(1, "un", "one"), is(true));
assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
verifyZeroInteractions(cacheLoader);
verify(cacheWriter, times(1)).write(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one"));
}
代码示例来源:origin: ehcache/ehcache3
@Test
public void testSimpleReplace2ArgsWithLoaderAndWriter_existsInStore() throws Exception {
testCache.put(1, "un");
reset(cacheWriter);
assertThat(testCache.replace(1, "one"), is(true));
assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
verifyZeroInteractions(cacheLoader);
verify(cacheWriter, times(1)).write(new Eh107CacheLoaderWriter.Entry<Number, CharSequence>(1, "one"));
}
代码示例来源:origin: apache/cxf
@Override
protected void linkRefreshTokenToAccessToken(RefreshToken rt, ServerAccessToken at) {
super.linkRefreshTokenToAccessToken(rt, at);
if (!isStoreJwtTokenKeyOnly()) {
accessTokenCache.replace(at.getTokenKey(), at);
}
}
代码示例来源:origin: cache2k/cache2k
@Override
public boolean replace(K key, V oldValue, V newValue) {
return cache.replace(
passingKeyTransformer.compact(key),
passingValueTransformer.compact(oldValue),
valueTransformer.compact(newValue));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_3arg() throws Exception {
long now = System.currentTimeMillis();
Date key = new Date(now);
Date value = new Date(now);
cache.put(key, value);
Date nextValue = new Date(now + 1);
assertTrue(cache.replace(key, value, nextValue));
assertSame(nextValue, cache.get(key));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_3arg_NullKey() {
try {
assertFalse(cache.replace(null, "1", "2"));
fail("should have thrown an exception - null key not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_2arg_NullKey() {
try {
assertFalse(cache.replace(null, ""));
fail("should have thrown an exception - null key not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_2arg_NullValue() {
try {
assertFalse(cache.replace(1L, null));
fail("should have thrown an exception - null value not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_3arg_NullValue2() {
try {
assertFalse(cache.replace(1L, "1", null));
fail("should have thrown an exception - null value not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void replace_3arg() throws Exception {
Long key = System.currentTimeMillis();
String value = "value" + key;
cache.put(key, value);
String nextValue = "value" + key + 1;
assertTrue(cache.replace(key, value, nextValue));
assertEquals(nextValue, cache.get(key));
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doReplace(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(null, readCache.get(key));
assertFalse(writeCache.replace(key, "xxx"));
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals("one", readCache.get(key));
assertTrue(writeCache.replace(key, "uno"));
assertEquals("uno", readCache.get(key));
assertTrue(writeCache.remove(key));
assertEquals(null, readCache.get(key));
}
内容来源于网络,如有侵权,请联系作者删除!