SpringBoot整合Ehcache
来源:互联网 发布:知乎lolfaker 编辑:程序博客网 时间:2024/06/05 03:15
前言
近段时间在做一个类似cms的项目,在做完几十万的数据导入后,发现各种查询异常缓慢,数据库(单库,暂时服务器没那么多),数据库层面的索引各种优化前期也已经做过,单笔查询0.5s左右,但架不住量多,和栏目多(几百个栏目)。本来考虑solr和es,但是项目周期短,所以最后选型为Ehcache。
技术点
Spring boot
mybatis
thymeleaf
mybatis-ehcache
Ehcache使用
废话不多说,我们开始整合吧(Springboot基础入门部分就不赘述了)。
添加相关maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--整合mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.mybatis.caches</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.1.0</version> </dependency>
ehcache 配置文件
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir/Tmp_EhCache" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="3600" memoryStoreEvictionPolicy="LRU" /> <cache name="content" maxEntriesLocalHeap="200" timeToLiveSeconds="3600"> </cache></ehcache>
application.properties支持
spring.cache.ehcache.config=classpath:ehcache.xml
Service层使用示例
使用注解的方式标明缓存kv
缓存注解有以下三个:
@Cacheable :先从缓存中查询,如果没有则查询数据库@CacheEvict :清空缓存 @CachePut : 更新的时候刷新到缓存
更多关于Ehcache的文档,可参阅官方Ehcache文档
//这里是做最简单的演示 //这里的value就是对应配置文件里面的名称 @Cacheable(value = "content") @Override public List<User> getUser(Map map1) { String sql=getSql(map1); return UserMapper.selectBySql(sql); }
mapper.xml配置
在相关配置文件中追加下面的代码即可.主要配置的是空闲时间,存活时间,最大堆,本地存储,内存策略等。
<cache type="org.mybatis.caches.ehcache.EhcacheCache" > <property name="timeToIdleSeconds" value="3600"/><!--1 hour--> <property name="timeToLiveSeconds" value="3600"/><!--1 hour--> <property name="maxEntriesLocalHeap" value="1000"/> <property name="maxEntriesLocalDisk" value="10000000"/> <property name="memoryStoreEvictionPolicy" value="LRU"/> </cache>
至此,Ehcache就配置完毕,可以放心使用了。
问题
在使用过程当中,有遇到不同页面使用同几个缓存的时候,其他页面可以正常使用缓存,其中的一个页面一直是从数据库读取数据,不走缓存。
查阅后发现,此接口中更新了对象。最初设置的@CachePut ,后来发现换成@cacheable也不行,取消注解也不可以。最后一气之下把更新取消了,发现神奇的走缓存了。坑爹啊,然而又是必须要更新的,怎么办呢?是不是更新的时候可以设置什么属性呢?
设置flushCache 属性为false,默认是true,即更新的时候会刷新缓存。
<update id="updateUser" flushCache="false"> UPDATE user set loginIp=#{ip} WHERE id=#{id} </update>
结语
使用了Ehcache之后,除了第一次比较缓慢之外,之后的都比较流畅,额外需要注意的是配置好缓存相关的策略,缓存刷新等。
作者
ricky
交流群:244930845
阅读全文
0 0
- SpringBoot整合Ehcache
- springboot同时整合redis和ehcache
- SpringBoot学习-(十八)SpringBoot整合EhCache
- springboot Ehcache
- SpringBoot项目实战--ehcache
- SpringBoot-EhCache使用
- SpringBoot集成ehcache
- SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
- SpringBoot系列(7)---SpringBoot-Cache(EhCache)
- mybatis 与 ehcache 整合
- Spring整合EhCache缓存
- SpringMVC整合Ehcache
- spring+hibernate整合ehcache
- EhCache与spring整合
- SpringMVC整合Ehcache
- Mybatis与Ehcache整合
- Ehcache整合spring配置
- Spring整合ehcache
- 导入导出Excel文件
- nodejs 构建本地web测试服务器 以及 解决访问静态资源的问题!有完整源码!
- React学习总结—生命周期
- codeforces838A Binary Blocks
- unity3D学习之音频基础原理-audio菜鸟笔记3
- SpringBoot整合Ehcache
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) 莫队算法
- 转载:推荐大学生看的一些书
- Linux解压命令
- 【C语言】队列演示(队列的几个函数)
- 选择排序实现
- (树状数组)hdu1166 敌兵布阵
- (转)一个由自由职业者建立的量化对冲基金
- 【C语言】双链表常用函数