shiro的缓存到底开不开
来源:互联网 发布:云计算应用场景举例 编辑:程序博客网 时间:2024/04/28 22:12
最近使用shiro时,遇到了一个奇怪的问题,就是doGetAuthorizationInfo
这个方法,会被重复调用,确切的说是每次有请求时,就会被调用。众所周知,此方法是用来给当前登录的用户加载权限的,频繁的调用此方法势必会降低网站的响应速度,那问题在哪呢?
我翻看了shiro源码,其中AuthorizingRealm
的部分源码如下:
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) { if (principals == null) { return null; } AuthorizationInfo info = null; if (log.isTraceEnabled()) { log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]"); } //获取缓存对象 Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache(); if (cache != null) { if (log.isTraceEnabled()) { log.trace("Attempting to retrieve the AuthorizationInfo from cache."); } Object key = getAuthorizationCacheKey(principals); info = cache.get(key); if (log.isTraceEnabled()) { if (info == null) { log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]"); } else { log.trace("AuthorizationInfo found in cache for principals [" + principals + "]"); } } } //如果缓存中没有授权信息,就调用doGetAuthorizationInfo进行授权 if (info == null) { // Call template method if the info was not found in a cache info = doGetAuthorizationInfo(principals); // If the info is not null and the cache has been created, then cache the authorization info. if (info != null && cache != null) { if (log.isTraceEnabled()) { log.trace("Caching authorization info for principals: [" + principals + "]."); } Object key = getAuthorizationCacheKey(principals); cache.put(key, info); } } return info; }
可以看到,shiro先是取缓存中获取授权信息的,当获取不到时,就会重新调用doGetAuthorizationInfo
方法进行获取。而缓存本身是通过getAvailableAuthorizationCache
这个方法获取的。之前听别人说使用shiro时可以不开启缓存,当时测试了下,还真可以,毕竟就不用引用ehcache那个东西了,而且在这个项目中,使用ehcache这么“重量级”的东西有点大材小用,所以当时就给关了。而且经过测试,确实可以正常运行,所以就没引起注意。
难道真的要在取使用ehcache那个“重量级”的缓存吗?当然是不可能的。经过查找,shiro提供了一个名为MemoryConstrainedCacheManager
的内存级别的缓存,其核心是使用hashmap来缓存其中的对象。所以只要new MemoryConstrainedCacheManager()
,然后直接赋值给securityManager
即可。
阅读全文
0 0
- shiro的缓存到底开不开
- shiro-04shiro的缓存
- Shiro + EHCache 缓存的使用
- shiro缓存
- shiro缓存
- shiro中的缓存的配置与使用
- Shiro的缓存和session管理
- Shiro的缓存交给redis管理
- Shiro 缓存失效以后的一个问题
- Apache shiro(4)-缓存
- Shiro EHCache缓存配置
- Apache shiro缓存
- shiro Realm 缓存默认值
- Shiro入门-缓存
- Shiro缓存整合EhCache
- Shiro(3) 缓存机制
- shiro缓存配置
- ehcache.xml及结合shiro缓存的ehcache的配置文件
- 邻接表的创建及其使用
- 盒子模型
- QT拦截关闭窗口事件
- Data analysis and Data mining
- iOS开发已已错已忘知识点总结,持续更新中,,,
- shiro的缓存到底开不开
- Android开发面试经——常见人事面试问题
- 2806 红与黑
- 利用ubuntu命令来进行查看训练时间
- python while循环
- PAT B1062. 最简分数
- Java:P12加解密方法封装
- iOS_捕捉JSAlert/confirm事件
- c++一些记录