NutzCN Logo
问答 来来来,兽哥!来聊下Ehcache怎么玩!
发布于 3371天前 作者 明天会吹什么风 3820 次浏览 复制 上一个帖子 下一个帖子
标签: ehcache

发现了个有意思的,搞了老久才发现毛病,但是你懂得,屌丝找不出原理!
开始重现了:

Account account = new Account();
account.setMobile(username);
dao.fetch(Account.class, Cnd.from(dao, account));

这是一段没毛病的代码,运行正常的。
好的,加上Ehcache吧!
按照配置,配置了需要缓存的表
按照nutzbookdao.js

...
cachedTableNames : ['accounts']
...

抛锚了,
注意sql,: org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:120) DEBUG - sql = SELECT * FROM accounts WHERE (mobile=?) LIMIT 0, 1 , tables = [accounts]
有点儿意思!
兽哥v5!!!

44 回复

看不出啥抛锚了
自豪地采用 NutzCN ionic

楼主的意思是没有缓存?还是执行了SQL?
貌似配置表名之后还要手动缓存。。。
也许是我学艺不精。。。求鞭策!

@qq_74967e40 不需要手动缓存,第一次查询的时候因为没有命中,所以查数据库,并自动存入缓存,第二次查自然命中了,不再会查数据库,直到缓存失效(有插入,删除,更新,或idle时间到了
自豪地采用 NutzCN ionic

那是我哪儿配置错了把,我都是手动。不然,每次都显示执行了SQL

我很好奇你怎么手动的
自豪地采用 NutzCN ionic

Element ele = cacheManager.getCache("mz").get(eleKey);
if(null == ele){
queryResult = ...
cacheManager.getCache("mz").put(new Element(eleKey,queryResult));
}else{
queryResult = (QueryResult) ele.getObjectValue();
}

就是这样

@qq_74967e40 这属于应用层管理一个Cache,不是daocache做的事。 daocache做的事,可以这样理解 dao.fetch多次,相同条件,仅第一次会执行数据库查询,剩下N次都从缓存取。
自豪地采用 NutzCN ionic

@wendal 是不是分页的时候,是不会缓存的?还是说会按照配置里的表来,对这个表的查询都会缓存?

相同查询条件相同分页,生成的sql一样,参数一样,才算匹配
自豪地采用 NutzCN ionic

@qq_74967e40 分页与否都会缓存
自豪地采用 NutzCN ionic

@wendal 我不知道哪儿有问题,就是不会自动缓存。。。
项目启动不会报错
catchManager也没问题,能报启动信息。
dao里配置也照搬的
不明白了

@qq_74967e40 有一种可能性是调用时在Trans.exec的作用域
自豪地采用 NutzCN ionic

默认配置下,如果在事务内,不读缓存
自豪地采用 NutzCN ionic

异常是这样的2015-10-08 10:20:05,478 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:145) DEBUG - Get 'userService'<class vc.thinker.module.auth.service.UserService>
2015-10-08 10:20:18,516 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:120) DEBUG - sql = SELECT * FROM accounts WHERE (mobile=?) LIMIT 0, 1 , tables = [accounts]
2015-10-08 10:20:18,517 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:129) DEBUG - KEY=2b60e8856c656bdf4c18c9afabc4d33871638c9c:5aaa4c20710f40c431b5cf79f78912614334e74b SQL=SELECT * FROM accounts WHERE (mobile=?) LIMIT 0, 1
org.nutz.dao.DaoException: java.lang.NullPointerException
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:78)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:219)
at org.nutz.dao.impl.NutDao.fetch(NutDao.java:549)
at vc.thinker.module.auth.service.UserService.fetchByUsername(UserService.java:61)
at vc.thinker.shiro.realm.NutDaoRealm.doGetAuthenticationInfo(NutDaoRealm.java:109)
at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:568)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:270)
at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
at vc.thinker.module.auth.router.UserRouter.signin(UserRouter.java:111)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at vc.thinker.mvc.processor.DemoProcessor.process(DemoProcessor.java:28)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at vc.thinker.shiro.NutShiroProcessor.process(NutShiroProcessor.java:53)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at vc.thinker.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:25)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:40)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:183)
at vc.thinker.mvc.AppFilter.doFilter(AppFilter.java:47)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.NullPointerException
at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.getCache(EhcacheDaoCacheProvider.java:44)
at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.get(EhcacheDaoCacheProvider.java:21)
at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:130)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:264)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:64)
... 68 more

2015-10-08 10:20:05,478 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:145) DEBUG - Get 'userService'<class vc.thinker.module.auth.service.UserService>
2015-10-08 10:20:18,516 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:120) DEBUG - sql = SELECT * FROM accounts  WHERE (mobile=?) LIMIT 0, 1 , tables = [accounts]
2015-10-08 10:20:18,517 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:129) DEBUG - KEY=2b60e8856c656bdf4c18c9afabc4d33871638c9c:5aaa4c20710f40c431b5cf79f78912614334e74b SQL=SELECT * FROM accounts  WHERE (mobile=?) LIMIT 0, 1 
org.nutz.dao.DaoException: java.lang.NullPointerException
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:78)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:219)
	at org.nutz.dao.impl.NutDao.fetch(NutDao.java:549)
	at vc.thinker.module.auth.service.UserService.fetchByUsername(UserService.java:61)
	at vc.thinker.shiro.realm.NutDaoRealm.doGetAuthenticationInfo(NutDaoRealm.java:109)
	at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:568)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
	at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
	at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:270)
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
	at vc.thinker.module.auth.router.UserRouter.signin(UserRouter.java:111)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at vc.thinker.mvc.processor.DemoProcessor.process(DemoProcessor.java:28)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at vc.thinker.shiro.NutShiroProcessor.process(NutShiroProcessor.java:53)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at vc.thinker.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:25)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:40)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:183)
	at vc.thinker.mvc.AppFilter.doFilter(AppFilter.java:47)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
	at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
	at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
	at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
	at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
	at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
	at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
	at org.mortbay.jetty.Server.handle(Server.java:326)
	at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
	at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
	at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
	at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
	at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
	at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
	at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.NullPointerException
	at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.getCache(EhcacheDaoCacheProvider.java:44)
	at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.get(EhcacheDaoCacheProvider.java:21)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:130)
	at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:264)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:64)
	... 68 more

执行语句dao.fetch(Account.class, Cnd.from(dao, account));

daocache版本不是最新吧?

是最新的,版本跟nutbook一致,
不加... cachedTableNames : ['accounts'] ...这个还好,
加了就不行

既然是抛空指针,那找出来什么变量为null。目测是ehcacheManager为null,然后ehcache.js里面的注释没看清楚。里面有写,如果单独用ehcache,是用create,如果是与shiro共用,就用get
自豪地采用 NutzCN ionic

	public void init(NutConfig conf) {
		Ioc ioc = conf.getIoc();
		dao = ioc.get(Dao.class,"dao");
		((NutDao) dao).setSqlManager(new FileSqlManager("sqls/base_views.sql","sqls/query.sql"));
		// ecache
		CacheManager cacheManager = ioc.get(CacheManager.class);
		CachedNutDaoExecutor.DEBUG = true;// 开启详细日志
		//log.debug("Ehcache CacheManager = " + cacheManager);
    }

dao.js

var ioc = {
	conf : {
		type : "org.nutz.ioc.impl.PropertiesProxy",
		fields : {
			paths : [ "customer/" ]
		// 获取customer文件下配置
		}
	},
	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",// mergeStat是带合并的sql状态过滤器
			connectionProperties : "druid.stat.slowSqlMillis=2000"// 2000代表如果sql执行超过2秒,就输出日志
		}
	},
	dao : {
		type : "org.nutz.dao.impl.NutDao",
		args : [ {
			refer : "dataSource"
		} ],
		fields : {
			executor : {
				refer : "cacheExecutor"
			}
		}
	},
	
	cacheExecutor : {
		type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
		fields : {
			cacheProvider : {
				refer : "cacheProvider"
			},
			// 需要缓存的表名
			cachedTableNames : [
//			 "accounts","users", "roles", "permissions", "role_permission","divisions","organs"
			],
			enableWhenTrans : false, // 事务作用域内是否启用,默认false
			cache4Null : true // 是否缓存空值,默认true
		}
	},
	/*
	// 基于内存的简单LRU实现
	cacheProvider : {
		type : "org.nutz.plugins.cache.dao.impl.provider.MemoryDaoCacheProvider",
		fields : {
			cacheSize : 10000 // 缓存的对象数
		},
		events : {
			create : "init"
		}
	}
	*/
	// 基于Ehcache的DaoCacheProvider
	cacheProvider : {
		type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
		fields : {
			cacheManager : {
				refer : "cacheManager"
			}
		// 引用ehcache.js中定义的CacheManager
		},
		events : {
			create : "init"
		}
	}
    };

关键的ehcache.js 没贴

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

用了shiro没? shiro里面用了ehcache没?

话说ehcache.js里面注释已经说得很清楚了吧
自豪地采用 NutzCN ionic

shiro.ini

[main]

###########Session
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager


############# Session Cache
sessionDAO = org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionManager.sessionDAO = $sessionDAO
securityManager.sessionManager = $sessionManager

############# cookie
sessionIdCookie=org.apache.shiro.web.servlet.SimpleCookie
sessionIdCookie.name=sid
sessionIdCookie.maxAge=946080000
sessionIdCookie.httpOnly=true
sessionManager.sessionIdCookie=$sessionIdCookie
sessionManager.sessionIdCookieEnabled=true
# 3,600,000 milliseconds = 1 hour
#timeout:-1000ms 永不超时
sessionManager.globalSessionTimeout = -1000

# use R.UU32()
sessionIdGenerator = vc.thinker.shiro.AppSessionIdGenerator
securityManager.sessionManager.sessionDAO.sessionIdGenerator = $sessionIdGenerator

authenticator=vc.thinker.shiro.ModularRealmAuthenticator2
securityManager.authenticator=$authenticator

;此处的加密算法必须和业务的加密算法一致
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm = vc.thinker.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher
nutzdao_realm.cacheManager = $cacheManager

authc = vc.thinker.shiro.CaptchaCodeFormAuthenticationFilter

;将shiro的登录路径映射到本项目module的登录
authc.loginUrl = /signin
;注销后页面跳转路径
;logout.redirectUrl = /logout

;###########配置ehcache
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager


[urls]
;配置注销url
;/logout = logout

/durid/** =anon
;静态资源
/public/**        = anon, noSessionCreation
/signup           = anon
/signin           = anon
/signin/code      = anon
/signup/code      = anon
/captcha/next     = anon
/admin            = anon

/**     = anon

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="app">
	<!-- 关键点在name=app,让shiro和app共享一个echache实例 -->
	<!-- <diskStore path="java.io.tmpdir/shiro-ehcache"/> -->
	<diskStore path="java.io.tmpdir/app" />
 <diskStore path="/data/app_cache/" />
	
	<cache name="shiro-activeSessionCache"
       maxElementsInMemory="1000"
       overflowToDisk="true"
       eternal="true"
       timeToLiveSeconds="0"
       timeToIdleSeconds="0"
       diskPersistent="true"
       diskExpiryThreadIntervalSeconds="600"/>
       
	<defaultCache maxElementsInMemory="1000" eternal="false"
		timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="false"
		diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
		statistics="false" />
		
	<cache name="app_avatar" maxEntriesLocalHeap="100"
		maxEntriesLocalDisk="100000" overflowToDisk="true" diskPersistent="true"
		diskSpoolBufferSizeMB="20" timeToIdleSeconds="0" timeToLiveSeconds="0"
		memoryStoreEvictionPolicy="LFU"
		eternal="true">
	</cache>
</ehcache>

因为报错信息里面指向这一行:

at org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider.getCache(EhcacheDaoCacheProvider.java:44)

Cache cache = cacheManager.getCache(name);

所以是cacheManager为null

然后呢, 你在MainSetup里面有这样的代码

		CacheManager cacheManager = ioc.get(CacheManager.class);
		CachedNutDaoExecutor.DEBUG = true;// 开启详细日志
		//log.debug("Ehcache CacheManager = " + cacheManager);

最后的 log.debug 被注释了, 取消注释, 看看打印的是不是null吧, 估计也是

所以呢, 问题就出在ehcache.js的了, 然后你提供的shiro.ini配置显示ehcache是启用了(而且名字就叫app), 理应是shiro初始化完成就有ehcache实例

可能性有:

  1. Shiro/ShiroFilter比NutFilter晚初始化
  2. Shiro就没启动
//动作链。以下配置仅比默认配置多了一个vc.thinker.mvc.LogTimeProcessor
var chain = {
	"default" : {
		"ps" : [ 
			"vc.thinker.mvc.processor.LogTimeProcessor",//自定义
			"org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor",
			"org.nutz.mvc.impl.processor.EncodingProcessor",
			"org.nutz.mvc.impl.processor.ModuleProcessor",
			"vc.thinker.shiro.NutShiroProcessor",//自定义
//			"ioc:demoProcessor",//自定义,直接注入ioc bean
			"vc.thinker.mvc.processor.DemoProcessor",
			"org.nutz.mvc.impl.processor.ActionFiltersProcessor",
			"org.nutz.mvc.impl.processor.AdaptorProcessor",
			"!org.nutz.plugins.validation.ValidationProcessor",//默认不启用,去掉 '!'可改为启用
			"org.nutz.mvc.impl.processor.MethodInvokeProcessor",
			"org.nutz.mvc.impl.processor.ViewProcessor" 
		],
		"error" : 'org.nutz.mvc.impl.processor.FailProcessor'
	}
};

可能性有:

  1. Shiro/ShiroFilter比NutFilter晚初始化, web.xml里面顺序
  2. Shiro就没启动
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>ttt</display-name>
	<listener>
		<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>com.xxxx.mvc.AppSessionListener</listener-class>
	</listener>
	<filter>
		<filter-name>ShiroFilter</filter-name>
		<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>ShiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
		<dispatcher>ERROR</dispatcher>
	</filter-mapping>
	
	<filter>
		<filter-name>DruidWebStatFilter</filter-name>
		<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
		<init-param>
			<param-name>exclusions</param-name>
			<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,/rs/*</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>DruidWebStatFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<filter>
		<filter-name>AppFilter</filter-name>
		<filter-class>com.xxxx.mvc.AppFilter</filter-class>
		<init-param>
			<param-name>modules</param-name>
			<param-value>com.xxxx.AppModule</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>AppFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>INCLUDE</dispatcher>
	</filter-mapping>
	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>DruidStatView</servlet-name>
		<url-pattern>/druid/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<error-page>
		<error-code>500</error-code>
		<location>/WEB-INF/500.jsp</location>
	</error-page>
</web-app>

把启动日志贴到 短点 然后把地址贴过来吧
自豪地采用 NutzCN ionic

真神奇, 看看你得debug一下CacheManager的getCacheManager方法了, 看看传入的name是啥, 然后CACHE_MANAGERS_MAP里面有啥

    public static CacheManager getCacheManager(String name) {
        synchronized (CacheManager.class) {
            if (name == null) {
                name = DEFAULT_NAME;
            }
            return CACHE_MANAGERS_MAP.get(name);
        }
    }

这里的name 和配置的一致

唉。。。。。。。。。。。。。。

终于发现点痕迹, 你的日志里面有这样一段

2015-10-10 11:35:33,663 org.apache.shiro.realm.AuthorizingRealm.getAuthorizationCacheLazy(AuthorizingRealm.java:242) DEBUG - CacheManager [org.apache.shiro.cache.ehcache.EhCacheManager@17088d81] has been configured.  Building authorization cache named [nutzdao_realm.authorizationCache]
2015-10-10 11:35:33,664 org.apache.shiro.cache.ehcache.EhCacheManager.ensureCacheManager(EhCacheManager.java:206) DEBUG - cacheManager property not set.  Constructing CacheManager instance... 
2015-10-10 11:35:33,672 org.apache.shiro.io.ResourceUtils.loadFromClassPath(ResourceUtils.java:159) DEBUG - Opening resource from class path [org/apache/shiro/cache/ehcache/ehcache.xml]
2015-10-10 11:35:33,676 net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:150) DEBUG - Configuring ehcache from InputStream
2015-10-10 11:35:33,710 net.sf.ehcache.config.DiskStoreConfiguration.translatePath(DiskStoreConfiguration.java:141) DEBUG - Disk Store Path: /tmp/shiro-ehcache

正常是这样, 找到一个自定义的ehcache.xml

2015-10-10 14:11:15,293 org.apache.shiro.realm.AuthorizingRealm.getAuthorizationCacheLazy(AuthorizingRealm.java:242) DEBUG - CacheManager [org.apache.shiro.cache.ehcache.EhCacheManager@12a5143] has been configured.  Building authorization cache named [nutzdao_realm.authorizationCache]
2015-10-10 14:11:15,293 org.apache.shiro.cache.ehcache.EhCacheManager.ensureCacheManager(EhCacheManager.java:206) DEBUG - cacheManager property not set.  Constructing CacheManager instance... 
2015-10-10 14:11:15,322 org.apache.shiro.io.ResourceUtils.loadFromClassPath(ResourceUtils.java:159) DEBUG - Opening resource from class path [ehcache.xml]
2015-10-10 14:11:15,327 net.sf.ehcache.config.ConfigurationFactory.parseConfiguration(ConfigurationFactory.java:150) DEBUG - Configuring ehcache from InputStream
2015-10-10 14:11:15,527 net.sf.ehcache.config.BeanHandler.setAttribute(BeanHandler.java:271) DEBUG - Ignoring ehcache attribute xmlns:xsi
2015-10-10 14:11:15,528 net.sf.ehcache.config.BeanHandler.setAttribute(BeanHandler.java:271) DEBUG - Ignoring ehcache attribute xsi:noNamespaceSchemaLocation
;###########配置ehcache
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager

这段要配置在前

1.首先是顺序问题

;###########配置ehcache
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager

;此处的加密算法必须和业务的加密算法一致
sha256Matcher = org.apache.shiro.authc.credential.Sha256CredentialsMatcher
nutzdao_realm.cacheManager = $cacheManager
nutzdao_realm = vc.thinker.shiro.realm.NutDaoRealm
nutzdao_realm.credentialsMatcher = $sha256Matcher

2.ehcache.xml中配置的缓存路径.这里配置多了,还有要么就是创建不了

<diskStore path="java.io.tmpdir/app" />
<diskStore path="/data/app_cache/" />

至今最长的楼... 终于解决了

论看清楚注释的重要性

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