ShiroRedisCache.java 5.44 KB
Newer Older
m1991's avatar
m1991 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
//package com.project.shiro.util.redis;
//
//import cn.wisenergy.service.common.utils.ByteUtil;
//import cn.wisenergy.service.common.utils.redis.RedisClient;
//import cn.wisenergy.service.common.utils.redis.RedisConsts;
//import org.apache.shiro.cache.Cache;
//import org.apache.shiro.cache.CacheException;
//import org.apache.shiro.util.CollectionUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.util.*;
//
//public class ShiroRedisCache<K, V> implements Cache<K, V> {
//
//    //初始化Log日志
//    private Logger logger = LoggerFactory.getLogger(this.getClass());
//
//    //注入redisClient实例
//    @Resource(name = "redisClient")
//    private RedisClient redisClient;
//
//    //shiroSession的key值前缀
//    private String keyPrefix;
//
//    //通过redisClient实例和prefix参数构造redisCache
//    public ShiroRedisCache(RedisClient redisClient, String prefix) {
//        if (redisClient == null) {
//            throw new IllegalArgumentException("shiroRedisCahe初始化时,redisClient参数不能为空");
//        }
//        this.redisClient = redisClient;
//        this.keyPrefix = prefix;
//    }
//
//    /**
//     * 获得String类型的KEY
//     *
//     * @param key
//     * @return
//     */
//    private String getPreStringKey(K key) {
//        String preKey = null;
//        if (key instanceof String) {
//            preKey = this.keyPrefix + key;
//            return preKey;
//        } else {
//            try {
//                preKey = keyPrefix + ByteUtil.bytesToHexString(ByteUtil.objectToBytes(key));
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return preKey;
//        }
//    }
//
//    @Override
//    public V get(K key) throws CacheException {
//        logger.debug("根据key从Redis中获取对象 key [" + key + "]");
//        try {
//            if (key == null) {
//                return null;
//            } else {
//                V Vvalue = (V) redisClient.get(getPreStringKey(key));
//                if (Vvalue == null) {
//                    return null;
//                }
//                return Vvalue;
//            }
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
//
//    }
//
//    @Override
//    public V put(K key, V value) throws CacheException {
//        logger.debug("根据key从存储 key [" + key + "]");
//        try {
//            redisClient.set(getPreStringKey(key), value);
//            redisClient.setAndExpire(getPreStringKey(key), value, RedisConsts.ADMIN_SHIRO_REALM_EXPIRE);
//            return value;
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
//    }
//
//    @Override
//    public V remove(K key) throws CacheException {
//        logger.debug("从redis中删除 key [" + key + "]");
//        try {
//            V previous = get(key);
//            redisClient.del(getPreStringKey(key));
//            return previous;
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
//    }
//
//    @Override
//    public void clear() throws CacheException {
//        logger.debug("从redis中删除所有元素");
//        try {
////            redisClient.flushDB();
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
//    }
//
//    @Override
//    public int size() {
////		try {
////			Long longSize = new Long(redisClient.dbSize());
////            return longSize.intValue();
////        } catch (Throwable t) {
////            throw new CacheException(t);
////        }
//        return 0;
//    }
//
//    @SuppressWarnings("unchecked")
//    @Override
//    public Set<K> keys() {
//        try {
//            Set<byte[]> keys = redisClient.keys(ByteUtil.objectToBytes(this.keyPrefix + "*"));
//            if (CollectionUtils.isEmpty(keys)) {
//                return Collections.emptySet();
//            } else {
//                Set<K> newKeys = new HashSet<K>();
//                for (byte[] key : keys) {
//                    newKeys.add((K) key);
//                }
//                return newKeys;
//            }
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
//    }
//
//    @Override
//    public Collection<V> values() {
//        try {
//            Set<byte[]> keys = redisClient.keys(ByteUtil.objectToBytes(this.keyPrefix + "*"));
//            if (!CollectionUtils.isEmpty(keys)) {
//                List<V> values = new ArrayList<V>(keys.size());
//                for (byte[] key : keys) {
//                    @SuppressWarnings("unchecked")
//                    V value = get((K) key);
//                    if (value != null) {
//                        values.add(value);
//                    }
//                }
//                return Collections.unmodifiableList(values);
//            } else {
//                return Collections.emptyList();
//            }
licc's avatar
licc committed
158 159 160
//        } catch (Throwable t) {
//            throw new CacheException(t);
//        }
m1991's avatar
m1991 committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
//    }
//
//    public String getKeyPrefix() {
//        return keyPrefix;
//    }
//
//    public void setKeyPrefix(String keyPrefix) {
//        this.keyPrefix = keyPrefix;
//    }
//
//    public RedisClient getRedisClient() {
//        return redisClient;
//    }
//
//    public void setRedisClient(RedisClient redisClient) {
//        this.redisClient = redisClient;
//    }
//}