java 具有弹簧启动和清除缓存方法的番石榴缓存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 具有弹簧启动和清除缓存方法的番石榴缓存相关的知识,希望对你有一定的参考价值。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
@Service
public class CachedService extends WebServiceGatewaySupport implements CachedService {
@Inject
private RestTemplate restTemplate;
@Cacheable(CacheConfig.CACHE_ONE)
public String getCached() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> reqEntity = new HttpEntity<>("url", headers);
ResponseEntity<String> response;
String url = "url";
response = restTemplate.exchange(
url,
HttpMethod.GET, reqEntity, String.class);
return response.getBody();
}
}
//Used for manually clearing the cache
@Controller
@RequestMapping("/")
public class CacheController {
@CacheEvict(value = CacheConfig.CACHE_ONE, allEntries = true)
@RequestMapping(value = "/clearCache", method = RequestMethod.GET)
public ResponseEntity<String> clearCache() {
return new ResponseEntity<String>("Cache Cleared", HttpStatus.OK);
}
}
import com.google.common.cache.CacheBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.guava.GuavaCache;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
@Configuration
@EnableCaching
public class CacheConfig implements CachingConfigurer {
public final static String CACHE_ONE = "cacheOne";
public final static String CACHE_TWO = "cacheTwo";
private final Logger log = LoggerFactory
.getLogger(CacheConfig.class);
@Bean
@Override
public CacheManager cacheManager() {
log.info("Initializing simple Guava Cache manager.");
SimpleCacheManager cacheManager = new SimpleCacheManager();
GuavaCache cache1 = new GuavaCache(CACHE_ONE, CacheBuilder.newBuilder()
.expireAfterWrite(60, TimeUnit.MINUTES)
.build());
GuavaCache cache2 = new GuavaCache(CACHE_TWO, CacheBuilder.newBuilder()
.expireAfterWrite(60, TimeUnit.SECONDS)
.build());
cacheManager.setCaches(Arrays.asList(cache1,cache2));
return cacheManager;
}
@Override
public CacheResolver cacheResolver() {
return null;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
@Override
public CacheErrorHandler errorHandler() {
return null;
}
}
以上是关于java 具有弹簧启动和清除缓存方法的番石榴缓存的主要内容,如果未能解决你的问题,请参考以下文章
具有LRU回收策略的Java缓存
怎么清除dns缓存批处理?
java如何清除数据缓存
怎样使用redis缓存,java代码
java ehcache
Java怎么清除缓存