本文整理了Java中javax.cache.Cache.getAndPut()
方法的一些代码示例,展示了Cache.getAndPut()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.getAndPut()
方法的具体详情如下:
包路径:javax.cache.Cache
类名称:Cache
方法名:getAndPut
[英]Associates the specified value with the specified key in this cache, returning an existing value if one existed.
If the cache previously contained a mapping for the key, the old value is replaced by the specified value. (A cache c is said to contain a mapping for a key k if and only if #containsKey(Object) would return true.)
The previous value is returned, or null if there was no value associated with the key previously.
[中]将指定值与此缓存中的指定键关联,如果存在,则返回现有值。
如果缓存以前包含键的映射,则旧值将替换为指定值。(缓存c被称为包含密钥k的映射,当且仅当#containsKey(Object)返回true。)
返回上一个值,如果以前没有与键关联的值,则返回null。
代码示例来源:origin: ehcache/ehcache3
@Test
public void getAndPut() {
expect(cache.getAndPut(1, "a")).isNull();
changesOf(0, 1, 1, 0);
cache.getAndPut(1, "b");
changesOf(1, 0, 1, 0);
cache.getAndPut(1, "b"); // again with the same value
changesOf(1, 0, 1, 0);
}
代码示例来源:origin: org.apache.camel/camel-jcache
@Override
void execute(Cache<Object, Object> cache, Exchange exchange) {
exchange.getIn().setBody(
cache.getAndPut(
exchange.getIn().getHeader(JCacheConstants.KEY),
exchange.getIn().getBody())
);
}
},
代码示例来源:origin: org.apache.camel/camel-jcache
@Override
public Exchange add(CamelContext camelContext, String key, Exchange exchange) {
if (optimistic) {
throw new UnsupportedOperationException();
}
LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
DefaultExchangeHolder oldHolder = cache.getAndPut(key, newHolder);
return unmarshallExchange(camelContext, oldHolder);
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_Existing_MutateValue() {
long now = System.currentTimeMillis();
Date existingKey = new Date(now);
Date value1 = new Date(now);
cache.getAndPut(existingKey, value1);
Date value2 = new Date(now + 1);
value1.setTime(now + 2);
assertEquals(new Date(now), cache.getAndPut(existingKey, value2));
value2.setTime(now + 3);
assertEquals(new Date(now + 1), cache.get(existingKey));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_Existing() {
long now = System.currentTimeMillis();
Date existingKey = new Date(now);
Date value1 = new Date(now);
cache.getAndPut(existingKey, value1);
Date value2 = new Date(now + 1);
assertSame(value1, cache.getAndPut(existingKey, value2));
assertSame(value2, cache.get(existingKey));
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doGetAndPut(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals("one", writeCache.getAndPut(key, "uno"));
assertEquals("uno", readCache.get(key));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_NotThere() {
if (cache == null) return;
long now = System.currentTimeMillis();
Date existingKey = new Date(now);
Date existingValue = new Date(now);
assertNull(cache.getAndPut(existingKey, existingValue));
existingValue.setTime(now + 1);
assertEquals(new Date(now), cache.get(existingKey));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_Existing() throws Exception {
Long existingKey = System.currentTimeMillis();
String value1 = "value1";
cache.getAndPut(existingKey, value1);
String value2 = "value2";
assertEquals(value1, cache.getAndPut(existingKey, value2));
assertEquals(value2, cache.get(existingKey));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_NullKey() throws Exception {
try {
cache.getAndPut(null, "");
fail("should have thrown an exception - null key not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_NullValue() throws Exception {
try {
cache.getAndPut(1L, null);
fail("should have thrown an exception - null value not allowed");
} catch (NullPointerException e) {
//good
}
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_NotThere() {
long now = System.currentTimeMillis();
Date existingKey = new Date(now);
Date existingValue = new Date(now);
assertNull(cache.getAndPut(existingKey, existingValue));
// unnecessary since after we test for same (not equals), but "advertises" consequence
existingValue.setTime(now + 1);
assertSame(existingValue, cache.get(existingKey));
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doPutGet(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals("one", readCache.get(key));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_Existing_NonSameKey() throws Exception {
Long key1 = System.currentTimeMillis();
String value1 = "value1";
assertNull(cache.getAndPut(key1, value1));
Long key2 = new Long(key1);
String value2 = "value2";
assertEquals(value1, cache.getAndPut(key2, value2));
assertEquals(value2, cache.get(key1));
assertEquals(value2, cache.get(key2));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_NotThere() {
Long existingKey = System.currentTimeMillis();
String existingValue = "value" + existingKey;
assertNull(cache.getAndPut(existingKey, existingValue));
assertEquals(existingValue, cache.get(existingKey));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void getAndPut_Closed() {
cache.close();
try {
cache.getAndPut(null, null);
fail("should have thrown an exception - cache closed");
} catch (IllegalStateException e) {
//good
}
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doContainsKey(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(false, readCache.containsKey(key));
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals(true, readCache.containsKey(key));
}
代码示例来源:origin: javax.cache/cache-tests
@Test
public void shouldWriteThroughUsingGetAndPut_SingleEntryMultipleTimes() {
assertEquals(0, cacheWriter.getWriteCount());
assertEquals(0, cacheWriter.getDeleteCount());
cache.getAndPut(1, "Gudday World");
cache.getAndPut(1, "Gudday World");
cache.getAndPut(1, "Gudday World");
assertEquals(3, cacheWriter.getWriteCount());
assertEquals(0, cacheWriter.getDeleteCount());
assertTrue(cacheWriter.hasWritten(1));
assertEquals("Gudday World", cacheWriter.get(1));
}
代码示例来源: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));
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doConditionalRemove(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(null, readCache.get(key));
assertFalse(writeCache.remove(key, "xxx"));
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals("one", readCache.get(key));
assertFalse(writeCache.remove(key, "xxx"));
assertEquals("one", readCache.get(key));
assertTrue(writeCache.remove(key, "one"));
assertEquals(null, readCache.get(key));
}
代码示例来源:origin: org.infinispan/infinispan-core
private <K> void doGetAndReplace(Supplier<K> keySupplier,
Cache<K, String> readCache, Cache<K, String> writeCache) {
K key = keySupplier.get();
assertEquals(null, readCache.get(key));
assertEquals(null, writeCache.getAndReplace(key, "xxx"));
assertEquals(null, writeCache.getAndPut(key, "one"));
assertEquals("one", readCache.get(key));
assertEquals("one", writeCache.getAndReplace(key, "uno"));
assertEquals("uno", readCache.get(key));
assertTrue(writeCache.remove(key));
assertEquals(null, readCache.get(key));
}
内容来源于网络,如有侵权,请联系作者删除!