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

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

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

Cache.putIfAbsent介绍

[英]Atomically associate the specified value with the specified key in this cache if it is not set already.

This is equivalent to:

Object existingValue = cache.get(key); 
if (existingValue == null) { 
cache.put(key, value); 
return null; 
} else { 
return existingValue; 
}

except that the action is performed atomically. While all out-of-the-box CacheManager implementations are able to perform the put atomically, the operation may also be implemented in two steps, e.g. with a check for presence and a subsequent put, in a non-atomic way. Check the documentation of the native cache implementation that you are using for more details.
[中]如果尚未设置指定值,则自动将其与此缓存中的指定键关联。
这相当于:

Object existingValue = cache.get(key); 
if (existingValue == null) { 
cache.put(key, value); 
return null; 
} else { 
return existingValue; 
}

除了操作是以原子方式执行之外。虽然所有开箱即用的CacheManager实现都能够以原子方式执行put,但该操作也可以通过两个步骤来实现,例如,以非原子方式检查存在性和后续put。有关更多详细信息,请查看您正在使用的本机缓存实现的文档。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
  return this.targetCache.putIfAbsent(key, value);
}

代码示例来源:origin: org.springframework/spring-context-support

@Override
@Nullable
public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
  return this.targetCache.putIfAbsent(key, value);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void putIfAbsent() { // no transactional support for putIfAbsent
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  Object key = new Object();
  assertNull(cache.putIfAbsent(key, "123"));
  assertEquals("123", target.get(key, String.class));
  assertEquals("123", cache.putIfAbsent(key, "456").get());
  assertEquals("123", target.get(key, String.class)); // unchanged
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testCachePutIfAbsent() throws Exception {
  T cache = getCache();
  String key = createRandomKey();
  Object value = "initialValue";
  assertNull(cache.get(key));
  assertNull(cache.putIfAbsent(key, value));
  assertEquals(value, cache.get(key).get());
  assertEquals("initialValue", cache.putIfAbsent(key, "anotherValue").get());
  assertEquals(value, cache.get(key).get()); // not changed
}

代码示例来源:origin: org.apache.camel/camel-spring

@Override
@ManagedOperation(description = "Adds the key to the store")
public boolean add(Object key) {
  return cache.putIfAbsent(key, true) == null;
}

代码示例来源:origin: spring-projects/spring-framework

assertNull(cache1.get("key3"));
assertEquals("value1", cache1.putIfAbsent("key1", "value1x").get());
assertEquals("value1", cache1.get("key1").get());
assertEquals(2, cache1.putIfAbsent("key2", 2.1).get());
assertNull(cache1.putIfAbsent("key3", null));
assertNull(cache1.get("key3").get());
assertNull(cache1.putIfAbsent("key3", null).get());
assertNull(cache1.get("key3").get());
cache1.evict("key3");

代码示例来源:origin: org.eclipse.hawkbit/hawkbit-autoconfigure

/**
 * @param key
 * @param value
 * @return
 * @see org.springframework.cache.Cache#putIfAbsent(java.lang.Object,
 *      java.lang.Object)
 */
@Override
public ValueWrapper putIfAbsent(final Object key, final Object value) {
  return delegate.putIfAbsent(key, value);
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public ValueWrapper putIfAbsent(final Object key, final Object value) {
  return this.targetCache.putIfAbsent(key, value);
}

代码示例来源:origin: com.github.edgar615/spring-boot-util-cache

@Override
public ValueWrapper putIfAbsent(Object key, @Nullable Object value) {
 ValueWrapper valueWrapper = level2.putIfAbsent(key, value);
 if (valueWrapper != null) {
  return valueWrapper;
 }
 valueWrapper = level1.putIfAbsent(key, value);
 if (valueWrapper != null) {
  return valueWrapper;
 }
 return null;
}

代码示例来源:origin: org.carewebframework/org.carewebframework.api.core

/**
 * Updates the delivered message cache. This avoids delivering the same message transported by
 * different messaging frameworks. If we have only one consumer registered, we don't need to
 * worry about this.
 * 
 * @param message The message being delivered.
 * @return True if the cache was updated (i.e., the message has not been previously delivered).
 */
private boolean updateDelivered(Message message) {
  if (consumers.size() <= 1) {
    return true;
  }
  
  String pubid = (String) message.getMetadata("cwf.pub.event");
  return deliveredMessageCache.putIfAbsent(pubid, "") == null;
}

代码示例来源:origin: org.infinispan/infinispan-spring4

@Test
public void testReturningNullValueIfThereIsNoValue() throws Exception {
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test");
 //then
 assertNull(existingValue);
}

代码示例来源:origin: org.infinispan/infinispan-spring4-embedded

@Test
public void testReturningNullValueIfThereIsNoValue() throws Exception {
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test");
 //then
 assertNull(existingValue);
}

代码示例来源:origin: org.infinispan/infinispan-spring5-embedded

@Test
public void testReturningNullValueIfThereIsNoValue() throws Exception {
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test");
 //then
 assertNull(existingValue);
}

代码示例来源:origin: org.infinispan/infinispan-spring4-embedded

@Test
public void testReturningNullValueConstant() throws Exception {
 //given
 this.cache.put("test", NullValue.NULL);
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals(NullValue.NULL, existingValue);
}

代码示例来源:origin: org.infinispan/infinispan-spring4

@Test
public void testReturningNullValueConstant() throws Exception {
 //given
 this.cache.put("test", NullValue.NULL);
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals(NullValue.NULL, existingValue);
}

代码示例来源:origin: org.infinispan/infinispan-spring5-embedded

@Test
public void testReturningNullValueConstant() throws Exception {
 //given
 this.cache.put("test", NullValue.NULL);
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals(NullValue.NULL, existingValue);
}

代码示例来源:origin: spring-cloud/spring-cloud-stream

@Override
protected Schema resolveWriterSchemaForDeserialization(MimeType mimeType) {
  if (this.readerSchema == null) {
    SchemaReference schemaReference = extractSchemaReference(mimeType);
    if (schemaReference != null) {
      ParsedSchema parsedSchema = cacheManager.getCache(REFERENCE_CACHE_NAME).get(schemaReference, ParsedSchema.class);
      if (parsedSchema == null) {
        String schemaContent = this.schemaRegistryClient.fetch(schemaReference);
        if (schemaContent != null) {
          Schema schema = new Schema.Parser().parse(schemaContent);
          parsedSchema = new ParsedSchema(schema);
          cacheManager.getCache(REFERENCE_CACHE_NAME).putIfAbsent(schemaReference, parsedSchema);
        }
      }
      if (parsedSchema != null) {
        return parsedSchema.getSchema();
      }
    }
  }
  return this.readerSchema;
}

代码示例来源:origin: org.infinispan/infinispan-spring4

@Test
public void testReturningPreviousValue() throws Exception {
 //given
 this.cache.put("test", "test");
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals("test", existingValue.get());
}

代码示例来源:origin: org.infinispan/infinispan-spring5-embedded

@Test
public void testReturningPreviousValue() throws Exception {
 //given
 this.cache.put("test", "test");
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals("test", existingValue.get());
}

代码示例来源:origin: org.infinispan/infinispan-spring4-embedded

@Test
public void testReturningPreviousValue() throws Exception {
 //given
 this.cache.put("test", "test");
 //when
 Cache.ValueWrapper existingValue = this.cache.putIfAbsent("test", "test1");
 //then
 assertEquals("test", existingValue.get());
}

相关文章