Shrio的缓存管理 ——笔记

CacheManager缓存管理,主要缓存角色数据和权限信息,避免每次授权时都需要数据库中获取数据,可以直接从缓存中获取,提升系统性能。

1.创建RedisCacheManager使用 CacheManager接口

package com.springshirodemo.cache;

import javax.annotation.Resource;

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


public class RedisCacheManager implements CacheManager {

	@Resource
	private RdisCache rdisCache;
	
	@Override
	public <K, V> Cache<K, V> getCache(String arg0) throws CacheException {
		// TODO Auto-generated method stub
		return rdisCache;
	}

}
2.RedisCache使用CacheManager接口
package com.springshirodemo.cache;

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

import javax.annotation.Resource;

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

import com.springshirodemo.util.JedisUtil;

@Component
public class RdisCache<K,V> implements Cache<K, V> {
	
	
	@Resource 
	private JedisUtil jedisUtil;
	private final String CACHE_PREFIX = "imooc-cache:";  //前缀
	
	private byte[] getKey(K k) {
		if(k instanceof String) {
			return (CACHE_PREFIX+k).getBytes();		 //返回key
		}
		return SerializationUtils.serialize(k);      //序列化
	}

	
	/**
	 * 返回存储在指定的{@代码键}下的缓存值。
	 * @param key
	 * @return
	 * @throws CacheException
	 */
	@Override
	public V get(K key) throws CacheException {
		// TODO Auto-generated method stub
		System.out.println("从redis获取数据");   //实际开发中可以加一个本地二级缓存,当本地缓存中读不到,再从redis中读,以提示性能
		byte[] value = jedisUtil.get(getKey(key));
		if(value != null) {
			return (V) SerializationUtils.deserialize(value);
		}
		return null;
	}

	/**
	 * 添加缓存条目。
	 * @param key
	 * @param value
	 * @return
	 * @throws CacheException
	 */
	@Override
	public V put(K key, V value) throws CacheException {
		// TODO Auto-generated method stub
		byte[] k = getKey(key);
		byte[] v = SerializationUtils.serialize(value);
		jedisUtil.set(k, v);
		jedisUtil.expire(k, 600);
		return value;
	}

	/**
	 * 移除对应于指定键的缓存条目。
	 * @param key
	 * @return
	 * @throws CacheException
	 */
	@Override
	public V remove(K key) throws CacheException {
		// TODO Auto-generated method stub
		byte[] k = getKey(key);
		byte[] v = jedisUtil.get(k);
		jedisUtil.delete(k);
		if(v != null) {
			return (V) SerializationUtils.deserialize(v);
		}
		return null;
	}

	@Override
	public void clear() throws CacheException {
		// TODO Auto-generated method stub
		//会清空redis所有数据
	}

	/**
	 * 返回缓存中的条目数量。
	 * @return
	 */
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 返回包含在该缓存中的条目的所有键的视图。
	 * @return
	 */
	@Override
	public Set<K> keys() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 返回此缓存中包含的所有值的视图
	 * @return
	 */
	@Override
	public Collection<V> values() {
		// TODO Auto-generated method stub
		return null;
	}
	
	


}
3.spring的xml配置中
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
	   <property name="realm" ref="bosRealm"/> 
	   <property name="sessionManager" ref="sessionManager" />
	   <property name="cacheManager" ref="redisCacheManager" /> //设置缓存
	</bean>  
	<bean class="com.springshirodemo.cache.RedisCacheManager" id="redisCacheManager"></bean> //注入rediscahemanager







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值