1. 程式人生 > >Spring data redis 之 spring 系統整合

Spring data redis 之 spring 系統整合

第一步: 新增依賴(以maven為例)

<!--     redis相關jar包依賴    -->
         <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

spring-data-redis 是spring 對redis的整合封裝,spring操作redis 最常用的就是其中的RedisTemplate 物件!spring 對redis操作做了很好的序列化封裝,使用起來會方便很多。

第二步:配置資料庫連線快取池

如圖所示:裡面每個欄位的基本都有說明(下面會將完整配置貼上)。裡面的testWhileIdle 是個無效配置項

 

第三步:配置redis 連結物件

和mybatis一樣配置 連結工廠就可以了。

第四步 : 配置 RedisTemplate

如果需要其他的Template 在下面追加就可以了,例如 : StringRedisTemplate 

 

整體配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:redis="http://www.springframework.org/schema/redis" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/redis
    http://www.springframework.org/schema/redis/spring-redis.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd
    ">
   
    <!-- Redis -->
    <!-- 連線池引數 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    	<!-- 最大連結數 -->
        <property name="maxIdle" value="100" />
        <!-- 最小連線數 -->
        <property name="minIdle" value="10" />
        
        <property name="maxTotal" value="200" />
        <property name="maxWaitMillis" value="10000" />
       <!--  表示一個物件至少停留在idle狀態的最短時間,然後才能被idle object evitor掃描並驅逐;
        	這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義; -->       	
        <property name="minEvictableIdleTimeMillis" value="300000"></property>
        <property name="numTestsPerEvictionRun" value="10"></property>        
     <!--    表示idle object evitor兩次掃描之間要sleep的毫秒數; -->
        <property name="timeBetweenEvictionRunsMillis" value="30000"></property>
        
        <!-- 獲得一個jedis例項的時候是否檢查連線可用性(ping());如果為true,則得到的jedis例項均是可用的; -->      
        <property name="testOnBorrow" value="true" />
        <!--  return 一個jedis例項給pool時,是否檢查連線可用性(ping()) -->        
        <property name="testOnReturn" value="true" />      
       <!--  如果為true,表示有一個idle object evitor執行緒對idle object進行掃描,
        	如果validate失敗,此object會被從pool中drop掉;
        	這一項只有在timeBetweenEvictionRunsMillis大於0時才有意義; -->
        <property name="testWhileIdle" value="true"></property>
    </bean>
 
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="6379" />
        <property name="password" value="" />
        <property name="usePool" value="true" />
        <property name="database" value="0" />
        <property name="timeout" value="1000" />
    </bean>
 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        
        <!-- 序列化方式 建議key/hashKey採用StringRedisSerializer -->
        <property name="keySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean
                class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean
                class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
         <!-- 開啟REIDS事務支援 -->
         <property name="enableTransactionSupport" value="false" />
    </bean>
 
    <!-- 對string操作的封裝 -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <constructor-arg ref="jedisConnectionFactory" />
            <!-- 開啟REIDS事務支援 -->  
         <property name="enableTransactionSupport" value="false" />
    </bean>
    
</beans>

第五步:新增spring 的getBean 方法 

package com.telchina.scada.common.util;
 
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
 
/**
 * Context 工具類
 */
@SuppressWarnings("static-access")
@Component
public class SpringtUtil implements ApplicationContextAware {
    private static ApplicationContext commonApplicationContext;
 
 
   
    public void setApplicationContext(ApplicationContext context) throws BeansException {
    	this.commonApplicationContext = context;
    }
 
 
    /**
     * 根據提供的bean名稱得到相應的服務類
     * @param beanId bean的id
     * @return 返回bean的例項物件
     */
    public static Object getBean(String beanId) {
	return commonApplicationContext.getBean(beanId);
    }
 
 
    /**
     * 根據提供的bean名稱得到對應於指定型別的服務類
     * @param beanId bean的id
     * @param clazz bean的類型別
     * @return 返回的bean型別,若型別不匹配,將丟擲異常
     */
    public static <T> T getBean(String beanId, Class<T> clazz) {
    	return commonApplicationContext.getBean(beanId, clazz);
    }
    
}   
    
    

第六步:redis工具類

package com.telchina.scada.common.util;
 
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
 
/**
 * redis工具類
 *
 */
@SuppressWarnings("unchecked")
public class RedisUtil {
 
    private static final Logger LOG = LoggerFactory.getLogger(CacheUtil.class);
 
    private static RedisTemplate<String, Object> redisTemplate = SpringtUtil.getBean("redisTemplate", RedisTemplate.class);
 
    private static StringRedisTemplate stringRedisTemplate = SpringtUtil.getBean("stringRedisTemplate", StringRedisTemplate.class);
 
    private static String CACHE_PREFIX;
 
    private static boolean CACHE_CLOSED;
 
    private CacheUtil() {
 
    }
 
    @SuppressWarnings("rawtypes")
    private static boolean isEmpty(Object obj) {
	if (obj == null) {
	    return true;
	}
	if (obj instanceof String) {
	    String str = obj.toString();
	    if ("".equals(str.trim())) {
		return true;
	    }
	    return false;
	}
	if (obj instanceof List) {
	    List<Object> list = (List<Object>) obj;
	    if (list.isEmpty()) {
		return true;
	    }
	    return false;
	}
	if (obj instanceof Map) {
	    Map map = (Map) obj;
	    if (map.isEmpty()) {
		return true;
	    }
	    return false;
	}
	if (obj instanceof Set) {
	    Set set = (Set) obj;
	    if (set.isEmpty()) {
		return true;
	    }
	    return false;
	}
	if (obj instanceof Object[]) {
	    Object[] objs = (Object[]) obj;
	    if (objs.length <= 0) {
		return true;
	    }
	    return false;
	}
	return false;
    }
    /**
     * 構建快取key值
     * @param key	快取key
     * @return
     */
    private static String buildKey(String key) {
	if (CACHE_PREFIX == null || "".equals(CACHE_PREFIX)) {
	    return key;
	}
	return CACHE_PREFIX + ":" + key;
    }
    /**
     * 返回快取的字首
     * @return CACHE_PREFIX_FLAG
     */
    public static String getCachePrefix() {
	return CACHE_PREFIX;
    }
    /**
     * 設定快取的字首
     * @param cachePrefix
     */
    public static void setCachePrefix(String cachePrefix) {
	if (cachePrefix != null && !"".equals(cachePrefix.trim())) {
	    CACHE_PREFIX = cachePrefix.trim();
	}
    }
    /**
     * 關閉快取
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean close() {
	LOG.debug(" cache closed ! ");
	CACHE_CLOSED = true;
	return true;
    }
    /**
     * 開啟快取
     * @return	true:存在
     * 		false:不存在
     */
    public static boolean openCache() {
	CACHE_CLOSED = false;
	return true;
    }
    /**
     * 檢查快取是否開啟
     * @return	true:已關閉 
     * 		false:已開啟
     */
    public static boolean isClose() {
	return CACHE_CLOSED;
    }
    /**
     * 判斷key值是否存在
     * @param key	快取的key
     * @return	true:存在
     * 		false:不存在
     */
    public static boolean hasKey(String key) {
	LOG.debug(" hasKey key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return false;
	    }
	    key = buildKey(key);
	    return redisTemplate.hasKey(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
    /**
     * 匹配符合正則的key
     * @param patternKey
     * @return key的集合
     */
    public static Set<String> keys(String patternKey) {
    	LOG.debug(" keys key :{}", patternKey);
    	try {
    	    if (isClose() || isEmpty(patternKey)) {
    		return Collections.emptySet();
    	    }
    	    return redisTemplate.keys(patternKey);
    	} catch (Exception e) {
    	    LOG.error(e.getMessage(), e);
    	}
    	return Collections.emptySet();
        }
 
    /**
     * 根據key刪除快取
     * @param key
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean del(String... key) {
	LOG.debug(" delete key :{}", key.toString());
	try {
	    if (isClose() || isEmpty(key)) {
		return false;
	    }
	    Set<String> keySet = new HashSet<String>();
	    for (String str : key) {
		keySet.add(buildKey(str));
	    }
	    redisTemplate.delete(keySet);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 根據key刪除快取
     * @param key
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean delPattern(String key) {
	LOG.debug(" delete Pattern keys :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.delete(redisTemplate.keys(key));
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 刪除一組key值
     * @param keys
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean del(Set<String> keys) {
	LOG.debug(" delete keys :{}", keys.toString());
	try {
	    if (isClose() || isEmpty(keys)) {
		return false;
	    }
	    Set<String> keySet = new HashSet<String>();
	    for (String str : keys) {
		keySet.add(buildKey(str));
	    }
	    redisTemplate.delete(keySet);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 設定過期時間
     * @param key	快取key
     * @param seconds	過期秒數
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean setExp(String key, long seconds) {
	LOG.debug(" setExp key :{}, seconds: {}", key, seconds);
	try {
	    if (isClose() || isEmpty(key) || seconds > 0) {
		return false;
	    }
	    key = buildKey(key);
	    return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 查詢過期時間
     * @param key	快取key
     * @return	秒數
     */
    public static Long getExpire(String key) {
	LOG.debug(" getExpire key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return 0L;
	    }
	    key = buildKey(key);
	    return redisTemplate.getExpire(key, TimeUnit.SECONDS);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return 0L;
    }
 
    /**
     * 快取存入key-value
     * @param key	快取鍵
     * @param value	快取值
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean setString(String key, String value) {
	LOG.debug(" setString key :{}, value: {}", key, value);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    stringRedisTemplate.opsForValue().set(key, value);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 快取存入key-value
     * @param key	快取鍵
     * @param value	快取值
     * @param seconds	秒數
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean setString(String key, String value, long seconds) {
	LOG.debug(" setString key :{}, value: {}, timeout:{}", key, value, seconds);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 根據key取出String value
     * @param key	快取key值
     * @return	String	快取的String
     */
    public static String getString(String key) {
	LOG.debug(" getString key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return stringRedisTemplate.opsForValue().get(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 去的快取中的最大值並+1
     * @param key	快取key值
     * @return	long	快取中的最大值+1
     */
    public static long incr(String key) {
	LOG.debug(" incr key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return 0;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForValue().increment(key, 1);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return 0;
    }
 
    /**
     * 快取中存入序列化的Object物件
     * @param <T>
     * @param key	快取key
     * @param obj	存入的序列化物件
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean set(String key, Object obj) {
	LOG.debug(" set key :{}, value:{}", key, obj);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(obj)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForValue().set(key, obj);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 快取中存入序列化的Object物件
     * @param <T>
     * @param key	快取key
     * @param obj	存入的序列化物件
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean setObj(String key, Object obj, long seconds) {
	LOG.debug(" set key :{}, value:{}, seconds:{}", key, obj, seconds);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(obj)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForValue().set(key, obj);
	    if (seconds > 0) {
		redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	    }
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 取出快取中儲存的序列化物件
     * @param key	快取key
     * @param clazz	物件類
     * @return <T>	序列化物件
     */
    public static <T> T getObj(String key, Class<T> clazz) {
	LOG.debug(" get key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return (T) redisTemplate.opsForValue().get(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 存入Map陣列
     * @param <T>
     * @param key	快取key
     * @param map	快取map
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setMap(String key, Map<String, T> map) {
	try {
	    if (isClose() || isEmpty(key) || isEmpty(map)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForHash().putAll(key, map);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 取出快取的map
     * @param key	快取key
     * @return map	快取的map
     */
    @SuppressWarnings("rawtypes")
    public static Map getMap(String key) {
	LOG.debug(" getMap key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForHash().entries(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 查詢快取的map的集合大小
     * @param key	快取key
     * @return int	快取map的集合大小
     */
    public static long getMapSize(String key) {
	LOG.debug(" getMap key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return 0;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForHash().size(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return 0;
    }
 
    
    /**
     * 根據key以及hashKey取出對應的Object物件
     * @param key	快取key
     * @param hashKey	對應map的key
     * @return object	map中的物件
     */
    public static Object getMapKey(String key, String hashKey) {
	LOG.debug(" getMapkey :{}, hashKey:{}", key, hashKey);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
		return null;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForHash().get(key, hashKey);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 取出快取中map的所有key值
     * @param key	快取key
     * @return Set<String> map的key值合集
     */
    public static Set<Object> getMapKeys(String key) {
	LOG.debug(" getMapKeys key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForHash().keys(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 刪除map中指定的key值
     * @param key	快取key
     * @param hashKey	map中指定的hashKey
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean delMapKey(String key, String hashKey) {
	LOG.debug(" delMapKey key :{}, hashKey:{}", key, hashKey);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(hashKey)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForHash().delete(key, hashKey);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 存入Map陣列
     * @param <T>
     * @param key	快取key
     * @param map	快取map
     * @param seconds	秒數
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setMapExp(String key, Map<String, T> map, long seconds) {
	LOG.debug(" setMapExp key :{}, value: {}, seconds:{}", key, map, seconds);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(map)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForHash().putAll(key, map);
	    redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * map中加入新的key
     * @param <T>
     * @param key	快取key
     * @param hashKey	map的Key值
     * @param value	map的value值
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean addMap(String key, String hashKey, T value) {
	LOG.debug(" addMap key :{}, hashKey: {}, value:{}", key, hashKey, value);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(hashKey) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForHash().put(key, hashKey, value);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 快取存入List
     * @param <T>
     * @param key	快取key
     * @param list	快取List
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setList(String key, List<T> list) {
	LOG.debug(" setList key :{}, list: {}", key, list);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(list)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForList().leftPushAll(key, list.toArray());
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 根據key值取出對應的list合集
     * @param key	快取key
     * @return List<Object> 快取中對應的list合集
     */
    public static <V> List<V> getList(String key) {
	LOG.debug(" getList key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return (List<V>) redisTemplate.opsForList().range(key, 0, -1);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 根據key值擷取對應的list合集
     * @param key	快取key
     * @param start	開始位置
     * @param end	結束位置
     * @return
     */
    public static void trimList(String key, int start, int end) {
	LOG.debug(" trimList key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForList().trim(key, start, end);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
    }
 
    /**
     * 取出list合集中指定位置的物件
     * @param key	快取key
     * @param index	索引位置
     * @return Object	list指定索引位置的物件
     */
    public static Object getIndexList(String key, int index) {
	LOG.debug(" getIndexList key :{}, index:{}", key, index);
	try {
	    if (isClose() || isEmpty(key) || index < 0) {
		return null;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForList().index(key, index);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
    /**
     * Object存入List
     * @param <T>
     * @param key	快取key
     * @param value	List中的值
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean addList(String key, Object value) {
	LOG.debug(" addList key :{}, value:{}", key, value);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForList().leftPush(key, value);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 快取存入List
     * @param <T>
     * @param key	快取key
     * @param list	快取List
     * @param seconds	秒數
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setList(String key, List<T> list, long seconds) {
	LOG.debug(" setList key :{}, value:{}, seconds:{}", key, list, seconds);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(list)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForList().leftPushAll(key, list.toArray());
	    if (seconds > 0) {
		redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	    }
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * set集合存入快取
     * @param <T>
     * @param key	快取key
     * @param set	快取set集合
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setSet(String key, Set<T> set) {
	LOG.debug(" setSet key :{}, value:{}", key, set);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(set)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForSet().add(key, set.toArray());
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * set集合中增加value
     * @param <T>
     * @param key	快取key
     * @param value	增加的value
     * @return	true:成功 
     * 		false:失敗
     */
    public static boolean addSet(String key, Object value) {
	LOG.debug(" addSet key :{}, value:{}", key, value);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForSet().add(key, value);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * set集合存入快取
     * @param <T>
     * @param key	快取key
     * @param set	快取set集合
     * @param seconds	秒數
     * @return	true:成功 
     * 		false:失敗
     */
    public static <T> boolean setSet(String key, Set<T> set, long seconds) {
	LOG.debug(" setSet key :{}, value:{}, seconds:{}", key, set, seconds);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(set)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForSet().add(key, set.toArray());
	    if (seconds > 0) {
		redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
	    }
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 取出快取中對應的set合集
     * @param <T>
     * @param key	快取key
     * @return Set<Object> 快取中的set合集
     */
    public static <T> Set<T> getSet(String key) {
	LOG.debug(" getSet key :{}", key);
	try {
	    if (isClose() || isEmpty(key)) {
		return null;
	    }
	    key = buildKey(key);
	    return (Set<T>) redisTemplate.opsForSet().members(key);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return null;
    }
 
    /**
     * 有序集合存入數值
     * @param key	快取key
     * @param value	快取value
     * @param score	評分
     * @return
     */
    public static boolean addZSet(String key, Object value, double score) {
	LOG.debug(" addZSet key :{},value:{}, score:{}", key, value, score);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    return redisTemplate.opsForZSet().add(key, value, score);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 從有序集合中刪除指定值
     * @param key	快取key
     * @param value	快取value
     * @return
     */
    public static boolean removeZSet(String key, Object value) {
	LOG.debug(" removeZSet key :{},value:{}", key, value);
	try {
	    if (isClose() || isEmpty(key) || isEmpty(value)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForZSet().remove(key, value);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 從有序集合中刪除指定位置的值
     * @param key	快取key
     * @param start	起始位置
     * @param end	結束為止
     * @return
     */
    public static boolean removeZSet(String key, long start, long end) {
	LOG.debug(" removeZSet key :{},start:{}, end:{}", key, start, end);
	try {
	    if (isClose() || isEmpty(key)) {
		return false;
	    }
	    key = buildKey(key);
	    redisTemplate.opsForZSet().removeRange(key, start, end);
	    return true;
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return false;
    }
 
    /**
     * 從有序集合中獲取指定位置的值
     * @param key	快取key
     * @param start	起始位置
     * @param end	結束為止
     * @return
     */
    public static <T> Set<T> getZSet(String key, long start, long end) {
	LOG.debug(" getZSet key :{},start:{}, end:{}", key, start, end);
	try {
	    if (isClose() || isEmpty(key)) {
		return Collections.emptySet();
	    }
	    key = buildKey(key);
	    return (Set<T>) redisTemplate.opsForZSet().range(key, start, end);
	} catch (Exception e) {
	    LOG.error(e.getMessage(), e);
	}
	return Collections.emptySet();
    }
}