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 ]
相关推荐
Long_poem21 分钟前
【自学笔记】Spring Boot框架技术基础知识点总览-持续更新
spring boot·笔记·后端
楠枬1 小时前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring
hong_zc1 小时前
SpringBoot 配置文件
java·spring boot·后端
早起的年轻人1 小时前
Docket Desktop 安装redis 并设置密码
数据库·redis·缓存
m0_748257462 小时前
创建一个简单的spring boot+vue前后端分离项目
vue.js·spring boot·后端
m0_748234902 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
草字2 小时前
vue,vue3 keepalive没有效果,无法缓存页面include无效,keep-alive
前端·vue.js·缓存
oioihoii2 小时前
深入理解 C++17 的缓存行接口
java·c++·缓存
qw9492 小时前
Redis(高阶篇)03章——缓存双写一致性之更新策略探讨
数据库·redis·缓存