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 ]
相关推荐
皮皮林55111 分钟前
SpringBoot + Disruptor 实现特快高并发处理,支撑每秒 600 万订单无压力!
spring boot
阿丰资源36 分钟前
基于SpringBoot的在线视频教育平台的设计与实现(附源码+数据库+文档,一键运行)
数据库·spring boot·后端
苍煜41 分钟前
ThreadPoolExecutor线程池终极全解:同步异步判定+SpringBoot生产实战
java·开发语言·spring boot
CyrusCJA7 小时前
在Windows系统上将Redis注册为系统服务使其实现开机自启
数据库·windows·redis·缓存
阿丰资源8 小时前
基于SpringBoot的房产销售系统设计与实现(附源码+数据库+文档,一键运行)
数据库·spring boot·后端
aLTttY9 小时前
Spring Boot整合AI大模型实现智能问答系统实战
人工智能·spring boot·后端
逆境不可逃11 小时前
一篇速通Redis 从原理到Java实战(含缓存问题解决方案+集群配置)
数据库·redis·缓存
Java成神之路-12 小时前
面试题:@Controller 与 @RestController 区别
java·spring boot
studytosky12 小时前
【高并发内存池】线程缓存核心原理与实现
linux·服务器·git·缓存
Lanren的编程日记12 小时前
Flutter 鸿蒙应用内存管理优化实战:对象池+智能缓存+泄漏检测,全方位提升应用稳定性
flutter·缓存·华为·harmonyos