Shiro缓存管理

导读:本篇文章讲解 Shiro缓存管理,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

通过缓存管理可使授权和认证在第一次操作之后不再经过数据库,能有效提高运行效率

第一步:创建RedisCacheManager实现CacheManager接口

package com.lmc.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

/**
 * @Author lmc
 * @Description
 * @Date: Create in 20:47 2020/2/2
 */
public class RedisCacheManager implements CacheManager {


    @Override
    public <K, V> Cache<K, V> getCache(String s) throws CacheException {
        return null;
    }
}

第二步:创建RedisCache实现Cache接口

package com.lmc.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;

import java.util.Collection;
import java.util.Set;

/**
 * @Author lmc
 * @Description
 * @Date: Create in 20:48 2020/2/2
 */
public class RedisCache<K, V> implements Cache<K, V> {
    @Override
    public Object get(Object o) throws CacheException {
        return null;
    }

    @Override
    public Object put(Object o, Object o2) throws CacheException {
        return null;
    }

    @Override
    public Object remove(Object o) throws CacheException {
        return null;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set keys() {
        return null;
    }

    @Override
    public Collection values() {
        return null;
    }
}

第三步:重写RedisCache方法

package com.lmc.cache;

import com.lmc.util.JedisUtil;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.stereotype.Component;
import org.springframework.util.SerializationUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Set;

/**
 * @Author lmc
 * @Description
 * @Date: Create in 20:48 2020/2/2
 */
@Component
public class RedisCache<K, V> implements Cache<K, V> {

    @Resource
    private JedisUtil jedisUtil;
    private final String CACHE_PREFIX = "lmc-cache:";

    private byte[] getKey(K k) {
        if (k instanceof String) {
            return (CACHE_PREFIX + k).getBytes();
        }
        return SerializationUtils.serialize(k);
    }

    @Override
    public Object get(Object o) throws CacheException {
        System.out.println("从Redis中获取权限数据......");
        byte[] value = jedisUtil.get(getKey((K) o));
        if (value != null) {
            return (V) SerializationUtils.deserialize(value);
        }
        return null;
    }

    @Override
    public Object put(Object o, Object o2) throws CacheException {
        byte[] k = getKey((K) o);
        byte[] v = SerializationUtils.serialize(o2);
        jedisUtil.set(k, v);
        jedisUtil.expire(k, 300);
        return v;
    }

    @Override
    public Object remove(Object o) throws CacheException {
        byte[] k = getKey((K) o);
        byte[] v = jedisUtil.get(k);
        jedisUtil.delete(k);
        if (v != null) {
            return SerializationUtils.deserialize(v);
        }
        return null;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set keys() {
        return null;
    }

    @Override
    public Collection values() {
        return null;
    }
}

第四步:重写RedisCacheManager的方法

package com.lmc.cache;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;

import javax.annotation.Resource;

/**
 * @Author lmc
 * @Description
 * @Date: Create in 20:47 2020/2/2
 */
public class RedisCacheManager implements CacheManager {

    @Resource
    private RedisCache redisCache;

    @Override
    public <K, V> Cache<K, V> getCache(String s) throws CacheException {
        return redisCache;
    }
}

第五步:修改spring.xml

<!--创建SecurityManager对象-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="userRealm" />
        <property name="sessionManager" ref="sessionManager" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>

<bean id="cacheManager" class="com.lmc.cache.RedisCacheManager" />

运行之后,先是通过Redis查询授权信息,没有的话再通过数据库访问

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81649.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!