NutzCN Logo
问答 Ehcache不能命中,请看看是什么问题
发布于 3128天前 作者 qq_0f18c1df 2124 次浏览 复制 上一个帖子 下一个帖子
标签:

问题描述:
访问/system/log-queryJsonGrid一直在数据库中查询,不会被ehcache命中,没有报错信息。

依赖的jar版本:
nutz-1.b.53.jar
nutz-plugins-daocache-1.r.56.jar
ehcache-2.10.2.jar

相关配置文件
ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
    monitoring="autodetect" dynamicConfig="true" name="hq">
    <!-- <diskStore path="java.io.tmpdir/shiro-ehcache"/> -->
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />
    <cache name="shiro-activeSessionCache"
           maxElementsInMemory="10000"
           overflowToDisk="true"
           eternal="true"
           timeToLiveSeconds="0"
           timeToIdleSeconds="0"
           diskPersistent="true"
           diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

shiro.ini

[main]
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = cn.calensoft.shiro.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher

authc = cn.calensoft.shiro.CaptchaFormAuthenticationFilter
authc.loginUrl  = /loginCheck.do
logout.redirectUrl= /login.do

cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
nutzdao_realm.cacheManager = $cacheManager

[urls]
/logout.do = logout 
/loginCheck.do = authc

ehcache.js

var ioc = {
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#getCacheManager",
            args : ["hq"] // 对应shiro.ini中指定的ehcache.xml中定义的name
        }
        /*      
        // 如果不需要shiro初始化的Ehcache, 使用下面的方式配置
        cacheManager : {
            type : "net.sf.ehcache.CacheManager",
            factory : "net.sf.ehcache.CacheManager#create"
        }
         */
};

dao.js

var ioc = {
	conf : {
		type : "org.nutz.ioc.impl.PropertiesProxy",
		fields : {
			paths : [ "custom/" ]
		}
	},
	dataSource : {
		type : "com.alibaba.druid.pool.DruidDataSource",
		events : {
			create : "init",
			depose : 'close'
		},
		fields : {
			url : {
				java : "$conf.get('db.url')"
			},
			username : {
				java : "$conf.get('db.username')"
			},
			password : {
				java : "$conf.get('db.password')"
			},
			testWhileIdle : true,
			validationQuery : {
				java : "$conf.get('db.validationQuery')"
			},
			maxActive : {
				java : "$conf.get('db.maxActive')"
			},
			filters : "mergeStat",
			connectionProperties : "druid.stat.slowSqlMillis=2000"
		}
	},
	dao : {
		type : "org.nutz.dao.impl.NutDaoExt",
		args : [ {
			refer : "dataSource"
		} ],
        fields : {
            executor : {refer:"cacheExecutor"}
        }
	},
    cacheExecutor : {
        type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
        fields : {
            cacheProvider : {refer:"cacheProvider"},
            // 需要缓存的表名
            cachedTableNames : ["t_system_log","t_system_jqgrid","v_system_jqgrid_col"
                                //"t_user", "t_role", "t_permission", "t_role_permission"
                                ],
                                enableWhenTrans : false, // 事务作用域内是否启用,默认false
                    			cache4Null : true // 是否缓存空值,默认true
        }
    },
    // 基于Ehcache的DaoCacheProvider
    cacheProvider : {
        type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
        fields : {
            cacheManager : {refer:"cacheManager"} // 引用ehcache.js中定义的CacheManager
        },
        events : {
            create : "init"
        }
    }
};

调用部分代码
BaseService.java

/**
 * 
 * @ClassName BaseService
 * @Description TODO 基础服务
 */
@InjectName
@IocBean(fields = {"dao"})
public class BaseService extends IdEntityService<Log> {
	
	@Inject
	private I18NModule i18NModule;
	
	@Inject protected CacheManager cacheManager;
	

LogModule.java

package cn.calensoft.module.system;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import cn.calensoft.bean.system.Log;
import cn.calensoft.service.BaseService;
import cn.calensoft.tool.Constants;
import cn.calensoft.util.jqgrid.JqFormBean;

/**
 * 
 * @ClassName LogModule
 * @Description TODO 系统日志
 * @date 2015-4-13
 */
@IocBean
public class LogModule {
	
	
	@Inject
	private BaseService baseService;
	
	/**
	 * 
	 * @Method: main 
	 * @Description: TODO 跳转页面
	 * @param 
	 * @return void
	 * @throws
	 */
	@At("/system/log-main")
	@Ok("jsp:page.system.log-main")
	public void main(){
	}

	/**
	 * 
	 * @Method: queryJsonGrid
	 * @Description: TODO 查询数据源构建
	 * @param @param request
	 * @param @return
	 * @return JqFormBean
	 * @throws
	 */
	@At("/system/log-queryJsonGrid")
	public JqFormBean queryJsonGrid(HttpServletRequest request) {
		JqFormBean jq = JqFormBean.convertParasToBean(request);
		return baseService.query(jq,Log.class,Constants.EXPORT_LOG);
	}
}

6 回复

哪里看出没有缓存到?

后台DEBUG信息中,每次相同请求还是向数据库发送查询,我没有配置事务

2016-05-30 13:42:36,951 [http-bio-8080-exec-10] DEBUG org.nutz.mvc.impl.UrlMappingImpl - Found mapping for [POST] path=/system/log-queryJsonGrid : LogModule.queryJsonGrid(...)
2016-05-30 13:42:36,951 [http-bio-8080-exec-10] DEBUG org.nutz.ioc.impl.NutIoc - Get 'logModule'<class cn.calensoft.module.system.LogModule>
2016-05-30 13:42:36,956 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.NutDao - NutDao[_selfId=110] init ...
2016-05-30 13:42:36,956 [http-bio-8080-exec-10] INFO  org.nutz.dao.jdbc.Jdbcs - Get Connection from DataSource for JdbcExpert
2016-05-30 13:42:36,958 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Driver --> mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} )
2016-05-30 13:42:36,958 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name   --> MySQL-AB JDBC Driver
2016-05-30 13:42:36,958 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL    --> jdbc:mysql://localhost:3306/hq?useUnicode=true&characterEncoding=UTF-8
2016-05-30 13:42:36,963 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_client=utf8
2016-05-30 13:42:36,963 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_connection=utf8
2016-05-30 13:42:36,964 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_database=utf8
2016-05-30 13:42:36,964 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_filesystem=binary
2016-05-30 13:42:36,965 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_results=
2016-05-30 13:42:36,965 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_server=utf8
2016-05-30 13:42:36,966 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_system=utf8
2016-05-30 13:42:36,967 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_sets_dir=C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\
2016-05-30 13:42:36,969 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> MYSQL:[MySQL - 5.5.29]
2016-05-30 13:42:36,970 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.NutDao - NutDao[_selfId=111] init ...
2016-05-30 13:42:36,970 [http-bio-8080-exec-10] INFO  org.nutz.dao.jdbc.Jdbcs - Get Connection from DataSource for JdbcExpert
2016-05-30 13:42:36,972 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Driver --> mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} )
2016-05-30 13:42:36,972 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name   --> MySQL-AB JDBC Driver
2016-05-30 13:42:36,973 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL    --> jdbc:mysql://localhost:3306/hq?useUnicode=true&characterEncoding=UTF-8
2016-05-30 13:42:36,977 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_client=utf8
2016-05-30 13:42:36,977 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_connection=utf8
2016-05-30 13:42:36,978 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_database=utf8
2016-05-30 13:42:36,978 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_filesystem=binary
2016-05-30 13:42:36,979 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_results=
2016-05-30 13:42:36,979 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_server=utf8
2016-05-30 13:42:36,979 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_system=utf8
2016-05-30 13:42:36,980 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_sets_dir=C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\
2016-05-30 13:42:36,982 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> MYSQL:[MySQL - 5.5.29]
2016-05-30 13:42:36,983 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.NutDao - NutDao[_selfId=112] init ...
2016-05-30 13:42:36,983 [http-bio-8080-exec-10] INFO  org.nutz.dao.jdbc.Jdbcs - Get Connection from DataSource for JdbcExpert
2016-05-30 13:42:36,985 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Driver --> mysql-connector-java-5.1.7 ( Revision: ${svn.Revision} )
2016-05-30 13:42:36,985 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC Name   --> MySQL-AB JDBC Driver
2016-05-30 13:42:36,986 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - JDBC URL    --> jdbc:mysql://localhost:3306/hq?useUnicode=true&characterEncoding=UTF-8
2016-05-30 13:42:36,990 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_client=utf8
2016-05-30 13:42:36,991 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_connection=utf8
2016-05-30 13:42:36,991 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_database=utf8
2016-05-30 13:42:36,992 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_filesystem=binary
2016-05-30 13:42:36,992 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_results=
2016-05-30 13:42:36,992 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_server=utf8
2016-05-30 13:42:36,993 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_set_system=utf8
2016-05-30 13:42:36,993 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Mysql : character_sets_dir=C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\
2016-05-30 13:42:36,995 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.DaoSupport - Database info --> MYSQL:[MySQL - 5.5.29]
2016-05-30 13:42:37,007 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT COUNT(*) FROM t_system_log 
2016-05-30 13:42:37,022 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.sql.run.NutDaoExecutor - SELECT * FROM t_system_log  ORDER BY id ASC   LIMIT 0, 20 

每次新建一个NutDao,作死啊

2016-05-30 13:42:36,970 [http-bio-8080-exec-10] DEBUG org.nutz.dao.impl.NutDao - NutDao[_selfId=111] init ...

通过@Inject等方式获取Dao实例啊, 不要new NutDao啊

谢谢提醒,之前测试一个应用要用到两个DAO,没有注意到这个问题,调整回来了,完美运行了。

如果不需要daocache,那么直接声明个cacheManager 就好了

ehcache.js

{
    cacheManager : {
        type : "net.sf.ehcache.CacheManager",
        factory : "net.sf.ehcache.CacheManager#create" // 这是工厂方法的强大之处
    }
}

添加回复
请先登陆
回到顶部