1. 程式人生 > >shiro中CacheManager相關的類結構介紹,提供redis Cache實現

shiro中CacheManager相關的類結構介紹,提供redis Cache實現

cache lob constrain manage alt session isempty debug arr

cacheManager主要用於對shiro中的session、realm中的認證信息、授權信息進行緩存。

1.類結構

技術分享圖片

2.接口及類介紹

  • CacheManager

技術分享圖片

提供根據名字獲取cache的作用。

  • AbstractCacheManager

技術分享圖片

本地提供並發map做緩存。提供抽象類給子類繼承,子類只需要創建cache即可。

  • MemoryConstrainedCacheManager

技術分享圖片

實現上面的抽象類。創建一個map作為緩存。

3.Cache相關介紹

  • Cache接口

技術分享圖片

主要提供緩存相關的增刪改查方法。

  • MapCache

技術分享圖片

用map做緩存。通過構造器註入。

下面也提供我自己的redisCache實現。key是String類型的。需要自己提供spring redistemplate。

技術分享圖片
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import
org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import java.util.*; import java.util.concurrent.TimeUnit; /** * desc: * * @author: * creat_date: 2018/3/22 0022 * creat_time: 9:53 *
*/ @Getter @Setter public class ShiroRedisCache<V> implements Cache<String, V> { private Logger log = LoggerFactory.getLogger(getClass()); private RedisTemplate<String, V> redisTemplate; /** * 緩存的全局前綴 */ private String globalPrefix = "shiro_cache:"; /** * 真正的緩存前綴 = 全局前綴 + 緩存名 */ private String prefix; /** * 過期時間 */ private int expireTime; public ShiroRedisCache(RedisTemplate<String, V> redisTemplate, String prefix, int expireTime) { this.redisTemplate = redisTemplate; this.prefix = prefix; this.expireTime = expireTime; } @Override public V get(String key) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}", key); } if (key == null) { return null; } return redisTemplate.opsForValue().get(key); } @Override public V put(String key, V value) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}, value: {}", key, value); } if (key == null || value == null) { return null; } redisTemplate.opsForValue().set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.MINUTES); return value; } @Override public V remove(String key) throws CacheException { if (log.isDebugEnabled()) { log.debug("Key: {}", key); } if (key == null) { return null; } ValueOperations<String, V> vo = redisTemplate.opsForValue(); V value = vo.get(key); redisTemplate.delete(key); return value; } @Override public void clear() throws CacheException { redisTemplate.delete(keys()); } @Override public int size() { int len = keys().size(); return len; } @SuppressWarnings("unchecked") @Override public Set<String> keys() { String key = prefix + "*"; Set<String> set = redisTemplate.keys(key); if (CollectionUtils.isEmpty(set)) { return Collections.emptySet(); } return set; } @Override public Collection<V> values() { Set<String> keys = keys(); List<V> values = new ArrayList<>(keys.size()); for (String key : keys) { values.add(redisTemplate.opsForValue().get(key)); } return values; } }
View Code

shiro中CacheManager相關的類結構介紹,提供redis Cache實現