springboot整合单机缓存ehcache的实现
作者:chenxianchon 发布时间:2023-11-09 15:27:24
标签:springboot,ehcache
区别于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
0
投稿
猜你喜欢
- 剑指Offer(Python多种思路实现):剪绳子面试14题:题目:剪绳子题:给你一根长度为n的绳子,请把绳子剪成m段(m,n都是整数,且n
- 事先在网上搜索了一大圈,头都大了,看到那么多文章写道在python里安装psycopg2的各种坑和各种麻烦,各种不成功。搜索了一下午,索性外
- python可以简单优美,也很有趣,下面是收集的例子:1.一句话开始一个http的文件服务器:$ python -m SimpleHTTPS
- 本文实例为大家分享了python简单实现图片文字分割的具体代码,供大家参考,具体内容如下原图:图片预处理:图片二值化以及图片降噪处理。# 图
- 一、前言上次写了一个俄罗斯方块,感觉好像大家都看懂了,这次就更新一个植物大战僵尸吧二、引入模块import pygameimport ran
- 二维矩阵的transpose函数:不晓得该怎么起头,直接上干货。transpose()简单来说,就相当于数学中的转置,在矩阵中,转置就是把行
- ScrapyScrapy是一个比较好用的Python爬虫框架,你只需要编写几个组件就可以实现网页数据的爬取。但是当我们要爬取的页面非常多的时
- 之前用Crystal做了一个数字转English Word的Formula刚刚心血来潮, 大半个晚上写了JS版本的数字转换, 由于JS的Bu
- PHP输出JSON格式数据常用框架封装好的方法来输出JSON数据,但是手动去书写的时候却遇到了问题,因为输出的数据类型为字符串类型,导致不能
- 在 ASP(VBScript 为语言)中,Asc 函数的返回值小于 0 的,可以被判断为中文字符。Asc 函数返回与字符串的第一个字母对应的
- 一个方便的特性是你可以给一个视图指定默认的参数。 这样,当没有给这个参数赋值的时候将会使用默认的值。例子:# urls.pyfrom dja
- 本文实例讲述了JS实现根据数组对象的某一属性排序操作。分享给大家供大家参考,具体如下:根据数组中对象的某一属性排序var newArray
- 迭代器迭代器(iterator)有时又称游标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如链表或阵列)上遍
- ajax应用非常普及,ajax的优点大家都知道,具体就不多说了,其缺点还真不少,比如破坏浏览器的back功能,同一个url打开的界面并不是完
- JDBC之C3P0数据库连接池,供大家参考,具体内容如下1 首先在src中创建c3p0-config.xml 配置文件,文件中内容如下(首先
- 写在最前最近在使用vue的时候,遇到一个需求,实现左右div可通过中间部分拖拽调整宽度,类似于这样这是我最终的实现效果还是老话,因为我不是专
- 欢欢喜喜辞旧岁 🐰🐰🐰辞旧迎新之际,来 AI Studio 一起 #欢喜迎兔年# !分享不同新年风俗,共享一段快乐时光!🐰🐰🐰p>下面
- 一、使用PyChram的正则首先,小编讲的不是爬取ip,而是讲了解PyCharm的正则,这里讲的正则不是Python的re模块哈!而是PyC
- 本文实例为大家分享了python保存网页图片到本地的具体代码,供大家参考,具体内容如下#!/usr/bin/env Python#codin
- W3C发布了WCAG 2.0提案(Web Content Accessibility Guidelines 网页内容无障碍指南),大概为了实