码迷,mamicode.com
首页 > 系统相关 > 详细

shiro中CacheManager相关的类结构介绍,提供redis Cache实现

时间:2018-03-23 16:24:22      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:put   led   gpo   data   work   return   log   实现   类继承   

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实现

标签:put   led   gpo   data   work   return   log   实现   类继承   

原文地址:https://www.cnblogs.com/grey-wolf/p/8630676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!