SpringBoot Cache缓存

复制代码
@EnableCaching
@SpringBootApplication
public class TestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TestApplication.class, args);
	}
	
}

@Service
public class CityService {

	@Cacheable(value = "city")
	public Map<String, Object> insert(String key) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "shanghai");
		return map;
	}
	
	@CachePut(value = "city")
	public Map<String, Object> update(String key) {
		Map<String, Object> map = new HashMap<>();
		map.put("name", "beijing");
		return map;
	}
	
	@CacheEvict(value = "city")
	public Map<String, Object> delete(String key) {
		return new HashMap<>();
	}
	
}

application.properties配置文件

复制代码
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:ehcache.xml

注意:如果不指定spring.cache.ehcache.config=classpath:ehcache.xml配置且使用了redis缓存框架,那么使用@Cacheable注解时,数据会被写入redis缓存

ehcache.xml配置文件

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <cache
            name="person"
            eternal="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="300"
            timeToLiveSeconds="0"
            memoryStoreEvictionPolicy="LRU" />
     <cache
            name="city"
            eternal="false"
            maxElementsInMemory="1000"
            overflowToDisk="false"
            diskPersistent="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="36000"
            memoryStoreEvictionPolicy="LRU" />
</ehcache>

测试和测试结果

复制代码
// key默认使用方法参数的值
cityService.insert("abc");
log.info("{}", CacheManager.create().getCache("city").get("abc"));
cityService.update("abc");
log.info("{}", CacheManager.create().getCache("city").get("abc"));
cityService.delete("abc");
log.info("{}", CacheManager.create().getCache("city").get("abc"));
Cache cache = CacheManager.create().getCache("person");
cache.put(new Element("name", "jerry"));
cache.put(new Element("age", 18));
log.info("{}", cache.get("name"));
log.info("{}", cache.get("age"));

2024-01-15 10:08:13 [restartedMain] INFO  cn.hwd.TestRunner - [ key = abc, value={name=shanghai}, version=1, hitCount=1, CreationTime = 1705284493613, LastAccessTime = 1705284493618 ] 
2024-01-15 10:08:13 [restartedMain] INFO  cn.hwd.TestRunner - [ key = abc, value={name=beijing}, version=1, hitCount=1, CreationTime = 1705284493619, LastAccessTime = 1705284493619 ] 
2024-01-15 10:08:13 [restartedMain] INFO  cn.hwd.TestRunner - null
2024-01-15 10:08:13 [restartedMain] INFO  cn.hwd.TestRunner - [ key = name, value=jerry, version=1, hitCount=1, CreationTime = 1705284493684, LastAccessTime = 1705284493684 ] 
2024-01-15 10:08:13 [restartedMain] INFO  cn.hwd.TestRunner - [ key = age, value=18, version=1, hitCount=1, CreationTime = 1705284493684, LastAccessTime = 1705284493684 ]
相关推荐
paopaokaka_luck9 小时前
基于Springboot3+Vue3的高校选课系统(AI选课助手、协同过滤算法、分享到微博、扣扣、Echarts图形化分析)
网络·spring boot·网络协议·echarts
小Ti客栈11 小时前
Spring Boot 整合 Swagger2 和 Knife4j实现接口文档与可视化调试
java·spring boot·后端
2601_9638701711 小时前
【计算机毕业设计】基于Spring Boot的社区老年大学课程报名与学习系统的设计与实现
java·spring boot·学习
weixin_BYSJ198712 小时前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
凤山老林13 小时前
SpringBoot 3 启用 spring.factories 后如何升级替换原有的 spring.factories ?
spring boot·后端·spring
海兰14 小时前
【高速缓存】RedisVL 存储类型选择指南:Hash 与 JSON
人工智能·redis·算法·缓存·json·哈希算法
啊啊啊迈 旋棍15 小时前
生态整合与实战篇:Spring Boot 整合 RocketMQ 完全指南
spring boot·rocketmq·java-rocketmq
减瓦15 小时前
Jackson 使用指南
java·spring boot·json
旺仔学长 哈哈16 小时前
基于SpringBoot的在线招聘测评系统的设计与实现----附源码35253+数据库文档
数据库·spring boot·后端·在线招聘
海兰16 小时前
【高速缓存】RedisVL 索引迁移指南:安全演进索引结构
前端·数据库·redis·安全·缓存·bootstrap