欢迎访问『www.cnblogs.com/blog-ice』
1.pom.xml增加2个依赖
2.application.properties增加配置
3.启动类增加@EnableCaching
4.配置CacheManager
5.打印所有缓存内容

注意:在当前类中调用缓存方法失效

<!-- cache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.8</version>
</dependency>

application.properties
caching.specs.user.timeout=10

@EnableCaching
public class SpringbootApiApplication {} @Configuration @ConfigurationProperties(prefix = "caching") @Data @Component public class CacheConfiguration { @Data public static class CacheSpec { private Integer timeout; private Integer max = 200; } private Map<String, CacheSpec> specs; @Primary @Bean("simpleCacheManager") public CacheManager cacheManager(Ticker ticker) { SimpleCacheManager manager = new SimpleCacheManager(); new CaffeineCacheManager(); if (specs != null) { List<CaffeineCache> caches = specs.entrySet().stream() .map(entry -> buildCache(entry.getKey(), entry.getValue(), ticker)) .collect(Collectors.toList()); manager.setCaches(caches); } return manager; } private CaffeineCache buildCache(String name, CacheSpec cacheSpec, Ticker ticker) { final Caffeine<Object, Object> caffeineBuilder = Caffeine.newBuilder() .expireAfterWrite(cacheSpec.getTimeout(), TimeUnit.SECONDS) .maximumSize(cacheSpec.getMax()) .ticker(ticker); return new CaffeineCache(name, caffeineBuilder.build()); } @Bean public Ticker ticker() { return Ticker.systemTicker(); } } @Service public class CacheService { // condition: 符合条件缓存 // unless: 符合条件不缓存 // @Cacheable(cacheNames = "user", condition = "#name != null") // @Cacheable(cacheNames = "user", unless = "#result.name.matches(\"null .*\")") // @Cacheable(cacheNames = "user", unless = "#result.id==1") // methodName cache // method.name cache // target com.example.springbootapi.service.CacheService@4db1afb2 // targetClass class com.example.springbootapi.service.CacheService // caches [org.springframework.cache.caffeine.CaffeineCache@7313263b] // #caches[0].name 异常 // caches[0].name user // args 3,null // #args 异常 // args[0] 3 // #args[0] 异常 // p0 3 // #p0 3 // #id 3 // #name Tom // p0 + p1 异常 // #p0 + #p1 3tom // #args[0] + #args[1] 异常 // #id + "_" + #name 3_null @Cacheable(cacheNames = "user", key = "#id + \"_\" + #name") public User cache(int id, String name) { User user = new User(id, name + " " + LocalDateTime.now().toString()); return user; } // 修改缓存 @CachePut("user") public User update(int id, String name) { User user = new User(id, name + " " + LocalDateTime.now().toString()); return user; } // 清除缓存 @CacheEvict("user") public User delete(int id, String name) { User user = new User(id, name + " " + LocalDateTime.now().toString()); return user; } }


调用
String info = cacheService.cache(id, null).toString();
info += "<br/>" + cacheService.cache(id, "tom").toString();


--------------------------------------------------
// 打印所有缓存
Collection<String> cacheNames = cacheManager.getCacheNames();
cacheNames.forEach(cacheName -> {
Cache cache = cacheManager.getCache(cacheName);
Object nativeCache = cache.getNativeCache();
Field[] fields = nativeCache.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
try {
System.out.println(field.getName() + "->" + field.get(nativeCache));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
// 打印所有缓存 end

 

posted on 2022-01-27 12:21  仙路尽头谁为峰  阅读(181)  评论(0编辑  收藏  举报
这里是自由发挥的天堂