springboot整合单机缓存ehcache的实现
作者:chenxianchon 时间:2023-11-09 15:27:24
区别于redis的分布式缓存,ehcache是纯java进程内的单机缓存,根据不同的场景可选择使用,以下内容主要为springboot整合ehcache以及注意事项
添加pom引用
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>
启动类添加开启缓存注解:@EnableCaching
添加xml配置,注意,ehcache需要单独的配置文件
<?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">
<!--默认缓存策略 -->
<!-- external:是否永久存在,设置为true则不会被清除,此时与timeout冲突,通常设置为false-->
<!-- diskPersistent:是否启用磁盘持久化-->
<!-- maxElementsInMemory:最大缓存数量-->
<!-- overflowToDisk:超过最大缓存数量是否持久化到磁盘-->
<!-- timeToIdleSeconds:最大不活动间隔,设置过长缓存容易溢出,设置过短无效果,可用于记录时效性数据,例如验证码-->
<!-- timeToLiveSeconds:最大存活时间-->
<!-- memoryStoreEvictionPolicy:缓存清除策略-->
<defaultCache
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
memoryStoreEvictionPolicy="LRU" />
<cache name="cache1"
eternal="false"
diskPersistent="false"
maxElementsInMemory="1000"
overflowToDisk="false"
timeToIdleSeconds="2"
timeToLiveSeconds="2"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
这里我定义了一个缓存名字为cache1
修改项目的配置文件application.properties,添加spring缓存类型以及缓存配置文件路径
spring.cache.ehcache.config=classpath:ehcache.xml
spring.cache.type=ehcache
上面的步骤做好之后,就可以使用了
给你需要加缓存的方法添加注解
@Configuration
public class TestConfig {
@Cacheable(value = "cache1",key = "#id")
public TestController.Person create(String id) {
return new TestController.Person();
}
}
这里的value跟xml配置文件里的一致即可
我们调用一下测试看看
@GetMapping("/testCache1")
public void testCache1(@Param("id") String id) throws InterruptedException {
Person obj1 = testConfig.create(id);
Person obj2 = testConfig.create(id);
Thread.sleep(3000);
Person obj3 = testConfig.create(id);
Person obj4 = testConfig.create(id);
log.info("test1:"+obj1.toString());
log.info("test2:"+obj2.toString());
log.info("test3:"+obj3.toString());
log.info("test4:"+obj4.toString());
System.out.println(obj1.equals(obj2));
}
执行一下结果看
可以看到,obj1跟obj2是同一个对象,当程序睡眠了三秒之后,再次调用方法,就会重新创建对象,缓存生效
注意事项:
@Cacheable修饰的方法必须是public并且不能是static,原理是因为使用了 * ,需要重写方法
xml里面的配置要写全,要不然项目启动报错,就是下图这些
xml里面配置的defaultCache没看出有啥用,我也没删了试试
使用缓存的方法不能在@RestController修饰的类中,即不能在controller层,要不然缓存失效,可以在@Service、@Configuratin、@Component等类下面
来源:https://blog.csdn.net/cxclll/article/details/128974928
标签:springboot,ehcache
0
投稿
猜你喜欢
Python pass 语句使用示例
2022-05-01 16:49:55
wxpython中利用线程防止假死的实现方法
2022-01-27 00:45:29
红黑树的插入详解及Javascript实现方法示例
2024-04-19 11:03:13
vue项目中常用解决跨域的方法总结(CORS和Proxy)
2024-04-28 09:33:05
建立MySQL数据库日常维护规范
2009-03-20 12:34:00
python中字符串的操作方法大全
2023-10-01 17:47:15
前端必备插件之纯原生JS的瀑布流插件Macy.js
2024-02-27 09:05:59
对Python发送带header的http请求方法详解
2022-02-06 11:44:59
mysql的in会不会让索引失效?
2024-01-27 11:50:44
python函数返回多个值的示例方法
2023-04-14 10:38:05
Git如何修改已提交的commit注释
2023-10-04 02:17:54
Python曲线拟合详解
2023-12-29 05:54:50
Python使用OpenCV进行标定
2022-08-17 15:05:33
深入浅析python继承问题
2023-05-20 15:22:26
Python连接MySQL并使用fetchall()方法过滤特殊字符
2024-01-23 07:35:36
Python解压 rar、zip、tar文件的方法
2023-07-22 10:51:38
java模拟ATM功能(控制台连接Mysql数据库)
2024-01-22 16:54:46
Django shell调试models输出的SQL语句方法
2022-07-13 00:59:50
Pandas之ReIndex重新索引的实现
2023-10-22 20:47:28
CentOS6.8使用cmake安装MySQL5.7.18
2024-01-27 06:00:06