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

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

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

Cache.get介绍

[英]Return the value to which this cache maps the specified key.

Returns null if the cache contains no mapping for this key; otherwise, the cached value (which may be null itself) will be returned in a ValueWrapper.
[中]返回此缓存映射到指定键的值。
如果缓存不包含此键的映射,则返回null;否则,缓存值(本身可能为null)将在ValueWrapper中返回。

代码示例

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

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

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

@Override
protected Mono<String> resolveUrlPathInternal(String resourceUrlPath,
    List<? extends Resource> locations, ResourceResolverChain chain) {
  String key = RESOLVED_URL_PATH_CACHE_KEY_PREFIX + resourceUrlPath;
  String cachedUrlPath = this.cache.get(key, String.class);
  if (cachedUrlPath != null) {
    logger.trace("Path resolved from cache");
    return Mono.just(cachedUrlPath);
  }
  return chain.resolveUrlPath(resourceUrlPath, locations)
      .doOnNext(resolvedPath -> this.cache.put(key, resolvedPath));
}

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

@Override
public Mono<Resource> transform(ServerWebExchange exchange, Resource resource,
    ResourceTransformerChain transformerChain) {
  Resource cachedResource = this.cache.get(resource, Resource.class);
  if (cachedResource != null) {
    logger.trace(exchange.getLogPrefix() + "Resource resolved from cache");
    return Mono.just(cachedResource);
  }
  return transformerChain.transform(exchange, resource)
      .doOnNext(transformed -> this.cache.put(resource, transformed));
}

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

@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
  Object key = SimpleKeyGenerator.generateKey(id);
  Cache.ValueWrapper valueWrapper = defaultCache.get(key);
  if (valueWrapper != null) {
    throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
  }
}

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

@Override
@CacheRemove(afterInvocation = false)
public void earlyRemove(String id) {
  Object key = SimpleKeyGenerator.generateKey(id);
  Cache.ValueWrapper valueWrapper = defaultCache.get(key);
  if (valueWrapper != null) {
    throw new AssertionError("Value with key " + key + " expected to be already remove from cache");
  }
}

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

public void testCacheUpdate(CacheableService<?> service) {
  Object o = new Object();
  Cache cache = this.cm.getCache("testCache");
  assertNull(cache.get(o));
  Object r1 = service.update(o);
  assertSame(r1, cache.get(o).get());
  o = new Object();
  assertNull(cache.get(o));
  Object r2 = service.update(o);
  assertSame(r2, cache.get(o).get());
}

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

@Test
public void getFail() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
  willThrow(exception).given(this.cache).get(0L);
  Object result = this.simpleService.get(0L);
  verify(this.errorHandler).handleCacheGetError(exception, cache, 0L);
  verify(this.cache).get(0L);
  verify(this.cache).put(0L, result); // result of the invocation
}

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

@Test
public void earlyPut() {
  String keyItem = name.getMethodName();
  Cache cache = getCache(DEFAULT_CACHE);
  Object key = createKey(keyItem);
  Object value = new Object();
  assertNull(cache.get(key));
  service.earlyPut(keyItem, value);
  Cache.ValueWrapper result = cache.get(key);
  assertNotNull(result);
  assertEquals(value, result.get());
}

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

@Test
public void getFailProperException() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
  willThrow(exception).given(this.cache).get(0L);
  this.cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler());
  this.thrown.expect(is(exception));
  this.simpleService.get(0L);
}

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

@Test
public void evictNonTransactional() {
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  Object key = new Object();
  cache.put(key, "123");
  cache.evict(key);
  assertNull(target.get(key));
}

代码示例来源: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: spring-projects/spring-framework

public void testRootVars(CacheableService<?> service) {
  Object key = new Object();
  Object r1 = service.rootVars(key);
  assertSame(r1, service.rootVars(key));
  Cache cache = this.cm.getCache("testCache");
  // assert the method name is used
  String expectedKey = "rootVarsrootVars" + AopProxyUtils.ultimateTargetClass(service) + service;
  assertNotNull(cache.get(expectedKey));
}

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

public void testCacheableNull(CacheableService<?> service) throws Exception {
  Object o1 = new Object();
  assertNull(this.cm.getCache("testCache").get(o1));
  Object r1 = service.cacheNull(o1);
  Object r2 = service.cacheNull(o1);
  Object r3 = service.cacheNull(o1);
  assertSame(r1, r2);
  assertSame(r1, r3);
  assertEquals(r3, this.cm.getCache("testCache").get(o1).get());
  assertNull("Cached value should be null", r3);
}

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

@Test
public void getFail() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
  Object key = SimpleKeyGenerator.generateKey(0L);
  willThrow(exception).given(this.cache).get(key);
  this.simpleService.get(0L);
  verify(this.errorHandler).handleCacheGetError(exception, this.cache, key);
}

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

@Test
public void testCustomKeyGenerator() {
  Object param = new Object();
  Object r1 = this.cs.customKeyGenerator(param);
  assertSame(r1, this.cs.customKeyGenerator(param));
  Cache cache = this.cm.getCache("testCache");
  // Checks that the custom keyGenerator was used
  Object expectedKey = SomeCustomKeyGenerator.generateKey("customKeyGenerator", param);
  assertNotNull(cache.get(expectedKey));
}

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

@Test
public void testCacheRemove() throws Exception {
  T cache = getCache();
  String key = createRandomKey();
  Object value = "george";
  assertNull(cache.get(key));
  cache.put(key, value);
}

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

@Test
public void getAndPutFail() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
  willThrow(exception).given(this.cache).get(0L);
  willThrow(exception).given(this.cache).put(0L, 0L); // Update of the cache will fail as well
  Object counter = this.simpleService.get(0L);
  willReturn(new SimpleValueWrapper(2L)).given(this.cache).get(0L);
  Object counter2 = this.simpleService.get(0L);
  Object counter3 = this.simpleService.get(0L);
  assertNotSame(counter, counter2);
  assertEquals(counter2, counter3);
}

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

@Test
public void cacheWithCustomKeyGenerator() {
  String keyItem = name.getMethodName();
  Cache cache = getCache(DEFAULT_CACHE);
  Object key = createKey(keyItem);
  service.cacheWithCustomKeyGenerator(keyItem, "ignored");
  assertNull(cache.get(key));
}

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

@Test
public void getSimple() {
  this.keyGenerator.expect(1L);
  Object first = this.simpleService.get(1L);
  Object second = this.simpleService.get(1L);
  assertSame(first, second);
  Object key = new SimpleKey(1L);
  assertEquals(first, cache.get(key).get());
}

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

@Test
public void getFlattenVararg() {
  this.keyGenerator.expect(1L, "foo", "bar");
  Object first = this.simpleService.get(1L, "foo", "bar");
  Object second = this.simpleService.get(1L, "foo", "bar");
  assertSame(first, second);
  Object key = new SimpleKey(1L, "foo", "bar");
  assertEquals(first, cache.get(key).get());
}

相关文章