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 ]
相关推荐
你这个代码我看不懂26 分钟前
SpringBoot单元测试Mock和Spy
spring boot·单元测试·log4j
打工的小王1 小时前
Redis(二)数据类型
数据库·redis·缓存
Java程序员威哥2 小时前
SpringBoot2.x与3.x自动配置注册差异深度解析:从原理到迁移实战
java·大数据·开发语言·hive·hadoop·spring boot·后端
shejizuopin2 小时前
基于Spring Boot+小程序的非遗科普平台设计与实现(毕业论文)
spring boot·后端·小程序·毕业设计·论文·毕业论文·非遗科普平台设计与实现
vx_bisheyuange3 小时前
【源码免费送】计算机毕设精选项目:基于SpringBoot的汽车租赁系统的设计与实现
spring boot·汽车·毕业设计·需求分析
笨蛋不要掉眼泪3 小时前
Redis核心数据类型与命令
数据库·redis·缓存
浅水壁虎3 小时前
任务调度——XXLJOB3(执行器)
java·服务器·前端·spring boot
小唐同学爱学习3 小时前
短链接修改之写锁
spring boot·redis·后端·mysql
zhglhy4 小时前
Redis Cluster 的数据分片机制
数据库·redis·缓存
编程彩机4 小时前
互联网大厂Java面试:从Spring Boot到分布式缓存的技术场景解析
java·redis·分布式·缓存·大厂面试·技术解析·sprint boot