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 ]
相关推荐
武哥聊编程28 分钟前
【AI实战项目】基于SpringAI+Springboot+Vue的AI面试刷题训练平台
vue.js·人工智能·spring boot·springai
vipxieliang1 小时前
固定电话验证详解:区号、号码、分机号的完整验证
java·spring boot
步行cgn1 小时前
Spring Boot 主入口类上的 @Enable 和 @Scan 注解详解
java·spring boot·后端
xixingzhe22 小时前
SpringBoot DDD 实战入门文档
spring boot·spring·ddd
杨运交4 小时前
[069][公共模块]Spring Boot 全局异常处理与参数校验实战(下):校验异常精细化处理与 WebFlux 适配
java·spring boot·后端
行者-全栈开发4 小时前
Spring Boot + FFmpeg 视频批量处理实战:压缩、HLS切片与异步任务引擎
spring boot·ffmpeg·异步处理·视频压缩·hls切片·批量任务·redis队列
Andya_net4 小时前
Spring Boot | 条件注解完全指南:从 @Conditional 到 @ConditionalOnExpression 的原理、实践与避坑
spring boot·后端·python
极小狐5 小时前
CI 流水线提速实战:缓存设计与依赖代理的工程实践
缓存·ci/cd·gitlab·devops
小荷才露尖尖角,早有蜻蜓立上头5 小时前
class java.util.LinkedHashMap cannot be cast to xxx
java·spring boot
莫得感情 o5 小时前
Redis 09 · 高可用:Sentinel 如何发现故障并完成切主
redis·缓存·sentinel