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
![](https://i-blog.csdnimg.cn/blog_migrate/eafda0baf25efc591518470c1d525392.png)