SpringMVC+Spring+Hibernate+Mybatis+Shiro等整合及开发(5)

    前面缓存用的Ehcache,需要自己实现redis缓存

    根据shiro提供的Ehcache实现shiro缓存的方式,只需要实现 Cache接口和CacheManager接口就行,先上Ehcache的bean文件

 <!-- 缓存管理器 -->
    <!--<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>
    </bean>-->

然后注入到SecurityManager

    <!-- securityManager安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--自定义用户信息获取器-->
        <property name="realm" ref="userRealm" />
        <!--session管理器-->
        <property name="sessionManager" ref="sessionManager"/>
        <!-- 注入缓存管理器 -->
        <property name="cacheManager" ref="cacheManager"/>
    </bean>

通过ehcache的源码看到实现了cacheManager接口


并且只返回一个Cache就行


所以目的很明确:按着shiro的要求实现这个两个接口

org.apache.shiro.cache.CacheManager

org.apache.shiro.cache.Cache

spring整合看这篇:

整合好后,我们实现这两个接口

CacheManager

/**
 * @author yanghs
 * @Description:
 * @date 2018/3/21 11:50
 */
public class RedisShiroManager implements CacheManager {
    private RedisDao redisDao;
    public <K, V> Cache<K, V> getCache(String s) throws CacheException {
        return new RedisShiroCache(s,redisDao);
    }
    public RedisDao getRedisDao() {
        return redisDao;
    }
    public void setRedisDao(RedisDao redisDao) {
        this.redisDao = redisDao;
    }

}
Cache
/**
 * @author yanghs
 * @Description:实现shiro提供的缓存
 * @date 2018/3/21 11:47
 */
public class RedisShiroCache implements Cache {
    private RedisDao redisDao;
    private String name;

    public RedisShiroCache(String name,RedisDao redisDao) {
        this.redisDao = redisDao;
        this.name = name;
    }

    public Object get(Object o) throws CacheException {
        try {
            SimplePrincipalCollection  principalCollection = (SimplePrincipalCollection)o;
            Userinfo userinfo = (Userinfo) principalCollection.getPrimaryPrincipal();
            /*key采用缓存名字加用户id+用户名防止冲突,我这里是简单实现后面还有修改的*/
            String key =name+":"+ userinfo.getUserid()+userinfo.getUsername();
            byte[] b =  redisDao.getValueByKey(key.getBytes());
            return SerializeUtil.deserialize(b);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Object put(Object o, Object o2) throws CacheException {
        SimplePrincipalCollection  principalCollection = (SimplePrincipalCollection)o;
        Userinfo userinfo = (Userinfo) principalCollection.getPrimaryPrincipal();
        byte[] key = (name+":"+userinfo.getUserid()+userinfo.getUsername()).getBytes();
        byte[] value = SerializeUtil.serialize(o2);
        try {
            redisDao.saveValueByKey(key,value,-1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return get(o);
    }

    public Object remove(Object o) throws CacheException {
        SimplePrincipalCollection  principalCollection = (SimplePrincipalCollection)o;
        Userinfo userinfo = (Userinfo) principalCollection.getPrimaryPrincipal();
        byte[] key = (name+":"+userinfo.getUserid()+userinfo.getUsername()).getBytes();
        try {
            redisDao.deleteByKey(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void clear() throws CacheException {

    }

    public int size() {
        return 0;
    }

    public Set keys() {
        return null;
    }

    public Collection values() {
        return null;
    }
/**
 * @author yanghs
 * @Description:针对redis存储对象序列化
 * @date 2018年3月21日17:29:19
 */
public class SerializeUtil {
	
    public static byte[] serialize(Object value) {
        if (value == null) { 
            throw new NullPointerException("Can't serialize null");
        }
        byte[] rv = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        try {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(value);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (Exception e) {
        } finally {
            close(os);
            close(bos);
        }
        return rv;
    }

    
	public static Object deserialize(byte[] in) {
        return deserialize(in, Object.class);
    }

    public static <T> T deserialize(byte[] in, Class<T>...requiredType) {
        Object rv = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream is = null;
        try {
            if (in != null) {
                bis = new ByteArrayInputStream(in);
                is = new ObjectInputStream(bis);
                rv = is.readObject();
            }
        } catch (Exception e) {
        } finally {
            close(is);
            close(bis);
        }
        return (T) rv;
    }

    private static void close(Closeable closeable) {
        if (closeable != null)
            try {
                closeable.close();
            } catch (IOException e) {

            }
    }

}

这样就简单实现了缓存使用redis。测试成功

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值