前提,整合好springmvc + mybatis + ehcache https://blog.csdn.net/u013628152/article/details/51442621
在mapper.xml文件加入,在<cache>
节点中配置ecache,就不需要额外的.ecache.xml
配置了
1 2 3 4 5 6 7 8 |
<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> --> <cache type="org.mybatis.caches.ehcache.LoggingEhcache" > <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> |
如果哪个SQL不想使用缓存:
1 2 3 |
<select id="selectByMap" parameterType="map" resultMap="CmsMediaMap" useCache="false"> ...... </select> |
拓展说明:
对于清除缓存的方法,ehcache提供了两种,一种是在ehcache.xml中配置的时间过后自动清除,一种是在数据发生变化后触发清除。个人感觉第二种比较好。可以将
@TriggersRemove(cacheName=”userCache“,removeAll=true)
@TriggersRemove(cacheName=”userCache“, when=When.AFTER_METHOD_INVOCATION, removeAll=true)
这句代码加到service里面的添加、删除、修改方法上。这样只要这几个方法有调用,缓存自动清除。