1. 程式人生 > >SpringBoot使用Jedis整合Redis(有demo)

SpringBoot使用Jedis整合Redis(有demo)

本篇基於Springboot2.0 + Redis實現資料快取及分庫儲存,上一篇主要使用官方推薦的RedisTemplate實現,這篇我主要以Jedis的方式來實現Redis的操作。這裡再重複一遍,兩種方式都可以實現對Redis的操作,RedisTemplate是對Jedis做了封裝,官方推薦使用Redistemplate方式。

本文使用idea工具構建Springboot+SpringMvc+Thymeleaf+SpringDataJPA+MySql+Redis專案

GitHub地址:https://github.com/jwwam/springbootRedis2.git

一、Redis基本配置及工具類
還是來樣子先看下專案demo的目錄結構,基本的目錄結構和上篇沒什麼差別。文末會給出pom包配置。

1.建立redis.properties配置檔案

#redis配置開始
# Redis資料庫索引(預設為0)
spring.redis.database=0
# Redis伺服器地址
spring.redis.host=localhost
# Redis伺服器連線埠
spring.redis.port=6379
# Redis伺服器連線密碼(預設為空)
spring.redis.password=123456
# 連線池最大連線數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=1024
# 連線池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=10000
# 連線池中的最大空閒連線
spring.redis.jedis.pool.max-idle=200
# 連線池中的最小空閒連線
spring.redis.jedis.pool.min-idle=0
# 連線超時時間(毫秒)
spring.redis.timeout=10000
#redis配置結束
spring.redis.block-when-exhausted=true


這裡我們要注意max-active,max-wait,max-idle,min-idle這幾個引數版本不同寫法也不一樣,我這裡由於引入了最新的Jedis包所以寫法如上,請注意。

2.新增配置類RedisConfig.java

package com.springboot.demo.base.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
@Configuration
@PropertySource("classpath:redis.properties")
@Slf4j
public class RedisConfig {
 
    @Value("${spring.redis.host}")
    private String host;
 
    @Value("${spring.redis.port}")
    private int port;
 
    @Value("${spring.redis.timeout}")
    private int timeout;
 
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;
 
    @Value("${spring.redis.jedis.pool.max-wait}")
    private long maxWaitMillis;
 
    @Value("${spring.redis.password}")
    private String password;
 
    @Value("${spring.redis.block-when-exhausted}")
    private boolean  blockWhenExhausted;
 
    @Bean
    public JedisPool redisPoolFactory()  throws Exception{
        log.info("JedisPool注入成功!!");
        log.info("redis地址:" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 連線耗盡時是否阻塞, false報異常,ture阻塞直到超時, 預設true
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        // 是否啟用pool的jmx管理功能, 預設true
        jedisPoolConfig.setJmxEnabled(true);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;
    }
 
}
3.建立RedisUtil.java工具類,注意,這是一個非常完整的Jedis操作redis的工具類,裡面多數方法我都加入了分庫操作,使用時注意規範就好。

package com.springboot.demo.base.utils;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;
 
@Component
@Slf4j
public class RedisUtil{
 
    @Autowired
    private JedisPool jedisPool;
 
    /**
     * <p>
     * 通過key獲取儲存在redis中的value
     * </p>
     * <p>
     * 並釋放連線
     * </p>
     *
     * @param key
     * @param indexdb 選擇redis庫 0-15
     * @return 成功返回value 失敗返回null
     */
    public String get(String key,int indexdb) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            value = jedis.get(key);
            log.info(value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return value;
    }
 
    /**
     * <p>
     * 通過key獲取儲存在redis中的value
     * </p>
     * <p>
     * 並釋放連線
     * </p>
     *
     * @param key
     * @param indexdb 選擇redis庫 0-15
     * @return 成功返回value 失敗返回null
     */
    public byte[] get(byte[] key,int indexdb) {
        Jedis jedis = null;
        byte[] value = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            value = jedis.get(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return value;
    }
    /**
     * <p>
     * 向redis存入key和value,並釋放連線資源
     * </p>
     * <p>
     * 如果key已經存在 則覆蓋
     * </p>
     *
     * @param key
     * @param value
     * @param indexdb 選擇redis庫 0-15
     * @return 成功 返回OK 失敗返回 0
     */
    public String set(String key, String value,int indexdb) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.set(key, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return "0";
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * <p>
     * 向redis存入key和value,並釋放連線資源
     * </p>
     * <p>
     * 如果key已經存在 則覆蓋
     * </p>
     *
     * @param key
     * @param value
     * @param indexdb 選擇redis庫 0-15
     * @return 成功 返回OK 失敗返回 0
     */
    public String set(byte[] key, byte[] value,int indexdb) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.set(key, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return "0";
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * <p>
     * 刪除指定的key,也可以傳入一個包含key的陣列
     * </p>
     *
     * @param keys 一個key 也可以使 string 陣列
     * @return 返回刪除成功的個數
     */
    public Long del(String... keys) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(keys);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * <p>
     * 刪除指定的key,也可以傳入一個包含key的陣列
     * </p>
     * @param indexdb 選擇redis庫 0-15
     * @param keys 一個key 也可以使 string 陣列
     * @return 返回刪除成功的個數
     */
    public Long del(int indexdb,String... keys) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.del(keys);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * <p>
     * 刪除指定的key,也可以傳入一個包含key的陣列
     * </p>
     * @param indexdb 選擇redis庫 0-15
     * @param keys 一個key 也可以使 string 陣列
     * @return 返回刪除成功的個數
     */
    public Long del(int indexdb,byte[]... keys) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.del(keys);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
    /**
     * <p>
     * 通過key向指定的value值追加值
     * </p>
     *
     * @param key
     * @param str
     * @return 成功返回 新增後value的長度 失敗 返回 新增的 value 的長度 異常返回0L
     */
    public Long append(String key, String str) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.append(key, str);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 判斷key是否存在
     * </p>
     *
     * @param key
     * @return true OR false
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return false;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 清空當前資料庫中的所有 key,此命令從不失敗。
     * </p>
     *
     * @return 總是返回 OK
     */
    public String flushDB() {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.flushDB();
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return null;
    }
 
    /**
     * <p>
     * 為給定 key 設定生存時間,當 key 過期時(生存時間為 0 ),它會被自動刪除。
     * </p>
     *
     * @param key
     * @param value
     *            過期時間,單位:秒
     * @return 成功返回1 如果存在 和 發生異常 返回 0
     */
    public Long expire(String key, int value, int indexdb) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.expire(key, value);
        } catch (Exception e) {
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 以秒為單位,返回給定 key 的剩餘生存時間
     * </p>
     *
     * @param key
     * @return 當 key 不存在時,返回 -2 。當 key 存在但沒有設定剩餘生存時間時,返回 -1 。否則,以秒為單位,返回 key
     *         的剩餘生存時間。 發生異常 返回 0
     */
    public Long ttl(String key,int indexdb) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            return jedis.ttl(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 移除給定 key 的生存時間,將這個 key 從『易失的』(帶生存時間 key )轉換成『持久的』(一個不帶生存時間、永不過期的 key )
     * </p>
     *
     * @param key
     * @return 當生存時間移除成功時,返回 1 .如果 key 不存在或 key 沒有設定生存時間,返回 0 , 發生異常 返回 -1
     */
    public Long persist(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.persist(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return -1L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 新增key,並將 key 的生存時間 (以秒為單位)
     * </p>
     *
     * @param key
     * @param seconds
     *            生存時間 單位:秒
     * @param value
     * @return 設定成功時返回 OK 。當 seconds 引數不合法時,返回一個錯誤。
     */
    public String setex(String key, int seconds, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.setex(key, seconds, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return null;
    }
 
    /**
     * <p>
     * 設定key value,如果key已經存在則返回0,nx==> not exist
     * </p>
     *
     * @param key
     * @param value
     * @return 成功返回1 如果存在 和 發生異常 返回 0
     */
    public Long setnx(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.setnx(key, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 將給定 key 的值設為 value ,並返回 key 的舊值(old value)。
     * </p>
     * <p>
     * 當 key 存在但不是字串型別時,返回一個錯誤。
     * </p>
     *
     * @param key
     * @param value
     * @return 返回給定 key 的舊值。當 key 沒有舊值時,也即是, key 不存在時,返回 nil
     */
    public String getSet(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.getSet(key, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return null;
    }
 
    /**
     * <p>
     * 設定key value並制定這個鍵值的有效期
     * </p>
     *
     * @param key
     * @param value
     * @param seconds
     *            單位:秒
     * @return 成功返回OK 失敗和異常返回null
     */
    public String setex(String key, String value, int seconds) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.setex(key, seconds, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key 和offset 從指定的位置開始將原先value替換
     * </p>
     * <p>
     * 下標從0開始,offset表示從offset下標開始替換
     * </p>
     * <p>
     * 如果替換的字串長度過小則會這樣
     * </p>
     * <p>
     * example:
     * </p>
     * <p>
     * value : [email protected]
     * </p>
     * <p>
     * str : abc
     * </p>
     * <P>
     * 從下標7開始替換 則結果為
     * </p>
     * <p>
     * RES : bigsea.abc.cn
     * </p>
     *
     * @param key
     * @param str
     * @param offset
     *            下標位置
     * @return 返回替換後 value 的長度
     */
    public Long setrange(String key, String str, int offset) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.setrange(key, offset, str);
        } catch (Exception e) {
 
            log.error(e.getMessage());
            return 0L;
        } finally {
            returnResource(jedisPool, jedis);
        }
    }
 
    /**
     * <p>
     * 通過批量的key獲取批量的value
     * </p>
     *
     * @param keys
     *            string陣列 也可以是一個key
     * @return 成功返回value的集合, 失敗返回null的集合 ,異常返回空
     */
    public List<String> mget(String... keys) {
        Jedis jedis = null;
        List<String> values = null;
        try {
            jedis = jedisPool.getResource();
            values = jedis.mget(keys);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return values;
    }
 
    /**
     * <p>
     * 批量的設定key:value,可以一個
     * </p>
     * <p>
     * example:
     * </p>
     * <p>
     * obj.mset(new String[]{"key2","value1","key2","value2"})
     * </p>
     *
     * @param keysvalues
     * @return 成功返回OK 失敗 異常 返回 null
     *
     */
    public String mset(String... keysvalues) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.mset(keysvalues);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 批量的設定key:value,可以一個,如果key已經存在則會失敗,操作會回滾
     * </p>
     * <p>
     * example:
     * </p>
     * <p>
     * obj.msetnx(new String[]{"key2","value1","key2","value2"})
     * </p>
     *
     * @param keysvalues
     * @return 成功返回1 失敗返回0
     */
    public Long msetnx(String... keysvalues) {
        Jedis jedis = null;
        Long res = 0L;
        try {
            jedis = jedisPool.getResource();
            res = jedis.msetnx(keysvalues);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 設定key的值,並返回一箇舊值
     * </p>
     *
     * @param key
     * @param value
     * @return 舊值 如果key不存在 則返回null
     */
    public String getset(String key, String value) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.getSet(key, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過下標 和key 獲取指定下標位置的 value
     * </p>
     *
     * @param key
     * @param startOffset
     *            開始位置 從0 開始 負數表示從右邊開始擷取
     * @param endOffset
     * @return 如果沒有返回null
     */
    public String getrange(String key, int startOffset, int endOffset) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.getrange(key, startOffset, endOffset);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key 對value進行加值+1操作,當value不是int型別時會返回錯誤,當key不存在是則value為1
     * </p>
     *
     * @param key
     * @return 加值後的結果
     */
    public Long incr(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.incr(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key給指定的value加值,如果key不存在,則這是value為該值
     * </p>
     *
     * @param key
     * @param integer
     * @return
     */
    public Long incrBy(String key, Long integer) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.incrBy(key, integer);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 對key的值做減減操作,如果key不存在,則設定key為-1
     * </p>
     *
     * @param key
     * @return
     */
    public Long decr(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.decr(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 減去指定的值
     * </p>
     *
     * @param key
     * @param integer
     * @return
     */
    public Long decrBy(String key, Long integer) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.decrBy(key, integer);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key獲取value值的長度
     * </p>
     *
     * @param key
     * @return 失敗返回null
     */
    public Long serlen(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.strlen(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key給field設定指定的值,如果key不存在,則先建立
     * </p>
     *
     * @param key
     * @param field
     *            欄位
     * @param value
     * @return 如果存在返回0 異常返回null
     */
    public Long hset(String key, String field, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hset(key, field, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key給field設定指定的值,如果key不存在則先建立,如果field已經存在,返回0
     * </p>
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hsetnx(String key, String field, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hsetnx(key, field, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key同時設定 hash的多個field
     * </p>
     *
     * @param key
     * @param hash
     * @return 返回OK 異常返回null
     */
    public String hmset(String key, Map<String, String> hash, int indexdb) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            res = jedis.hmset(key, hash);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key 和 field 獲取指定的 value
     * </p>
     *
     * @param key
     * @param field
     * @return 沒有返回null
     */
    public String hget(String key, String field) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hget(key, field);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key 和 fields 獲取指定的value 如果沒有對應的value則返回null
     * </p>
     *
     * @param key
     * @param fields
     *            可以使 一個String 也可以是 String陣列
     * @return
     */
    public List<String> hmget(String key, int indexdb, String... fields) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            res = jedis.hmget(key, fields);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key給指定的field的value加上給定的值
     * </p>
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hincrby(String key, String field, Long value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hincrBy(key, field, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key和field判斷是否有指定的value存在
     * </p>
     *
     * @param key
     * @param field
     * @return
     */
    public Boolean hexists(String key, String field) {
        Jedis jedis = null;
        Boolean res = false;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hexists(key, field);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key返回field的數量
     * </p>
     *
     * @param key
     * @return
     */
    public Long hlen(String key) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hlen(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
 
    }
 
    /**
     * <p>
     * 通過key 刪除指定的 field
     * </p>
     *
     * @param key
     * @param fields
     *            可以是 一個 field 也可以是 一個數組
     * @return
     */
    public Long hdel(String key, String... fields) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hdel(key, fields);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key返回所有的field
     * </p>
     *
     * @param key
     * @return
     */
    public Set<String> hkeys(String key) {
        Jedis jedis = null;
        Set<String> res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hkeys(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key返回所有和key有關的value
     * </p>
     *
     * @param key
     * @return
     */
    public List<String> hvals(String key) {
        Jedis jedis = null;
        List<String> res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.hvals(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key獲取所有的field和value
     * </p>
     *
     * @param key
     * @return
     */
    public Map<String, String> hgetall(String key, int indexdb) {
        Jedis jedis = null;
        Map<String, String> res = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            res = jedis.hgetAll(key);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key向list頭部新增字串
     * </p>
     *
     * @param key
     * @param strs
     *            可以使一個string 也可以使string陣列
     * @return 返回list的value個數
     */
    public Long lpush(int indexdb, String key, String... strs) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            jedis.select(indexdb);
            res = jedis.lpush(key, strs);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key向list尾部新增字串
     * </p>
     *
     * @param key
     * @param strs
     *            可以使一個string 也可以使string陣列
     * @return 返回list的value個數
     */
    public Long rpush(String key, String... strs) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.rpush(key, strs);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key在list指定的位置之前或者之後 新增字串元素
     * </p>
     *
     * @param key
     * @param where
     *            LIST_POSITION列舉型別
     * @param pivot
     *            list裡面的value
     * @param value
     *            新增的value
     * @return
     */
    public Long linsert(String key, LIST_POSITION where, String pivot,
                        String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.linsert(key, where, pivot, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key設定list指定下標位置的value
     * </p>
     * <p>
     * 如果下標超過list裡面value的個數則報錯
     * </p>
     *
     * @param key
     * @param index
     *            從0開始
     * @param value
     * @return 成功返回OK
     */
    public String lset(String key, Long index, String value) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.lset(key, index, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key從對應的list中刪除指定的count個 和 value相同的元素
     * </p>
     *
     * @param key
     * @param count
     *            當count為0時刪除全部
     * @param value
     * @return 返回被刪除的個數
     */
    public Long lrem(String key, long count, String value) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.lrem(key, count, value);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key保留list中從strat下標開始到end下標結束的value值
     * </p>
     *
     * @param key
     * @param start
     * @param end
     * @return 成功返回OK
     */
    public String ltrim(String key, long start, long end) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.ltrim(key, start, end);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key從list的頭部刪除一個value,並返回該value
     * </p>
     *
     * @param key
     * @return
     */
    synchronized public String lpop(String key) {
        Jedis jedis = null;
        String res = null;
        try {
            jedis = jedisPool.getResource();
            res = jedis.lpop(key);
        } catch (Exception e) {
 
            log.error(e.getMessage());
        } finally {
            returnResource(jedisPool, jedis);
        }
        return res;
    }
 
    /**
     * <p>
     * 通過key從list尾部刪除一個value,並返回該元素
     * </p>
     *
     * @param key
     * @return
     */
    synchronized public String rpop(String key, int indexdb) {
        Jedis jedis = null;
        String res = null;
        try {