CacheManager cache management, mainly caches role data and permission information, avoids needing to obtain data from the database each time authorization, and can directly obtain it from the cache to improve system performance.
1. Create RedisCacheManager using CacheManager interface
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 uses the CacheManager interface
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); //Serialization } /** * Returns the cached value stored under the specified {@code key}. * @param key * @return * @throws CacheException */ @Override public V get(K key) throws CacheException { // TODO Auto-generated method stub System.out.println("Get data from redis"); //In actual development, a local second-level cache can be added. When the local cache cannot be read, read from redis to prompt performance byte[] value = jedisUtil.get(getKey(key)); if(value != null) { return (V) SerializationUtils.deserialize(value); } return null; } /** * Add cache entry. * @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; } /** * Remove the cache entry corresponding to the specified key. * @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 // will clear all data in redis } /** * Returns the number of entries in the cache. * @return */ @Override public int size() { // TODO Auto-generated method stub return 0; } /** * Returns a view of all keys of entries contained in this cache. * @return */ @Override public Set<K> keys() { // TODO Auto-generated method stub return null; } /** * Returns a view of all values contained in this cache * @return */ @Override public Collection<V> values() { // TODO Auto-generated method stub return null; } }3. Spring's xml configuration
<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" /> //Set the cache </bean> <bean class="com.springshirodemo.cache.RedisCacheManager" id="redisCacheManager"></bean> //注入rediscahemanager