Python 官方文档:入门教程 => 点击学习
目录一、前言二、缓存注解三、实战操作1、依赖引入2、yaml配置3、开启缓存4、模拟方法5、测试6、改造一、前言 spring Cache本身是Spring框架中一个缓存体系的抽象实
spring Cache本身是Spring框架中一个缓存体系的抽象实现,本身不具备缓存能力,需要配合具体的缓存实现来完成,如Ehcache、Caffeine、Guava、Redis等。
我选择使用目前最受欢迎的Caffeine来作为具体的缓存实现方式,下面是一个demo:
<dependency>
<groupId>com.GitHub.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
spring:
cache:
cache-names: USER
caffeine:
spec: initialCapacity=50,maximumSize=500,expireAfterWrite=5s
type: caffeine
Caffeine配置说明
注意
service层
@Service
@Slf4j
public class CaffeineService {
public static Map<String, String> map = new HashMap<>();
static {
map.put("1", "zhangsan");
map.put("2", "lisi");
map.put("3", "wangwu");
}
@Cacheable(value = "USER", key = "#id")
public String getUser(String id) {
log.info("getUser() run......");
return map.get(id);
}
@CachePut(value = "USER", key = "#id")
public String updateUser(String id, String name) {
log.info("updateUser() run......");
map.put(id, name);
return map.toString();
}
@CacheEvict(value = "USER", key = "#id")
public String delUser(String id) {
log.info("delUser() run......");
map.remove(id);
return map.toString();
}
}
controller层
@RestController
@RequestMapping("/cache")
@Slf4j
public class CaffeineController {
@Autowired
private CaffeineService caffeineService;
@GetMapping("/user/{id}")
public String getUser(@PathVariable String id) {
long start = System.currentTimeMillis();
String res = caffeineService.getUser(id);
long end = System.currentTimeMillis();
log.info("查询耗时:" + (end - start));
return res;
}
@GetMapping("/user/{id}/{name}")
public String updateUser(@PathVariable String id, @PathVariable String name) {
return caffeineService.updateUser(id, name);
}
@DeleteMapping("/user/{id}")
public String delUser(@PathVariable String id) {
return caffeineService.delUser(id);
}
}
第一次查询:
第二次查询:
查询耗时明显小于第一次查询,因为第二次直接返回缓存,速度提升。
执行更新后再查询:
会使缓存失效。会重新执行查询方法查询
执行删除后再查询:
会使缓存失效。会重新执行查询方法查询
上述通过yaml文件配置的方式不够灵活,无法实现多种缓存策略,所以现在一般使用javaconfig的形式进行配置。
下面是示例代码:
@Configuration
public class CaffeineConfig {
@Bean
public CacheManager caffeineCacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
List<CaffeineCache> caffeineCaches = new ArrayList<>();
for (CacheType cacheType : CacheType.values()) {
caffeineCaches.add(new CaffeineCache(cacheType.name(),
Caffeine.newBuilder()
.expireAfterWrite(cacheType.getExpires(), TimeUnit.SECONDS)
.build()));
}
simpleCacheManager.setCaches(caffeineCaches);
return simpleCacheManager;
}
}
public enum CacheType {
USER(5),
TENANT(20);
private int expires;
CacheType(int expires) {
this.expires = expires;
}
public int getExpires() {
return expires;
}
}
这样我们就能对USER设置5秒消防时间,对TENANT设置20秒消亡时间,在实际项目中这种方式更加的灵活。
到此这篇关于Spring Cache 集成 Caffeine实现项目缓存的示例的文章就介绍到这了,更多相关Spring Cache Caffeine缓存内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Spring Cache 集成 Caffeine实现项目缓存的示例
本文链接: https://www.lsjlt.com/news/160749.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0