SpringBoot整合Ehcache
2017-08-07 21:17
295 查看
前言
近段时间在做一个类似cms的项目,在做完几十万的数据导入后,发现各种查询异常缓慢,数据库(单库,暂时服务器没那么多),数据库层面的索引各种优化前期也已经做过,单笔查询0.5s左右,但架不住量多,和栏目多(几百个栏目)。本来考虑solr和es,但是项目周期短,所以最后选型为Ehcache。技术点
Spring bootmybatis
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
相关文章推荐
- springboot同时整合redis和ehcache
- spring boot spring cache ehcache3.x整合
- 【spring-boot】spring-boot整合ehcache实现缓存机制
- spring-boot整合ehcache实现缓存机制的方法
- spring-boot之spring-boot整合ehcache实现缓存机制
- SpringBoot整合EHcache学习笔记
- springboot整合ehcache 实现支付超时限制的方法
- spring boot ehcache整合
- spring-boot整合ehcache实现缓存机制
- Spring Boot整合Ehcache实现缓存功能
- Springboot整合集成ehcache3
- Spring Boot整合ehcache3.0 缓存
- spring boot spring cache ehcache3.x整合
- SpringBoot 整合Ehcache3
- spring-boot整合ehcache实现缓存机制
- 详解springboot整合ehcache实现缓存机制
- springboot同时整合redis和ehcache
- spring boot spring cache ehcache3.x整合
- SpringBoot学习-(十八)SpringBoot整合EhCache