博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
shiro中的缓存的配置与使用
阅读量:2494 次
发布时间:2019-05-11

本文共 3295 字,大约阅读时间需要 10 分钟。

shiro缓存

缓存是提供性能的重要手段。缓存适合那些经常不变动的数据,比如系统中用户的信息和权限不会经常改变,特别适合缓存起来供下次使用。这样减少了系统查询数据库的次数,提升了性能。shiro自身不实现缓存,而是提供缓存接口,让其他第三方实现,经常EHcache缓存。

shiro配置EHcache(spring-shiro-web.xml)

ehcache的配置

认证中缓存的使用(AuthenticatingRealm 类)

shiro主要使用AuthenticatingRealm 类进行认证,其中getAuthenticationInfo方法中使用了缓存。在getAuthenticationInfo中,首先判断是否有缓存记录,没有的话,再调用子类Ream的doGetAuthenticationInfo方法查询数据库,再将查询到数据缓存起来,下次就不用查询数据库。核心代码:

public final AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {        //首先从缓存中获取记录        AuthenticationInfo info = getCachedAuthenticationInfo(token);        if (info == null) {            //otherwise not cached, perform the lookup:            info = doGetAuthenticationInfo(token);            log.debug("Looked up AuthenticationInfo [{}] from doGetAuthenticationInfo", info);            if (token != null && info != null) {                  //将记录缓存起来                cacheAuthenticationInfoIfPossible(token, info);            }        } else {            log.debug("Using cached authentication info [{}] to perform credentials matching.", info);        }        if (info != null) {            assertCredentialsMatch(token, info);        } else {            log.debug("No AuthenticationInfo found for submitted AuthenticationToken [{}].  Returning null.", token);        }        return info;    }

授权中缓存的使用(AuthorizingRealm类)

主要实现为AuthorizingRealm类中getAuthorizationInfo方法。过程和认证挺相似,首先找缓存,没有的话再去数据库找。核心代码如下:

protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {        if (principals == null) {            return null;        }        AuthorizationInfo info = null;        if (log.isTraceEnabled()) {            log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");        }        Cache
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 + "]"); } } } 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; }

源码

托管在

转载地址:http://hthrb.baihongyu.com/

你可能感兴趣的文章
cURL模拟POST方式提交数据
查看>>
headroom.js插件使用方法
查看>>
Java 可变参数
查看>>
关闭和定时显示DIV
查看>>
screen
查看>>
iOS 动画基础总结篇
查看>>
Android ContentProvider
查看>>
史上最全最强SpringMVC详细示例实战教程
查看>>
class里面只能写以下5种
查看>>
《Vim实用技巧》阅读笔记 --- 移动及跳转
查看>>
C# 全角符号转半角
查看>>
python-2:工欲善其事,必先利其器 修改jupyter保存文件目录(亲测)
查看>>
Python 环境搭建
查看>>
免费字典api ,查询汉字完整信息
查看>>
Flume协作框架
查看>>
基于数据库的事务消息解决分布式事务方案
查看>>
HDU 2461 Rectangles#容斥原理
查看>>
网口扫盲二:Mac与Phy组成原理的简单分析(转)
查看>>
使用最大似然法来求解线性模型(1)
查看>>
EF 从sqlserver2008 迁移到 2005出现的BUG
查看>>