首页 新闻 会员 周边

spring+mybatis+ehcache 缓存配置问题

0
[待解决问题]

使用ehcache 作为mybatis的缓存,发现即使查询时传的参数变动了,还是从之前的缓存中读取,不根据新的查询条件进行查询,导致查询出的数据不正确,如何解决

 

 我的分页是通过拦截器拦截,然后拼接分页语句查询的,传的page,size这些改变是从缓存读,如果是其他的就会重新读数据库。是什么原因呢》

分页拦截器代码如下:

public Object intercept(Invocation ivk) throws Throwable {
        if (ivk.getTarget() instanceof RoutingStatementHandler) {
            RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
            BaseStatementHandler handler = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
            MappedStatement ms = (MappedStatement) ReflectHelper.getValueByFieldName(handler, "mappedStatement");
            BoundSql bs = handler.getBoundSql();
            Object param = bs.getParameterObject();
            String sql = bs.getSql();
            if (param instanceof HashMap) {
                HashMap map = ((HashMap) param);
                try {
                    if (map.get(PAGE_KEY) != null) {
                        Page p = (Page)map.get(PAGE_KEY);
                        if(p.isWhePage() && p.getSize() != Integer.MAX_VALUE) {
                            p.setTotal(queryTotal(ivk, ms, bs, param, sql));
                            if(p.getTotal() > 0 && (p.getMaxCount() == 0 || (p.getMaxCount() > 0 && p.getTotal() <= p.getMaxCount()))) {
                                set(p);
                                ReflectHelper.setValueByFieldName(bs, "sql", pageSql(sql, p));
                                if(p.getCollect().size() > 0) {
                                    queryCollect(ivk, ms, bs, param, p, sql);
                                }
                            } else {
                                set(p);
                                ReflectHelper.setValueByFieldName(bs, "sql", pageSql(sql));
                            }
                        } else {
                            set(p);
                            ReflectHelper.setValueByFieldName(bs, "sql", pageSql(sql, p));//pageSql() 将sql转成分页语句
 } } } catch (BindingException e) { } } } return ivk.proceed(); }

 

配置如下:

<!-- 开启spring缓存 -->
	<cache:annotation-driven cache-manager="cacheManager" />
	<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation" value="classpath:ehcache/ehcache-shiro.xml"/>
		<property name="shared" value="true"></property>
	</bean>
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
		<property name="cacheManager" ref="cacheManagerFactory"/>
	</bean>

	<!-- 用户授权信息Cache, 采用EhCache -->
	<bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManager" ref="cacheManagerFactory" />
		<!--<property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache-shiro.xml"/>-->
	</bean>

 

 

ehcache-shiro.xml配置如下

<?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">
    <diskStore path="java.io.tmpdir" />
    <defaultCache eternal="false" maxElementsInMemory="1000"
                  overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
                  timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
    <cache name="baseCache" eternal="false" maxElementsInMemory="100"
           overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
           timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />
</ehcache>

 

 

mybatis-mapper.xml中加了如下配置:

<cache type="org.mybatis.caches.ehcache.LoggingEhcache" >
		<property name="timeToIdleSeconds" value="3600"/>
		<property name="timeToLiveSeconds" value="3600"/>
		<property name="maxEntriesLocalHeap" value="1000"/>
		<property name="maxEntriesLocalDisk" value="10000000"/>
		<property name="memoryStoreEvictionPolicy" value="LRU"/>
	</cache>

 

 

求帮助

烟雨凌的主页 烟雨凌 | 初学一级 | 园豆:197
提问于:2016-10-25 17:21
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册