shiro的缓存到底开不开

最近使用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即可。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值