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

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

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

Cache.put介绍

[英]Associate the specified value with the specified key in this cache.

If the cache previously contained a mapping for this key, the old value is replaced by the specified value.
[中]将指定的值与此缓存中的指定键相关联。
如果缓存以前包含此键的映射,则旧值将替换为指定值。

代码示例

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

/**
 * Execute {@link Cache#put(Object, Object)} on the specified {@link Cache}
 * and invoke the error handler if an exception occurs.
 */
protected void doPut(Cache cache, Object key, @Nullable Object result) {
  try {
    cache.put(key, result);
  }
  catch (RuntimeException ex) {
    getErrorHandler().handleCachePutError(ex, cache, key, result);
  }
}

代码示例来源: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

@Test
public void resolverUrlPathFromCache() {
  String expected = "cached-imaginary.css";
  this.cache.put(CachingResourceResolver.RESOLVED_URL_PATH_CACHE_KEY_PREFIX + "imaginary.css", expected);
  String actual = this.chain.resolveUrlPath("imaginary.css", this.locations).block(TIMEOUT);
  assertEquals(expected, actual);
}

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

@Test
public void resolveResourceInternalFromCache() {
  Resource expected = Mockito.mock(Resource.class);
  this.cache.put(resourceKey("bar.css"), expected);
  Resource actual = this.chain.resolveResource(null, "bar.css", this.locations);
  assertSame(expected, actual);
}

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

@Test
public void resolveResourceMatchingEncoding() {
  Resource resource = Mockito.mock(Resource.class);
  Resource gzipped = Mockito.mock(Resource.class);
  this.cache.put(resourceKey("bar.css"), resource);
  this.cache.put(resourceKey("bar.css+encoding=gzip"), gzipped);
  String file = "bar.css";
  MockServerWebExchange exchange = MockServerWebExchange.from(get(file));
  assertSame(resource, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
  exchange = MockServerWebExchange.from(get(file).header("Accept-Encoding", "gzip"));
  assertSame(gzipped, this.chain.resolveResource(exchange, file, this.locations).block(TIMEOUT));
}

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

@Test
public void resolveResourceInternalFromCache() {
  Resource expected = Mockito.mock(Resource.class);
  this.cache.put(resourceKey("bar.css"), expected);
  MockServerWebExchange exchange = MockServerWebExchange.from(get(""));
  Resource actual = this.chain.resolveResource(exchange, "bar.css", this.locations).block(TIMEOUT);
  assertSame(expected, actual);
}

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

@Test
public void putNonTransactional() {
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  Object key = new Object();
  cache.put(key, "123");
  assertEquals("123", target.get(key, String.class));
}

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

@Test
public void putFailProperException() {
  UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on put");
  willThrow(exception).given(this.cache).put(0L, 0L);
  this.cacheInterceptor.setErrorHandler(new SimpleCacheErrorHandler());
  this.thrown.expect(is(exception));
  this.simpleService.put(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 remove() {
  String keyItem = name.getMethodName();
  Cache cache = getCache(DEFAULT_CACHE);
  Object key = createKey(keyItem);
  Object value = new Object();
  cache.put(key, value);
  service.remove(keyItem);
  assertNull(cache.get(key));
}

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

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

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

@Test
public void removeAll() {
  Cache cache = getCache(DEFAULT_CACHE);
  Object key = createKey(name.getMethodName());
  cache.put(key, new Object());
  service.removeAll();
  assertTrue(isEmpty(cache));
}

代码示例来源: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 earlyRemoveAll() {
  Cache cache = getCache(DEFAULT_CACHE);
  Object key = createKey(name.getMethodName());
  cache.put(key, new Object());
  service.earlyRemoveAll();
  assertTrue(isEmpty(cache));
}

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

@Test
public void testNoOpCache() throws Exception {
  String name = createRandomKey();
  Cache cache = this.manager.getCache(name);
  assertEquals(name, cache.getName());
  Object key = new Object();
  cache.put(key, new Object());
  assertNull(cache.get(key));
  assertNull(cache.get(key, Object.class));
  assertSame(cache, cache.getNativeCache());
}

代码示例来源: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 putTransactional() {
  Cache target = new ConcurrentMapCache("testCache");
  Cache cache = new TransactionAwareCacheDecorator(target);
  TransactionStatus status = this.txManager.getTransaction(
      new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED));
  Object key = new Object();
  cache.put(key, "123");
  assertNull(target.get(key));
  this.txManager.commit(status);
  assertEquals("123", target.get(key, String.class));
}

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

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

代码示例来源: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);
}

相关文章