1. 程式人生 > >Redis+Jedis+Spring封裝RedisManager類操作快取

Redis+Jedis+Spring封裝RedisManager類操作快取

Redis作為一個key-value儲存系統。與和Memcached相比,它支援儲存的value型別更多,有string、list、set、zset(sorted set )和hash。 針對這些型別,Redis命令也比較多:

這裡寫圖片描述

而在程式碼中使用jedis就可以操作這些命令實現儲存。 本部落格打算封裝一個個RedisManger類,用於實現操作快取的命令。 Redis配置檔案:

cache.redis.servers=127.0.0.1
cache.redis.port=6379
cache.redis.maxActive=300 cache.redis.maxIdle=200 cache.redis.maxWaitMillis=3000 cache.redis.testOnBorrow=true cache.redis.testOnReturn=true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

spring管理的配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"
>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:Redis.properties</value> </list> </property> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${cache.redis.maxActive}" /> <property name="maxIdle" value="${cache.redis.maxIdle}" /> <property name="maxWaitMillis" value="${cache.redis.maxWaitMillis}" /> <property name="testOnBorrow" value="${cache.redis.testOnBorrow}" /> </bean> <bean id="redisPool" class="com.test.cache.redis.RedisPool" init-method="init"> <property name="config" ref="jedisPoolConfig" /> <property name="serverIp" value="${cache.redis.servers}" /> <property name="port" value="${cache.redis.port}" /> </bean> <bean id="redisManager" class="com.test.cache.redis.RedisManager"> <property name="redisPool" ref="redisPool" /> </bean> </beans>
  • 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

連線池程式碼:

package com.test.cache.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {

    private JedisPoolConfig config; 
    private String serverIp;
    private int port;
    private JedisPool pool;

    public void init() {
        pool = new JedisPool(config, serverIp, port, 4000);
    }

    public Jedis getInstance() {
        return pool.getResource();
    }

    public void returnResource(Jedis jedis) {
        pool.returnResource(jedis);
    }

    public void returnBrokenResource(Jedis jedis){
        pool.returnBrokenResource(jedis);
    }

    public JedisPoolConfig getConfig() {
        return config;
    }

    public void setConfig(JedisPoolConfig config) {
        this.config = config;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

}
  • 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

封裝的RedisManager類:

package com.test.cache.redis;
import java.util.List;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.*;
public class RedisManager {

    private RedisPool redisPool;

    public Jedis getJedis() {
        return redisPool.getInstance();
    }

    public void releaseJedis(Jedis jedis) {
        redisPool.returnResource(jedis);
    }

    public void releaseBrokenJedis(Jedis jedis) {
        redisPool.returnBrokenResource(jedis);
    }

    /**hash
     * 通過key給field設定指定的值,如果key不存在,則先建立 ,存在會覆蓋原來的值
     * @param key
     * @param field欄位
     * @param value
     * @return 如果不存在,新建的返回1,存在返回0, 異常返回null
     * 
     */
    public Long hset(String key, String field, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.hset(key, field, value);
        releaseJedis(jedis);
        return result;
    }
    /**Hash
     * 為雜湊表 key 中的域 field 的值加上增量 value
     * @param key
     * @param field
     * @param value
     * @return
     */
    public Long hincrBy(String key, String field, long value) {
        Jedis jedis = getJedis();
        Long result = jedis.hincrBy(key, field, value);
        releaseJedis(jedis);
        return result;
    }

    /**

     * 通過key給field設定指定的值,如果key不存在則先建立,如果field已經存在,操作無效
     * @param key
     * @param field
     * @param value
     * @return 不存在新建返回1,存在返回0
     */
    public Long hsetnx(String key, String field, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.hsetnx(key, field, value);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key同時設定 hash的多個field
     * @param key
     * @param hash
     * @return 返回OK 異常返回null
     */
    public String hmset(String key, Map<String, String> hash) {
        Jedis jedis = getJedis();
        String result = jedis.hmset(key, hash);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key 和 field 獲取指定的 value
     * @param key
     * @param field
     * @return 沒有返回null
     */
    public String hget(String key, String field) {
        Jedis jedis = getJedis();
        String result = jedis.hget(key, field);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key 和 fields 獲取指定的value 如果沒有對應的value則返回null
     * @param key
     * @param fields可以使 一個String 也可以是 String陣列
     * @return
     */
    public List<String> hmget(String key, String... fields) {
        Jedis jedis = getJedis();
        List<String> result = jedis.hmget(key, fields);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key獲取所有的field和value
     * @param key
     * @return
     */
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = getJedis();
        Map<String, String> result = jedis.hgetAll(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 通過key刪除field的value
     * @param key
     * @return
     */
    public Long hdel(String key, String field) {
        Jedis jedis = getJedis();
        Long result = jedis.hdel(key, field);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回key為鍵中存放的field值的個數
     * @param key
     * @return 
     */
    public Long hlen(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.hlen(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 檢視key是否存在指定的field
     * @param key
     * @return 
     */
    public Boolean hexists(String key, String field) {
        Jedis jedis = getJedis();
        Boolean result = jedis.hexists(key, field);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 返回key儲存的map物件中的所有key  
     * @param key
     * @return 
     */
    public Set<String> hkeys(String key) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.hkeys(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回key儲存的map物件中的所有鍵的values值  
     * @param key
     * @return 
     */
    public List<String> hvals(String key) {
        Jedis jedis = getJedis();
        List<String> result = jedis.hvals(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 判斷key是否存在
     * @param key
     * @return true OR false
     */
    public boolean exists(String key) {
        Jedis jedis = null;
        boolean result = false;
        try {
            jedis = getJedis();
            result = jedis.exists(key);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }

        return result;
    }

    /**
     * 刪除指定的key,也可以傳入一個包含key的陣列
     * @param keys
     * @return 返回刪除成功的個數
     */
    public Long del(String... keys) {
        Jedis jedis = null;
        Long result = 0L;
        try {
            jedis = getJedis();
            result = jedis.del(keys);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
            result = 0L;
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**
     * 對key的對應value值排序
     * @return 
     */
    public List<String> sort(String key) {
        Jedis jedis = getJedis();
        List<String> result = jedis.sort(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 將當前資料庫的 ke移動到給定的資料庫 db 當中
     * @return 
     */
    public Long move(String key, int dbIndex) {
        Jedis jedis = getJedis();
        Long result = jedis.move(key, dbIndex);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回某個key元素的資料型別 ( none:不存在,string:字元,list,set,zset,hash)
     * @return 
     */
    public String type(String key) {
        Jedis jedis = getJedis();
        String result = jedis.type(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回當前資料庫的key的總數
     * @return 
     */
    public Long dbsize() {
        Jedis jedis = getJedis();
        Long result = jedis.dbSize();
        releaseJedis(jedis);
        return result;
    }
    /**
     *設定某個key的過期時間(秒),(EXPIRE bruce 1000:設定bruce這個key1000秒後系統自動刪除)注意:如果在還沒有過期的時候,對值進行了改變,那麼那個值會被清除。
     * @return 
     */
    public Long expire(String key, int seconds) {
        Jedis jedis = getJedis();
        Long result = jedis.expire(key, seconds);
        releaseJedis(jedis);
        return result;
    }
    /**
     * EXPIREAT 的作用和 EXPIRE 類似,都用於為 key 設定生存時間。
     * 不同在於 EXPIREAT 命令接受的時間引數是 UNIX 時間戳(unix timestamp)。
     * @return 
     */

    public Long expireAt(String key, Long unixTime) {
        Jedis jedis = getJedis();
        Long result = jedis.expireAt(key, unixTime);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 通過key在list頭部新增值
     * @param key
     * @param value
     * @return 在 push 操作後的 list 長度。
     */
    public Long lpush(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lpush(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 向存於 key 的列表的尾部插入所有指定的值。如果 key 不存在,那麼會建立一個空的列表然後再進行 push 操作。 
     * 當 key 儲存的不是一個列表,那麼會返回一個錯誤。
     * @param key
     * @param value
     * @return  在 push 操作後的列表長度
     */
    public Long rpush(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.rpush(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 獲取list的長度
     * @param key
     * @return 
     */
    public Long llen(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.llen(key);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 返回儲存在 key 的列表裡指定範圍內的元素
     * @param key 
     * @parm start 開始位置
     * @param end 結束位置 -1表示最後一個
     * @return 
     */
    public List<String> lrange(String key, long start, long end) {
        Jedis jedis = getJedis();
        List<String> result = jedis.lrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 擷取(trim)一個已存在的 list,這樣 list 就會只包含指定範圍的指定元素
     * @param key 
     * @parm start 開始位置
     * @param end 結束位置 -1表示最後一個
     * @return 
     */

    public String ltrim(String key, long start, long end) {
        Jedis jedis = getJedis();
        String result = jedis.ltrim(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**List
     * 通過key在list頭部新增值
     * 只有當 key 已經存在並且存著一個 list 的時候,在這個 key 下面的 list 的頭部插入 value。 與 LPUSH 相反,當 key 不存在的時候不會進行任何操作。
     * @param key
     * @param value
     * @return 在 push 操作後的 list 長度。
     */

    public Long lpushx(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lpushx(key, value);
        releaseJedis(jedis);
        return result;
    }

    public Long rpushx(String key, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.rpushx(key, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 彈出 List 的第一個元素
     * @param key
     * @return
     */
    public String lpop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.lpop(key);
        releaseJedis(jedis);
        return result;
    }

    public String rpop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.rpop(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 根據引數 COUNT 的值,移除列表中與引數 VALUE 相等的元素。
     * count > 0 : 從表頭開始向表尾搜尋,移除與 VALUE 相等的元素,數量為 COUNT 。
     * count < 0 : 從表尾開始向表頭搜尋,移除與 VALUE 相等的元素,數量為 COUNT 的絕對值。
     * count = 0 : 移除表中所有與 VALUE 相等的值。
     */

    public Long lrem(String key, long count, String value) {
        Jedis jedis = getJedis();
        Long result = jedis.lrem(key, count, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 設定 index 位置的list元素的值為 value
     * @param key
     * @param index
     * @param value
     * @return
     */
    public String lset(String key, long index, String value) {
        Jedis jedis = getJedis();
        String result = jedis.lset(key, index, value);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回列表 key 中,下標為 index 的元素。
     * @param key
     * @param index
     * @return
     */
    public String lindex(String key, long index) {
        Jedis jedis = getJedis();
        String result = jedis.lindex(key, index);
        releaseJedis(jedis);
        return result;
    }
   /**
    *命令 RPOPLPUSH 在一個原子時間內,執行以下兩個動作:
    *將列表 source 中的最後一個元素(尾元素)彈出,並返回給客戶端。
    *將 source 彈出的元素插入到列表 destination ,作為 destination 列表的的頭元素。
    *舉個例子,你有兩個列表 source 和 destination , source 列表有元素 a, b, c , destination 列表有元素 x, y, z ,
    *執行 RPOPLPUSH source destination 之後, source 列表包含元素 a, b , destination 列表包含元素 c, x, y, z ,
    *並且元素 c 會被返回給客戶端。
    * @param srcKey
    * @param dstKey
    * @return
    */
    public String rpoplpush(String srcKey, String dstKey) {
        Jedis jedis = getJedis();
        String result = jedis.rpoplpush(srcKey, dstKey);
        releaseJedis(jedis);
        return result;
    }
    /**
     * BRPOPLPUSH 是 RPOPLPUSH 的阻塞版本,當給定列表 source 不為空時, BRPOPLPUSH 的表現和 RPOPLPUSH 一樣。
     *當列表 source 為空時, BRPOPLPUSH 命令將阻塞連線,直到等待超時,或有另一個客戶端對 source 執行 LPUSH 或 RPUSH 命令為止。
     *超時引數 timeout 接受一個以秒為單位的數字作為值。超時引數設為 0 表示阻塞時間可以無限期延長(block indefinitely) 。
     * @param source
     * @param destination
     * @param timeout
     * @return
     */
    public String brpoplpush(String source, String destination, int timeout) {
        Jedis jedis = getJedis();
        String result = jedis.brpoplpush(source, destination, timeout);
        releaseJedis(jedis);
        return result;
    }


   /**
    * 將一個或多個 member 元素加入到集合 key 當中,已經存在於集合的 member 元素將被忽略(set不重複)。
    * 假如 key 不存在,則建立一個只包含 member 元素作成員的集合。
    * @param key
    * @param member
    * @return
    */
    public Long sadd(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.sadd(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除集合 key 中的一個 member 元素,不存在的 member 元素會被忽略。
     * @param key
     * @param member
     * @return
     */
    public Long srem(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.srem(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回集合 key 中的所有成員。
     * @param key
     * @return
     */
    public Set<String> smembers(String key) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.smembers(key);
        releaseJedis(jedis);
        return result;
    }
     /**
      * 判斷 member 元素是否集合 key 的成員。
      * @param key
      * @param member
      * @return
      */
    public Boolean sismember(String key, String member) {
        Jedis jedis = getJedis();
        Boolean result = jedis.sismember(key, member);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回集合 key集合中元素的數量)。
    * @param key
    * @return
    */
    public Long scard(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.scard(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 將 member 元素從 source 集合移動到 destination 集合。
     *SMOVE 是原子性操作。
     *如果 source 集合不存在或不包含指定的 member 元素,則 SMOVE 命令不執行任何操作,僅返回 0 。否則, member 元素從 source 集合中被移除,並新增到 destination 集合中去。
     *當 destination 集合已經包含 member 元素時, SMOVE 命令只是簡單地將 source 集合中的 member 元素刪除。
     *當 source 或 destination 不是集合型別時,返回一個錯誤。
     * @param srckey
     * @param dstkey
     * @param member
     * @return
     */
    public Long smove(String srckey, String dstkey, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.smove(srckey, dstkey, member);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 移除並返回集合中的一個隨機元素。
     * @param key
     * @return
     */
    public String spop(String key) {
        Jedis jedis = getJedis();
        String result = jedis.spop(key);
        releaseJedis(jedis);
        return result;
    }
     /**
      * 返回集合中的一個隨機元素
      * @param key
      * @return
      */
    public String srandmember(String key) {
        Jedis jedis = getJedis();
        String result = jedis.srandmember(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 返回給定集合的交集。
     * @param keys
     * @return
     */
    public Set<String> sinter(String... keys) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.sinter(keys);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 類似於 SINTER 命令,但它將結果儲存到 destination 集合,而不是簡單地返回結果集
     * @param dstkey
     * @param keys
     * @return
     */
    public Long sinterstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sinterstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回所有給定集合的並集
    * @param keys
    * @return
    */
    public Set<String> sunion(String... keys) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.sunion(keys);
        releaseJedis(jedis);
        return result;
    }

    public Long sunionstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sunionstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回所有給定集合之間的差集。
     * @param keys
     * @return
     */
    public Set<String> sdiff(String... keys) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.sdiff(keys);
        releaseJedis(jedis);
        return result;
    }

    public Long sdiffstore(String dstkey, String... keys) {
        Jedis jedis = getJedis();
        Long result = jedis.sdiffstore(dstkey, keys);
        releaseJedis(jedis);
        return result;
    }
   /**server
    * 清空整個 Redis 伺服器的資料(刪除所有資料庫的所有 key )。
    * @return
    */
    public String flushAll() {
        Jedis jedis = getJedis();
        String result = jedis.flushAll();
        releaseJedis(jedis);
        return result;
    }
   /**
    * 清空當前資料庫中的所有 key。
    * @return
    */
    public String flushDB() {
        Jedis jedis = getJedis();
        String result = jedis.flushDB();
        releaseJedis(jedis);
        return result;
    }
    /** 
    *停止所有客戶端
    *如果有至少一個儲存點在等待,執行 SAVE 命令
    *如果 AOF 選項被開啟,更新 AOF 檔案
    * 關閉 redis 伺服器(server)
    * @return
    */

    public String shutdown() {
        Jedis jedis = getJedis();
        String result = jedis.shutdown();
        releaseJedis(jedis);
        return result;
    }

    /**sorted set
     * 將一個 member 元素及其 score值加入到有序集 key 當中。
     * 如果某個 member 已經是有序集的成員,那麼更新這個 member 的 score 值,
     *並通過重新插入這個 member 元素,來保證該 member 在正確的位置上。
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Long zadd(String key, double score, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zadd(key, score, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * sorted set
     * 移除有序集 key 中的一成員member,不存在的成員將被忽略。
     * @param key
     * @param member
     * @return
     */
    public Long zrem(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrem(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**sorted set
     * 返回集合 key集合中元素的數量
     * @param key
     * @return
     */
    public Long zcard(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.zcard(key);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中, score 值在 min 和 max 之間(預設包括 score 值等於 min 或 max )的成員的數量。
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Long zcount(String key, double min, double max) {
        Jedis jedis = getJedis();
        Long result = jedis.zcount(key, min, max);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中,成員 member 的 score 值。
     * @param key
     * @param member
     * @return
     */
    public Double zscore(String key, String member) {
        Jedis jedis = getJedis();
        Double result = jedis.zscore(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 為有序集 key 的成員 member 的 score 值加上增量"score"
     * @param key
     * @param score
     * @param member
     * @return
     */
    public Double zincrby(String key, double score, String member) {
        Jedis jedis = getJedis();
        Double result = jedis.zincrby(key, score, member);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 返回有序集 key 中,指定區間內的成員。其中成員的位置按 score 值遞增(從小到大)來排序。
    * @param key
    * @param start
    * @param end
    * @return
    */
    public Set<String> zrange(String key, int start, int end) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.zrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 其中成員的位置按 score 值遞減(從大到小)來排列。
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrevrange(String key, int start, int end) {
        Jedis jedis = getJedis();
        Set<String> result = jedis.zrevrange(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中, score 值介於 max 和 min 之間(預設包括等於 max 或 min )的所有的成員。
     * 有序整合員按 score 值遞減(從大到小)的次序排列。
     * @param key
     * @param max
     * @param min
     * @param offset
     * @param count
     * @return
     */
    public Set<String> zrevrangeByScore(String key, double max, double min,int offset, int count) {
        Jedis jedis=getJedis();
        try{
            Set<String> result=jedis.zrevrangeByScore(key, max, min, offset, count);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }

    public Set<String> zrevrangeByScore(String key, double max, double min) {
        Jedis jedis=getJedis();
        try{
            Set<String> result=jedis.zrevrangeByScore(key, max, min);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }
    /**
     * 返回有序集 key 中,所有 score 值介於 min 和 max 之間(包括等於 min 或 max )的成員。
     * 有序整合員按 score 值遞增(從小到大)次序排列。
     * @param key
     * @param min
     * @param max
     * @return
     */
    public Set<String> zrangeByScore(String key, double min, double max) {
        Jedis jedis=getJedis();
        try{
            Set<String> result=jedis.zrangeByScore(key, min, max);
            return result;
        }finally{
            releaseJedis(jedis);
        }
    }
    /**
     * 返回有序集 key 中成員 member 的排名。其中有序整合員按 score 值遞增(從小到大)順序排列。
     *  排名以 0 為底,也就是說, score 值最小的成員排名為 0 。
     */

    public Long zrank(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrank(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回有序集 key 中成員 member 的排名。其中有序整合員按 score 值遞減(從大到小)排序。
     * 排名以 0 為底,也就是說, score 值最大的成員排名為 0 。
     * @param key
     * @param member
     * @return
     */
    public Long zrevrank(String key, String member) {
        Jedis jedis = getJedis();
        Long result = jedis.zrevrank(key, member);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除有序集 key 中,指定排名(rank)區間內的所有成員。
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Long zremrangeByRank(String key, int start, int end) {
        Jedis jedis = getJedis();
        Long result = jedis.zremrangeByRank(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 移除有序集 key 中,所有 score 值介於 min 和 max 之間(包括等於 min 或 max )的成員。
     * @param key
     * @param start
     * @param end
     * @return
     */

    public Long zremrangeByScore(String key, double start, double end) {
        Jedis jedis = getJedis();
        Long result = jedis.zremrangeByScore(key, start, end);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 計算給定的一個或多個有序集的交集,並將該交集(結果集)儲存到 destination 。
     * 預設情況下,結果集中某個成員的 score 值是所有給定集下該成員 score 值之和.
     * @param dstkey
     * @param sets
     * @return
     */
    public Long zinterstore(String dstkey, String... sets) {
        Jedis jedis = getJedis();
        Long result = jedis.zinterstore(dstkey, sets);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 計算給定的一個或多個有序集的並集,並將該並集(結果集)儲存到 destination 。
     * 預設情況下,結果集中某個成員的 score 值是所有給定集下該成員 score 值之 和 。
     * @param dstkey
     * @param sets
     * @return
     */
    public Long zunionstore(String dstkey, String... sets) {
        Jedis jedis = getJedis();
        Long result = jedis.zunionstore(dstkey, sets);
        releaseJedis(jedis);
        return result;
    }

    /**String
     * 通過key獲取儲存在redis中的value
     * 並釋放連線
     * @param key
     * @return 成功返回value 失敗返回null
     */
    public String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**string
     * 向redis存入key和value,並釋放連線資源
     * 如果key已經存在 則覆蓋
     * @param key
     * @param value
     * @return 成功 返回OK 失敗返回 0
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
            result = "0";
        } finally {
            releaseJedis(jedis);
        }
        return result;
    }

    /**
     * <p>
     * 設定key value,如果key已經存在則返回0,nx==> not exist
     * @param key
     * @param value
     * @return 成功返回1 如果存在 和 發生異常 返回 0
     */
    public Long setnx(String key, String value) {
        Jedis jedis = null;
        Long result = 0L;
        try {
            jedis = getJedis();
            result = jedis.setnx(key, value);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
        } finally {
            releaseJedis(jedis);
        }

        return result;
    }
  /**string
   * 將給定 key 的值設為 value ,並返回 key 的舊值(old value)。
   * @param key
   * @param value
   * @return
   */
    public String getSet(String key, String value) {
        Jedis jedis = getJedis();
        String result = jedis.getSet(key, value);
        releaseJedis(jedis);
        return result;
    }
/**
 * 返回所有(一個或多個)給定 key 的值。
 * 如果給定的 key 裡面,有某個 key 不存在,那麼這個 key 返回特殊值 nil 。因此,該命令永不失敗。
 * @param keys
 * @return
 */
    public List<String> mget(String[] keys) {
        Jedis jedis = getJedis();
        List<String> result = jedis.mget(keys);
        releaseJedis(jedis);
        return result;
    }
   /**
    * 同時設定一個或多個 key-value 對。
    * 有會覆蓋
    * @param keysvalues
    */
    public void mset(String... keysvalues) {
        Jedis jedis = getJedis();
        jedis.mset(keysvalues);
        releaseJedis(jedis);
    }
    /**
     * key不存在時才插入
     * @param keysvalues
     */
    public void msetnx(String... keysvalues) {
        Jedis jedis = getJedis();
        jedis.msetnx(keysvalues);
        releaseJedis(jedis);
    }
     /**
      * 將 key 所儲存的值加上增量 increment 。
      * 如果 key 不存在,那麼 key 的值會先被初始化為 0 ,然後再執行 INCRBY 命令。
      * @param key
      * @param integer
      * @return
      */
    public Long incrBy(String key, Integer integer) {
        Jedis jedis = getJedis();
        Long result = jedis.incrBy(key, integer);
        releaseJedis(jedis);
        return result;
    }
    /**
     * 返回 key 所儲存的字串值的長度。
     * @param key
     * @return
     */
    public Long strlen(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.strlen(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key 對value進行加值+1操作,當value不是int型別時會返回錯誤,當key不存在是則value為1
     * @param key
     * @return 加值後的結果
     */
    public Long incr(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.incr(key);
        releaseJedis(jedis);
        return result;
    }



    /**
     * 對key的值做減減操作,如果key不存在,則設定key為-1

     * @param key
     * @return
     */
    public Long decr(String key) {
        Jedis jedis = getJedis();
        Long result = jedis.decr(key);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 減去指定的值
     * @param key
     * @param integer
     * @return
     */
    public Long decrBy(String key, Integer integer) {
        Jedis jedis = getJedis();
        Long result = jedis.decrBy(key, integer);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 通過key向指定的value值追加值
     * @param key
     * @param str
     * @return 成功返回 新增後value的長度 失敗 返回 新增的 value 的長度 異常返回0L
     */
    public Long append(String key, String str) {
        Jedis jedis = null;
        Long res = null;
        try {
            jedis = getJedis();
            res = jedis.append(key, str);
        } catch (Exception e) {
            releaseBrokenJedis(jedis);
            e.printStackTrace();
            res = 0L;
        } finally {
            releaseJedis(jedis);
        }
        return res;
    }

    public String subStr(String key, int Start, int end) {
        Jedis jedis = getJedis();
        String result = jedis.substr(key, Start, end);
        releaseJedis(jedis);
        return result;
    }

    /**
     * 設定key value並制定這個鍵值的有效期
     * @param key
     * @param value
     *
            
           

相關推薦

Redis+Jedis+Spring封裝RedisManager操作快取

Redis作為一個key-value儲存系統。與和Memcached相比,它支援儲存的value型別更多,有string、list、set、zset

Redis+Jedis+Spring封裝RedisManager操作快取(三)

Redis作為一個key-value儲存系統。與和Memcached相比,它支援儲存的value型別更多,有string、list、set、zset(sorted set )和hash。 針對這些型別,Redis命令也比較多: 而在程式碼中使用jedis

Redis:Jedis封裝:RedisTemplate在Springboot中的使用

一、 首先RedisTemplate是什麼? RedisTemplate簡單來說就是Jedis的封裝, 在沒有RedisTemplate之前,我們需要為每一個Redis連線做這些事情: 1)Redis連線池 2)Redis伺服器 3)寫Redis操作語句 4)Red

征服 Redis + Jedis + Spring (二)—— 雜湊表操作(HMGET HMSET)

一、預期 接上一篇,擴充User屬性: Java程式碼   public class User implements Serializable {       private static final long serialVersionUID = -1267

Asp.Net Core 2.0 項目實戰(6)Redis配置、封裝幫助RedisHelper及使用實例

命名 redis數據庫 remove per chang open htm lazy 鏈接 本文目錄 1. 摘要 2. Redis配置 3. RedisHelper 4.使用實例 5. 總結 1. 摘要   由於內存存取速度遠高於磁盤讀取的特

redis整合spring(redisTemplate工具) redis整合spring(redisTemplate工具)

redis整合spring(redisTemplate工具類) 原文地址:http://blog.csdn.net/qq_34021712/article/details/75949706   ©王賽超 前言 關於哨兵模式的配置,我是參考網上

Redis實戰之徵服 Redis + Jedis + Spring (二)

不得不說,用雜湊操作來存物件,有點自討苦吃! 不過,既然吃了苦,也做個記錄,也許以後API升級後,能好用些呢?! 或許,是我的理解不對,沒有真正的理解雜湊表。 相關連結: 一、預期 接上一篇,擴充User屬性: Java程式碼   publiccla

Redis實戰之徵服 Redis + Jedis + Spring (三)

一開始以為Spring下操作雜湊表,列表,真就是那麼土。恍惚間發現“stringRedisTemplate.opsForList()”的強大,抓緊時間惡補下。 通過spring-data-redis完成LINDEX, LLEN, LPOP, LPUSH, LRANGE, L

Redis 整合spring ,做mysql的快取

專案環境: 在SpringMVC + MyBatis + Mysql。Redis部署在Linux虛擬機器。 1、整體思路 參考Ehcache實現MyBatis二級快取程式碼(Maven引用對應jar查閱)使用Spring管理Redis連線池模仿EhcacheCache

使用Spring Boot整合Redis操作快取

下來操作SpringBoot整合Redis 首先Redis是一個開源(BSD許可)的,記憶體中的資料結構儲存系統,它可以用作資料庫,快取和訊息中介軟體 1)使用docker安裝redis 如果沒有安裝Redis, 請使用以下命令安裝: [[email 

Redis操作List工具封裝,Java Redis List命令封裝

Redis列表是簡單的字串列表,按照插入順序排序。你可以新增一個元素導列表的頭部(左邊)或者尾部(右邊) 一個列表最多可以包含 232 - 1 個元素 (4294967295, 每個列表超過40億個元素)。 下載 Java程式碼   /*********

使用spring-data-redis進行對redis操作封裝的一些操作方法

   這個算是工作筆記吧,因為是我的實際工作內容    spring-data-redis api地址  http://docs.spring.io/spring-data/redis/docs/current/api/   依賴maven包(當前spring-data-

基本的快取操作封裝(抽象

Imports SystemImports System.WebImports System.Web.CachingNamespace DRMSystem.Common.Caching    PublicMustInheritClass DataCacheAbstract#Region "Abstract M

redis的五大數據型以及與 key 關鍵字相關的常用操作命令

redis數據類型 key關鍵字相關的指令 redis學習 1、redis的五大數據類型: 先來看看redis官方網上文檔 的介紹: 這裏簡單地說,就是redis不是一個普通的 key-value 存儲,而是一個數據結構服務器,支持各種不同 類型的值,這

功能比較全的StackExchange.Redis封裝幫助(.Net/C#)

fault wke setting ret stack gin lex sortedset each Redis官網https://redis.io/ 以下內容未全部驗證,如有問題請指出 //static NewtonsoftSerializer serializer

jedis,spring-redis-data 整合使用,版本問題異常

問題 artifact ons com pri connect def 中心 div jedis,spring-redis-data 整合使用,版本不匹配的時候經常會報一些異常,例如1: java.lang.NoClassDefFoundError: org/springf

[Spring MVC] 表單提交日期轉換問題,比如可能導致封裝實體時400錯誤

new tac med tab mat -m bin Edito ack   三種格式的InitBinder @InitBinder//https://stackoverflow.com/questions/20616319/the-request-sent-by-the

Spring Data Redis 2.x 中 RedisConfiguration 的新編寫方法

redis 分享 name pub 名稱 per localhost 端口號 vat 在 Spring Data Redis 1.x 的時候,我們可能會在項目中編寫這樣一個RedisConfig類: @Configuration @EnableCaching public

redisspring boot中 使用redis hash 操作

edi oar else .get als ash tcl del ima 示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dea

redis中各種數據型的常用操作方法匯總

sca 底層 float 逆序 常用命令 表示 oat 另一個 str 一、Redis的五大數據類型 1.String(字符串) string是redis最基本的類型,你可以理解成與Memcached一模一樣的類型,一個key對應一個value。string類型是二進制安全