Redis學習


1 概述

    目前多數的NoSql數據庫本質上都是鍵值對形式,Redis也不例外。作為緩存數據庫的一種,和Memcached相比,有以下幾種主要的優點:

(1)速度上,Redis要比Memcached快,這是基於一些Benchmarks的測試結果得出的,而且在內存使用上,Redis突破了物理內存限制,可以使用虛擬內存

(2)數據類型比Memcached要多,Redis支持List、Set、SortedSet、HashMap等多種數據結構;

(3)持久化方面,Memcached沒有相應的持久化機制,而Redis有RDB快照和AOF日志兩種形式結合做持久化,很大限度上保證了數據的持久化和安全性,不像Memcached斷電后全都沒了。

    Redis不失為Memcached的一種良好的替代方案。

    MongoDB是一種適合做格式化文檔的存儲及查詢的NoSql數據庫,它可以可以用來做海量數據存儲,畢竟是一種硬盤數據庫,和Redis這種緩存數據庫沒什么可比性。網上那么多討論的,這里就不說了,見參考文檔1。

    Redis也可以在硬盤上存儲數據以及跨節點復制數據,也可以用來做隊列系統,據說GitHub就采用Redis作為其基礎架構。

2 安裝部署

    目前官方發布的最新穩定版是2.8.6,我們就以它為例進行下述的操作及說明。

2.1 Centos安裝部署

    安裝步驟如下:

(1)將redis-2.8.6.tar.gz安裝包拷貝到我們的指定目錄下;

(2)使用命令tar -zxvf redis-2.8.6.tar.gz將tar包解壓縮,得到redis-2.8.6目錄;

(3)進入redis-2.8.6目錄,執行make命令進行編譯;

(4)編譯結束之后,配置redis.conf文件,將“daemonize”屬性設置為“yes”,表示我們會以后台進程形式啟動Redis服務;將“port”屬性設置為指定的端口,這里默認為“6379”;將“logfile”屬性設置為指定的日志路徑,其余的屬性可以保持默認。

(5)使用命令src/redis-server ./redis.conf啟動Redis服務,啟動之后,可以在剛才配置的日志路徑中看到如下類似信息:

 

這個帥氣的圖標就是Redis的LOGO了。

(6)執行客戶端命令,驗證服務是否正常:

 

(7)Redis服務停止命令,src/redis-cli shutdown

2.2 conf配置文件說明

    剛才在部署的時候,提到了redis.conf這個文件,這是整個Redis的最重要的配置文件,對於其中的一些參數,做如下說明:

屬性

說明

daemonize

如果值是“yes”,則啟動服務的時候是后台守護進程形式,如果值是“no”,則相反

pidfile

指定存儲Redis進程號的文件路徑

port

指定當前Redis服務的端口,默認為6379

tcp-backlog

此參數確定了TCP連接中已完成隊列(完成三次握手之后)的長度, 當然此值必須不大於Linux系統定義的/proc/sys/net/core/somaxconn值,默認是511,而Linux的默認參數值是128。當系統並發量大並且客戶端速度緩慢的時候,可以將這二個參數一起參考設定。

timeout

客戶端和Redis服務端的連接超時時間,默認是0,表示永不超時。

tcp-keepalive

如果值非0,單位是秒,表示將周期性的使用SO_KEEPALIVE檢測客戶端是否還處於健康狀態,避免服務器一直阻塞,官方給出的建議值是60S。

loglevel

Redis總共支持四個級別:debug、verbose、notice、warning

Debug:記錄很多信息,用於開發和測試;

Varbose:有用的信息,不像debug會記錄那么多;

Notice:普通的verbose,常用於生產環境;

Warning:只有非常重要或者嚴重的信息會記錄到日志;

默認是notice級別。

logfile

日志的存儲路徑

databases

可用的數據庫數,默認值為16,默認數據庫為0,數據庫范圍在0-(database-1)之間,個人覺得DB的概念類似於命名空間

save

保存數據庫快照信息到磁盤,其對應的值有兩個,比如save 300 10表示:300秒內至少有300個key被改變時,觸發保存信息到磁盤的事件。

stop-writes-on-bgsave-error

當持久化出現錯誤之后,是否繼續提供寫服務

rdbcompression

持久化到RDB文件時,是否壓縮,“yes”為壓縮,“no”則反之

rdbchecksum

讀取和寫入的時候是否支持CRC64校驗,默認是開啟的

dbfilename

鏡像文件的名字

dir

當前工作目錄,配置文件和鏡像文件等都在此目錄下

masterauth

設置訪問master服務器的密碼

slave-serve-stale-data

當slave服務器和master服務器失去連接后,或者當數據正在復制傳輸的時候,如果此參數值設置“yes”,slave服務器可以繼續接受客戶端的請求,否則,會返回給請求的客戶端如下信息“SYNC with master in progress

slave-read-only

是否允許slave服務器節點只提供讀服務

repl-disable-tcp-nodelay

指定向slave同步數據時,是否禁用socket的NO_DELAY選項。若配置為“yes”,則禁用NO_DELAY,則TCP協議棧會合並小包統一發送,這樣可以減少主從節點間的包數量並節省帶寬,但會增加數據同步到slave的時間。若配置為“no”,表明啟用NO_DELAY,則TCP協議棧不會延遲小包的發送時機,這樣數據同步的延時會減少,但需要更大的帶寬。通常情況下,應該配置為no以降低同步延時,但在主從節點間網絡負載已經很高的情況下,可以配置為yes。

slave-priority

指定slave的優先級。在不只1個slave存在的部署環境下,當master宕機時,Redis Sentinel會將priority值最小的slave提升為master。需要注意的是,若該配置項為0,則對應的slave永遠不會自動提升為master。

appendonly

開啟append only 模式之后,redis 會把所接收到的每一次寫操作請求都追加到appendonly.aof 文件中,當redis 重新啟動時,會從該文件恢復出之前的狀態。但是這樣會造成appendonly.aof 文件過大,所以redis 還支持了BGREWRITEAOF 指令,對appendonly.aof 進行重新整理。默認是不開啟的。

appendfilename

默認為appendonly.aof

appendfsync

設置aof的同步頻率,有三種選擇always、everysec、no,默認是everysec表示每秒同步一次。

no-appendfsync-on-rewrite

指定是否在后台aof文件rewrite期間調用fsync,默認為no,表示要調用fsync(無論后台是否有子進程在刷盤)。Redis在后台寫RDB文件或重寫afo文件期間會存在大量磁盤IO,此時,在某些linux系統中,調用fsync可能會阻塞。

auto-aof-rewrite-percentage

指定Redis重寫aof文件的條件,默認為100,表示與上次rewrite的aof文件大小相比,當前aof文件增長量超過上次afo文件大小的100%時,就會觸發background rewrite。若配置為0,則會禁用自動rewrite

auto-aof-rewrite-min-size

指定觸發rewrite的aof文件大小。若aof文件小於該值,即使當前文件的增量比例達到auto-aof-rewrite-percentage的配置值,也不會觸發自動rewrite。即這兩個配置項同時滿足時,才會觸發rewrite。

lua-time-limit

一個Lua腳本最長的執行時間,單位為毫秒,如果為0或負數表示無限執行時間,默認為5000

notify-keyspace-events

見參考3,按鍵通知事件

aof-rewrite-incremental-fsync

aof rewrite過程中,是否采取增量文件同步策略,默認為“yes”。 rewrite過程中,每32M數據進行一次文件同步,這樣可以減少aof大文件寫入對磁盤的操作次數

 

3 基本原理

    Redis是C編寫的,對外開放的List、Set、SortedSet等數據結構都有着自己獨到的實現和算法,其內部的實現原理見參考4,這里就不賣弄了,我也在學習。

    需求推動技術的進步,Redis可以應用在如下幾種場景中:

(1)取最新N個數據操作

(2)排行榜操作,Top N

(3)需要精准設定過期時間的應用

(4)計數器應用

(5)獲取某段時間內所有數據的排重值,uniq操作

(6)實時系統

(7)發布/訂閱系統

(8)隊列系統

(9)緩存

    具體的場景分析見參考9。

4 客戶端操作

    使用src/redis-cli可以打開客戶端對話框,輸入命令即可。目前Redis支持String、Map、List、Set、SortedSets等五種數據結構以及支持訂閱、事務等操作。每種命令的操作及詳細解釋見參考8。

5 Java操作Redis

    目前Jedis是官方推薦的比較好的Redis操作API包,我們這里結合Spring來看下如何使用Redis這一神器。 

    首先在pom文件中引入jredis包:

1 <dependency>
2    <groupId>redis.clients</groupId>
3    <artifactId>jedis</artifactId>
4    <version>2.1.0</version>
5 </dependency>

  其次,在Spring的resource目錄中增加Redis的配置文件/src/main/resources/redis.properties

 1 # Redis settings
 2 redis.host=192.168.1.106
 3 redis.port=6379
 4 redis.pass=
 5 redis.timeout=0
 6 
 7 redis.maxIdle=300
 8 redis.maxActive=600
 9 redis.maxWait=1000
10 redis.testOnBorrow=true

  接着,在Spring的配置文件中配置好Redis的相關Bean注入:

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
 4     xmlns:context="http://www.springframework.org/schema/context"  
 5     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
 6     xmlns:aop="http://www.springframework.org/schema/aop"  
 7     xsi:schemaLocation="  
 8             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
 9             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
10 
11     <context:property-placeholder location="classpath:redis.properties" />  
12     <context:component-scan base-package="com.scott.demo" />
13     
14     <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
15     
16     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
17         <property name="maxActive" value="50" />
18         <property name="maxIdle" value="8" />
19         <property name="maxWait" value="1000" />
20         <property name="testOnBorrow" value="true"/>
21         <property name="testOnReturn" value="true"/>
22         <!-- <property name="testWhileIdle" value="true"/> -->
23     </bean>
24 
25     <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
26         <constructor-arg index="0" ref="jedisPoolConfig" />
27         <constructor-arg index="1">
28             <list>
29                 <bean class="redis.clients.jedis.JedisShardInfo">
30                     <constructor-arg name="host" value="${redis.host}" />
31                     <constructor-arg name="port" value="${redis.port}" />
32                     <constructor-arg name="timeout" value="${redis.timeout}" />
33                     <constructor-arg name="weight" value="1" />
34                 </bean>
35             </list>
36         </constructor-arg>
37     </bean>
38 </beans>

  配置文件准備就緒,下面是獲取Redis客戶端對象的類:

 1 package com.scott.demo.redis.service.impl;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Repository;
 7 
 8 import com.scott.demo.redis.service.RedisService;
 9 
10 import redis.clients.jedis.ShardedJedis;
11 import redis.clients.jedis.ShardedJedisPool;
12 
13 /**
14  * @author Scott
15  * @date 2014年2月23日
16  * @description
17  */
18 @Repository("redisService")
19 public class RedisServiceImpl implements RedisService {
20 
21     private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);
22 
23     @Autowired
24     private ShardedJedisPool shardedJedisPool;
25 
26     public ShardedJedis getRedisClient() {
27         try {
28             ShardedJedis shardJedis = shardedJedisPool.getResource();
29             return shardJedis;
30         } catch (Exception e) {
31             log.error("getRedisClent error", e);
32         }
33         return null;
34     }
35 
36     public void returnResource(ShardedJedis shardedJedis) {
37         shardedJedisPool.returnResource(shardedJedis);
38     }
39 
40     public void returnResource(ShardedJedis shardedJedis, boolean broken) {
41         if (broken) {
42             shardedJedisPool.returnBrokenResource(shardedJedis);
43         } else {
44             shardedJedisPool.returnResource(shardedJedis);
45         }
46     }
47 
48 }

 

  根據上述類,可以在下面的RedisClient類中實現Redis的相關操作:

   1 package com.scott.demo.redis;
   2 
   3 import java.util.Collection;
   4 import java.util.List;
   5 import java.util.Map;
   6 import java.util.Set;
   7 
   8 import org.slf4j.Logger;
   9 import org.slf4j.LoggerFactory;
  10 import org.springframework.beans.factory.annotation.Autowired;
  11 
  12 import com.scott.demo.redis.service.impl.RedisServiceImpl;
  13 
  14 import redis.clients.jedis.BinaryClient.LIST_POSITION;
  15 import redis.clients.jedis.Jedis;
  16 import redis.clients.jedis.JedisShardInfo;
  17 import redis.clients.jedis.ShardedJedis;
  18 import redis.clients.jedis.ShardedJedisPipeline;
  19 import redis.clients.jedis.SortingParams;
  20 import redis.clients.jedis.Tuple;
  21 
  22 /**
  23  * @author Scott
  24  * @date 2014年2月23日
  25  * @description
  26  */
  27 public class RedisClient {
  28 
  29     private static final Logger log = LoggerFactory.getLogger(RedisClient.class);
  30 
  31     @Autowired
  32     private RedisServiceImpl redisService;
  33 
  34     public void disconnect() {
  35         ShardedJedis shardedJedis = redisService.getRedisClient();
  36         shardedJedis.disconnect();
  37     }
  38 
  39     /**
  40      * 設置單個值
  41      */
  42     public String set(String key, String value) {
  43         String result = null;
  44 
  45         ShardedJedis shardedJedis = redisService.getRedisClient();
  46         if (shardedJedis == null) {
  47             return result;
  48         }
  49         boolean flag = false;
  50         try {
  51             result = shardedJedis.set(key, value);
  52         } catch (Exception e) {
  53             log.error(e.getMessage(), e);
  54             flag = true;
  55         } finally {
  56             redisService.returnResource(shardedJedis, flag);
  57         }
  58         return result;
  59     }
  60 
  61     /**
  62      * 獲取單個值
  63      */
  64     public String get(String key) {
  65         String result = null;
  66         ShardedJedis shardedJedis = redisService.getRedisClient();
  67         if (shardedJedis == null) {
  68             return result;
  69         }
  70 
  71         boolean flag = false;
  72         try {
  73             result = shardedJedis.get(key);
  74 
  75         } catch (Exception e) {
  76             log.error(e.getMessage(), e);
  77             flag = true;
  78         } finally {
  79             redisService.returnResource(shardedJedis, flag);
  80         }
  81         return result;
  82     }
  83 
  84     public Boolean exists(String key) {
  85         Boolean result = false;
  86         ShardedJedis shardedJedis = redisService.getRedisClient();
  87         if (shardedJedis == null) {
  88             return result;
  89         }
  90         boolean flag = false;
  91         try {
  92             result = shardedJedis.exists(key);
  93         } catch (Exception e) {
  94             log.error(e.getMessage(), e);
  95             flag = true;
  96         } finally {
  97             redisService.returnResource(shardedJedis, flag);
  98         }
  99         return result;
 100     }
 101 
 102     public String type(String key) {
 103         String result = null;
 104         ShardedJedis shardedJedis = redisService.getRedisClient();
 105         if (shardedJedis == null) {
 106             return result;
 107         }
 108         boolean flag = false;
 109         try {
 110             result = shardedJedis.type(key);
 111 
 112         } catch (Exception e) {
 113             log.error(e.getMessage(), e);
 114             flag = true;
 115         } finally {
 116             redisService.returnResource(shardedJedis, flag);
 117         }
 118         return result;
 119     }
 120 
 121     /**
 122      * 在某段時間后失效
 123      */
 124     public Long expire(String key, int seconds) {
 125         Long result = null;
 126         ShardedJedis shardedJedis = redisService.getRedisClient();
 127         if (shardedJedis == null) {
 128             return result;
 129         }
 130         boolean flag = false;
 131         try {
 132             result = shardedJedis.expire(key, seconds);
 133 
 134         } catch (Exception e) {
 135             log.error(e.getMessage(), e);
 136             flag = true;
 137         } finally {
 138             redisService.returnResource(shardedJedis, flag);
 139         }
 140         return result;
 141     }
 142 
 143     /**
 144      * 在某個時間點失效
 145      */
 146     public Long expireAt(String key, long time) {
 147         Long result = null;
 148         ShardedJedis shardedJedis = redisService.getRedisClient();
 149         if (shardedJedis == null) {
 150             return result;
 151         }
 152         boolean flag = false;
 153         try {
 154             result = shardedJedis.expireAt(key, time);
 155 
 156         } catch (Exception e) {
 157             log.error(e.getMessage(), e);
 158             flag = true;
 159         } finally {
 160             redisService.returnResource(shardedJedis, flag);
 161         }
 162         return result;
 163     }
 164 
 165     public Long ttl(String key) {
 166         Long result = null;
 167         ShardedJedis shardedJedis = redisService.getRedisClient();
 168         if (shardedJedis == null) {
 169             return result;
 170         }
 171         boolean flag = false;
 172         try {
 173             result = shardedJedis.ttl(key);
 174 
 175         } catch (Exception e) {
 176             log.error(e.getMessage(), e);
 177             flag = true;
 178         } finally {
 179             redisService.returnResource(shardedJedis, flag);
 180         }
 181         return result;
 182     }
 183 
 184     public boolean setbit(String key, long offset, boolean value) {
 185 
 186         ShardedJedis shardedJedis = redisService.getRedisClient();
 187         boolean result = false;
 188         if (shardedJedis == null) {
 189             return result;
 190         }
 191         boolean flag = false;
 192         try {
 193             result = shardedJedis.setbit(key, offset, value);
 194         } catch (Exception e) {
 195             log.error(e.getMessage(), e);
 196             flag = true;
 197         } finally {
 198             redisService.returnResource(shardedJedis, flag);
 199         }
 200         return result;
 201     }
 202 
 203     public boolean getbit(String key, long offset) {
 204         ShardedJedis shardedJedis = redisService.getRedisClient();
 205         boolean result = false;
 206         if (shardedJedis == null) {
 207             return result;
 208         }
 209         boolean flag = false;
 210 
 211         try {
 212             result = shardedJedis.getbit(key, offset);
 213         } catch (Exception e) {
 214             log.error(e.getMessage(), e);
 215             flag = true;
 216         } finally {
 217             redisService.returnResource(shardedJedis, flag);
 218         }
 219         return result;
 220     }
 221 
 222     public long setrange(String key, long offset, String value) {
 223         ShardedJedis shardedJedis = redisService.getRedisClient();
 224         long result = 0;
 225         if (shardedJedis == null) {
 226             return result;
 227         }
 228         boolean flag = false;
 229         try {
 230             result = shardedJedis.setrange(key, offset, value);
 231         } catch (Exception e) {
 232             log.error(e.getMessage(), e);
 233             flag = true;
 234         } finally {
 235             redisService.returnResource(shardedJedis, flag);
 236         }
 237         return result;
 238     }
 239 
 240     public String getrange(String key, long startOffset, long endOffset) {
 241         ShardedJedis shardedJedis = redisService.getRedisClient();
 242         String result = null;
 243         if (shardedJedis == null) {
 244             return result;
 245         }
 246         boolean flag = false;
 247         try {
 248             result = shardedJedis.getrange(key, startOffset, endOffset);
 249 
 250         } catch (Exception e) {
 251             log.error(e.getMessage(), e);
 252             flag = true;
 253         } finally {
 254             redisService.returnResource(shardedJedis, flag);
 255         }
 256         return result;
 257     }
 258 
 259     public String getSet(String key, String value) {
 260         String result = null;
 261         ShardedJedis shardedJedis = redisService.getRedisClient();
 262         if (shardedJedis == null) {
 263             return result;
 264         }
 265         boolean flag = false;
 266         try {
 267             result = shardedJedis.getSet(key, value);
 268         } catch (Exception e) {
 269             log.error(e.getMessage(), e);
 270             flag = true;
 271         } finally {
 272             redisService.returnResource(shardedJedis, flag);
 273         }
 274         return result;
 275     }
 276 
 277     public Long setnx(String key, String value) {
 278         Long result = null;
 279         ShardedJedis shardedJedis = redisService.getRedisClient();
 280         if (shardedJedis == null) {
 281             return result;
 282         }
 283         boolean flag = false;
 284         try {
 285             result = shardedJedis.setnx(key, value);
 286         } catch (Exception e) {
 287             log.error(e.getMessage(), e);
 288             flag = true;
 289         } finally {
 290             redisService.returnResource(shardedJedis, flag);
 291         }
 292         return result;
 293     }
 294 
 295     public String setex(String key, int seconds, String value) {
 296         String result = null;
 297         ShardedJedis shardedJedis = redisService.getRedisClient();
 298         if (shardedJedis == null) {
 299             return result;
 300         }
 301         boolean flag = false;
 302         try {
 303             result = shardedJedis.setex(key, seconds, value);
 304 
 305         } catch (Exception e) {
 306             log.error(e.getMessage(), e);
 307             flag = true;
 308         } finally {
 309             redisService.returnResource(shardedJedis, flag);
 310         }
 311         return result;
 312     }
 313 
 314     public Long decrBy(String key, long integer) {
 315         Long result = null;
 316         ShardedJedis shardedJedis = redisService.getRedisClient();
 317         if (shardedJedis == null) {
 318             return result;
 319         }
 320         boolean flag = false;
 321         try {
 322             result = shardedJedis.decrBy(key, integer);
 323 
 324         } catch (Exception e) {
 325             log.error(e.getMessage(), e);
 326             flag = true;
 327         } finally {
 328             redisService.returnResource(shardedJedis, flag);
 329         }
 330         return result;
 331     }
 332 
 333     public Long decr(String key) {
 334         Long result = null;
 335         ShardedJedis shardedJedis = redisService.getRedisClient();
 336         if (shardedJedis == null) {
 337             return result;
 338         }
 339         boolean flag = false;
 340         try {
 341             result = shardedJedis.decr(key);
 342 
 343         } catch (Exception e) {
 344             log.error(e.getMessage(), e);
 345             flag = true;
 346         } finally {
 347             redisService.returnResource(shardedJedis, flag);
 348         }
 349         return result;
 350     }
 351 
 352     public Long incrBy(String key, long integer) {
 353         Long result = null;
 354         ShardedJedis shardedJedis = redisService.getRedisClient();
 355         if (shardedJedis == null) {
 356             return result;
 357         }
 358         boolean flag = false;
 359         try {
 360             result = shardedJedis.incrBy(key, integer);
 361 
 362         } catch (Exception e) {
 363             log.error(e.getMessage(), e);
 364             flag = true;
 365         } finally {
 366             redisService.returnResource(shardedJedis, flag);
 367         }
 368         return result;
 369     }
 370 
 371     public Long incr(String key) {
 372         Long result = null;
 373         ShardedJedis shardedJedis = redisService.getRedisClient();
 374         if (shardedJedis == null) {
 375             return result;
 376         }
 377         boolean flag = false;
 378         try {
 379             result = shardedJedis.incr(key);
 380 
 381         } catch (Exception e) {
 382             log.error(e.getMessage(), e);
 383             flag = true;
 384         } finally {
 385             redisService.returnResource(shardedJedis, flag);
 386         }
 387         return result;
 388     }
 389 
 390     public Long append(String key, String value) {
 391         Long result = null;
 392         ShardedJedis shardedJedis = redisService.getRedisClient();
 393         if (shardedJedis == null) {
 394             return result;
 395         }
 396         boolean flag = false;
 397         try {
 398             result = shardedJedis.append(key, value);
 399 
 400         } catch (Exception e) {
 401             log.error(e.getMessage(), e);
 402             flag = true;
 403         } finally {
 404             redisService.returnResource(shardedJedis, flag);
 405         }
 406         return result;
 407     }
 408 
 409     public String substr(String key, int start, int end) {
 410         String result = null;
 411         ShardedJedis shardedJedis = redisService.getRedisClient();
 412         if (shardedJedis == null) {
 413             return result;
 414         }
 415         boolean flag = false;
 416         try {
 417             result = shardedJedis.substr(key, start, end);
 418 
 419         } catch (Exception e) {
 420             log.error(e.getMessage(), e);
 421             flag = true;
 422         } finally {
 423             redisService.returnResource(shardedJedis, flag);
 424         }
 425         return result;
 426     }
 427 
 428     public Long hset(String key, String field, String value) {
 429         Long result = null;
 430         ShardedJedis shardedJedis = redisService.getRedisClient();
 431         if (shardedJedis == null) {
 432             return result;
 433         }
 434         boolean flag = false;
 435         try {
 436             result = shardedJedis.hset(key, field, value);
 437 
 438         } catch (Exception e) {
 439             log.error(e.getMessage(), e);
 440             flag = true;
 441         } finally {
 442             redisService.returnResource(shardedJedis, flag);
 443         }
 444         return result;
 445     }
 446 
 447     public String hget(String key, String field) {
 448         String result = null;
 449         ShardedJedis shardedJedis = redisService.getRedisClient();
 450         if (shardedJedis == null) {
 451             return result;
 452         }
 453         boolean flag = false;
 454         try {
 455             result = shardedJedis.hget(key, field);
 456 
 457         } catch (Exception e) {
 458             log.error(e.getMessage(), e);
 459             flag = true;
 460         } finally {
 461             redisService.returnResource(shardedJedis, flag);
 462         }
 463         return result;
 464     }
 465 
 466     public Long hsetnx(String key, String field, String value) {
 467         Long result = null;
 468         ShardedJedis shardedJedis = redisService.getRedisClient();
 469         if (shardedJedis == null) {
 470             return result;
 471         }
 472         boolean flag = false;
 473         try {
 474             result = shardedJedis.hsetnx(key, field, value);
 475 
 476         } catch (Exception e) {
 477             log.error(e.getMessage(), e);
 478             flag = true;
 479         } finally {
 480             redisService.returnResource(shardedJedis, flag);
 481         }
 482         return result;
 483     }
 484 
 485     public String hmset(String key, Map<String, String> hash) {
 486         String result = null;
 487         ShardedJedis shardedJedis = redisService.getRedisClient();
 488         if (shardedJedis == null) {
 489             return result;
 490         }
 491         boolean flag = false;
 492         try {
 493             result = shardedJedis.hmset(key, hash);
 494 
 495         } catch (Exception e) {
 496             log.error(e.getMessage(), e);
 497             flag = true;
 498         } finally {
 499             redisService.returnResource(shardedJedis, flag);
 500         }
 501         return result;
 502     }
 503 
 504     public List<String> hmget(String key, String... fields) {
 505         List<String> result = null;
 506         ShardedJedis shardedJedis = redisService.getRedisClient();
 507         if (shardedJedis == null) {
 508             return result;
 509         }
 510         boolean flag = false;
 511         try {
 512             result = shardedJedis.hmget(key, fields);
 513 
 514         } catch (Exception e) {
 515             log.error(e.getMessage(), e);
 516             flag = true;
 517         } finally {
 518             redisService.returnResource(shardedJedis, flag);
 519         }
 520         return result;
 521     }
 522 
 523     public Long hincrBy(String key, String field, long value) {
 524         Long result = null;
 525         ShardedJedis shardedJedis = redisService.getRedisClient();
 526         if (shardedJedis == null) {
 527             return result;
 528         }
 529         boolean flag = false;
 530         try {
 531             result = shardedJedis.hincrBy(key, field, value);
 532 
 533         } catch (Exception e) {
 534             log.error(e.getMessage(), e);
 535             flag = true;
 536         } finally {
 537             redisService.returnResource(shardedJedis, flag);
 538         }
 539         return result;
 540     }
 541 
 542     public Boolean hexists(String key, String field) {
 543         Boolean result = false;
 544         ShardedJedis shardedJedis = redisService.getRedisClient();
 545         if (shardedJedis == null) {
 546             return result;
 547         }
 548         boolean flag = false;
 549         try {
 550             result = shardedJedis.hexists(key, field);
 551 
 552         } catch (Exception e) {
 553             log.error(e.getMessage(), e);
 554             flag = true;
 555         } finally {
 556             redisService.returnResource(shardedJedis, flag);
 557         }
 558         return result;
 559     }
 560 
 561     public Long del(String key) {
 562         Long result = null;
 563         ShardedJedis shardedJedis = redisService.getRedisClient();
 564         if (shardedJedis == null) {
 565             return result;
 566         }
 567         boolean flag = false;
 568         try {
 569             result = shardedJedis.del(key);
 570 
 571         } catch (Exception e) {
 572             log.error(e.getMessage(), e);
 573             flag = true;
 574         } finally {
 575             redisService.returnResource(shardedJedis, flag);
 576         }
 577         return result;
 578     }
 579 
 580     public Long hdel(String key, String field) {
 581         Long result = null;
 582         ShardedJedis shardedJedis = redisService.getRedisClient();
 583         if (shardedJedis == null) {
 584             return result;
 585         }
 586         boolean flag = false;
 587         try {
 588             result = shardedJedis.hdel(key, field);
 589 
 590         } catch (Exception e) {
 591             log.error(e.getMessage(), e);
 592             flag = true;
 593         } finally {
 594             redisService.returnResource(shardedJedis, flag);
 595         }
 596         return result;
 597     }
 598 
 599     public Long hlen(String key) {
 600         Long result = null;
 601         ShardedJedis shardedJedis = redisService.getRedisClient();
 602         if (shardedJedis == null) {
 603             return result;
 604         }
 605         boolean flag = false;
 606         try {
 607             result = shardedJedis.hlen(key);
 608 
 609         } catch (Exception e) {
 610             log.error(e.getMessage(), e);
 611             flag = true;
 612         } finally {
 613             redisService.returnResource(shardedJedis, flag);
 614         }
 615         return result;
 616     }
 617 
 618     public Set<String> hkeys(String key) {
 619         Set<String> result = null;
 620         ShardedJedis shardedJedis = redisService.getRedisClient();
 621         if (shardedJedis == null) {
 622             return result;
 623         }
 624         boolean flag = false;
 625         try {
 626             result = shardedJedis.hkeys(key);
 627 
 628         } catch (Exception e) {
 629             log.error(e.getMessage(), e);
 630             flag = true;
 631         } finally {
 632             redisService.returnResource(shardedJedis, flag);
 633         }
 634         return result;
 635     }
 636 
 637     public List<String> hvals(String key) {
 638         List<String> result = null;
 639         ShardedJedis shardedJedis = redisService.getRedisClient();
 640         if (shardedJedis == null) {
 641             return result;
 642         }
 643         boolean flag = false;
 644         try {
 645             result = shardedJedis.hvals(key);
 646 
 647         } catch (Exception e) {
 648             log.error(e.getMessage(), e);
 649             flag = true;
 650         } finally {
 651             redisService.returnResource(shardedJedis, flag);
 652         }
 653         return result;
 654     }
 655 
 656     public Map<String, String> hgetAll(String key) {
 657         Map<String, String> result = null;
 658         ShardedJedis shardedJedis = redisService.getRedisClient();
 659         if (shardedJedis == null) {
 660             return result;
 661         }
 662         boolean flag = false;
 663         try {
 664             result = shardedJedis.hgetAll(key);
 665 
 666         } catch (Exception e) {
 667             log.error(e.getMessage(), e);
 668             flag = true;
 669         } finally {
 670             redisService.returnResource(shardedJedis, flag);
 671         }
 672         return result;
 673     }
 674 
 675     /**
 676      * 在redis list尾部增加一個String
 677      * */
 678     public Long rpush(String key, String string) {
 679         Long result = null;
 680         ShardedJedis shardedJedis = redisService.getRedisClient();
 681         if (shardedJedis == null) {
 682             return result;
 683         }
 684         boolean flag = false;
 685         try {
 686             result = shardedJedis.rpush(key, string);
 687 
 688         } catch (Exception e) {
 689             log.error(e.getMessage(), e);
 690             flag = true;
 691         } finally {
 692             redisService.returnResource(shardedJedis, flag);
 693         }
 694         return result;
 695     }
 696 
 697     /**
 698      * 在redis list頭部增加一個String
 699      * */
 700     public Long lpush(String key, String string) {
 701         Long result = null;
 702         ShardedJedis shardedJedis = redisService.getRedisClient();
 703         if (shardedJedis == null) {
 704             return result;
 705         }
 706         boolean flag = false;
 707         try {
 708             result = shardedJedis.lpush(key, string);
 709 
 710         } catch (Exception e) {
 711             log.error(e.getMessage(), e);
 712             flag = true;
 713         } finally {
 714             redisService.returnResource(shardedJedis, flag);
 715         }
 716         return result;
 717     }
 718 
 719     public Long llen(String key) {
 720         Long result = null;
 721         ShardedJedis shardedJedis = redisService.getRedisClient();
 722         if (shardedJedis == null) {
 723             return result;
 724         }
 725         boolean flag = false;
 726         try {
 727             result = shardedJedis.llen(key);
 728 
 729         } catch (Exception e) {
 730             log.error(e.getMessage(), e);
 731             flag = true;
 732         } finally {
 733             redisService.returnResource(shardedJedis, flag);
 734         }
 735         return result;
 736     }
 737 
 738     public List<String> lrange(String key, long start, long end) {
 739         List<String> result = null;
 740         ShardedJedis shardedJedis = redisService.getRedisClient();
 741         if (shardedJedis == null) {
 742             return result;
 743         }
 744         boolean flag = false;
 745         try {
 746             result = shardedJedis.lrange(key, start, end);
 747 
 748         } catch (Exception e) {
 749             log.error(e.getMessage(), e);
 750             flag = true;
 751         } finally {
 752             redisService.returnResource(shardedJedis, flag);
 753         }
 754         return result;
 755     }
 756 
 757     public String ltrim(String key, long start, long end) {
 758         String result = null;
 759         ShardedJedis shardedJedis = redisService.getRedisClient();
 760         if (shardedJedis == null) {
 761             return result;
 762         }
 763         boolean flag = false;
 764         try {
 765             result = shardedJedis.ltrim(key, start, end);
 766 
 767         } catch (Exception e) {
 768             log.error(e.getMessage(), e);
 769             flag = true;
 770         } finally {
 771             redisService.returnResource(shardedJedis, flag);
 772         }
 773         return result;
 774     }
 775 
 776     public String lIndex(String key, long index) {
 777         String result = null;
 778         ShardedJedis shardedJedis = redisService.getRedisClient();
 779         if (shardedJedis == null) {
 780             return result;
 781         }
 782         boolean flag = false;
 783         try {
 784             result = shardedJedis.lindex(key, index);
 785 
 786         } catch (Exception e) {
 787             log.error(e.getMessage(), e);
 788             flag = true;
 789         } finally {
 790             redisService.returnResource(shardedJedis, flag);
 791         }
 792         return result;
 793     }
 794 
 795     public String lset(String key, long index, String value) {
 796         String result = null;
 797         ShardedJedis shardedJedis = redisService.getRedisClient();
 798         if (shardedJedis == null) {
 799             return result;
 800         }
 801         boolean flag = false;
 802         try {
 803             result = shardedJedis.lset(key, index, value);
 804 
 805         } catch (Exception e) {
 806             log.error(e.getMessage(), e);
 807             flag = true;
 808         } finally {
 809             redisService.returnResource(shardedJedis, flag);
 810         }
 811         return result;
 812     }
 813 
 814     public Long lrem(String key, long count, String value) {
 815         Long result = null;
 816         ShardedJedis shardedJedis = redisService.getRedisClient();
 817         if (shardedJedis == null) {
 818             return result;
 819         }
 820         boolean flag = false;
 821         try {
 822             result = shardedJedis.lrem(key, count, value);
 823 
 824         } catch (Exception e) {
 825             log.error(e.getMessage(), e);
 826             flag = true;
 827         } finally {
 828             redisService.returnResource(shardedJedis, flag);
 829         }
 830         return result;
 831     }
 832 
 833     /**
 834      * 從redis list頭部取出一個key
 835      * */
 836     public String lpop(String key) {
 837         String result = null;
 838         ShardedJedis shardedJedis = redisService.getRedisClient();
 839         if (shardedJedis == null) {
 840             return result;
 841         }
 842         boolean flag = false;
 843         try {
 844             result = shardedJedis.lpop(key);
 845 
 846         } catch (Exception e) {
 847             log.error(e.getMessage(), e);
 848             flag = true;
 849         } finally {
 850             redisService.returnResource(shardedJedis, flag);
 851         }
 852         return result;
 853     }
 854 
 855     /**
 856      * 從redis list尾部取出一個key
 857      * */
 858     public String rpop(String key) {
 859         String result = null;
 860         ShardedJedis shardedJedis = redisService.getRedisClient();
 861         if (shardedJedis == null) {
 862             return result;
 863         }
 864         boolean flag = false;
 865         try {
 866             result = shardedJedis.rpop(key);
 867 
 868         } catch (Exception e) {
 869             log.error(e.getMessage(), e);
 870             flag = true;
 871         } finally {
 872             redisService.returnResource(shardedJedis, flag);
 873         }
 874         return result;
 875     }
 876 
 877     public Long sadd(String key, String member) {
 878         Long result = null;
 879         ShardedJedis shardedJedis = redisService.getRedisClient();
 880         if (shardedJedis == null) {
 881             return result;
 882         }
 883         boolean flag = false;
 884         try {
 885             result = shardedJedis.sadd(key, member);
 886 
 887         } catch (Exception e) {
 888             log.error(e.getMessage(), e);
 889             flag = true;
 890         } finally {
 891             redisService.returnResource(shardedJedis, flag);
 892         }
 893         return result;
 894     }
 895 
 896     public Set<String> smembers(String key) {
 897         Set<String> result = null;
 898         ShardedJedis shardedJedis = redisService.getRedisClient();
 899         if (shardedJedis == null) {
 900             return result;
 901         }
 902         boolean flag = false;
 903         try {
 904             result = shardedJedis.smembers(key);
 905 
 906         } catch (Exception e) {
 907             log.error(e.getMessage(), e);
 908             flag = true;
 909         } finally {
 910             redisService.returnResource(shardedJedis, flag);
 911         }
 912         return result;
 913     }
 914 
 915     public Long srem(String key, String member) {
 916         ShardedJedis shardedJedis = redisService.getRedisClient();
 917 
 918         Long result = null;
 919         if (shardedJedis == null) {
 920             return result;
 921         }
 922         boolean flag = false;
 923         try {
 924             result = shardedJedis.srem(key, member);
 925         } catch (Exception e) {
 926             log.error(e.getMessage(), e);
 927             flag = true;
 928         } finally {
 929             redisService.returnResource(shardedJedis, flag);
 930         }
 931         return result;
 932     }
 933 
 934     public String spop(String key) {
 935         ShardedJedis shardedJedis = redisService.getRedisClient();
 936         String result = null;
 937         if (shardedJedis == null) {
 938             return result;
 939         }
 940         boolean flag = false;
 941         try {
 942             result = shardedJedis.spop(key);
 943         } catch (Exception e) {
 944             log.error(e.getMessage(), e);
 945             flag = true;
 946         } finally {
 947             redisService.returnResource(shardedJedis, flag);
 948         }
 949         return result;
 950     }
 951 
 952     public Long scard(String key) {
 953         ShardedJedis shardedJedis = redisService.getRedisClient();
 954         Long result = null;
 955         if (shardedJedis == null) {
 956             return result;
 957         }
 958         boolean flag = false;
 959         try {
 960             result = shardedJedis.scard(key);
 961 
 962         } catch (Exception e) {
 963             log.error(e.getMessage(), e);
 964             flag = true;
 965         } finally {
 966             redisService.returnResource(shardedJedis, flag);
 967         }
 968         return result;
 969     }
 970 
 971     public Boolean sismember(String key, String member) {
 972         ShardedJedis shardedJedis = redisService.getRedisClient();
 973         Boolean result = null;
 974         if (shardedJedis == null) {
 975             return result;
 976         }
 977         boolean flag = false;
 978         try {
 979             result = shardedJedis.sismember(key, member);
 980         } catch (Exception e) {
 981             log.error(e.getMessage(), e);
 982             flag = true;
 983         } finally {
 984             redisService.returnResource(shardedJedis, flag);
 985         }
 986         return result;
 987     }
 988 
 989     public String srandmember(String key) {
 990         ShardedJedis shardedJedis = redisService.getRedisClient();
 991         String result = null;
 992         if (shardedJedis == null) {
 993             return result;
 994         }
 995         boolean flag = false;
 996         try {
 997             result = shardedJedis.srandmember(key);
 998         } catch (Exception e) {
 999             log.error(e.getMessage(), e);
1000             flag = true;
1001         } finally {
1002             redisService.returnResource(shardedJedis, flag);
1003         }
1004         return result;
1005     }
1006 
1007     public Long zadd(String key, double score, String member) {
1008         Long result = null;
1009         ShardedJedis shardedJedis = redisService.getRedisClient();
1010         if (shardedJedis == null) {
1011             return result;
1012         }
1013         boolean flag = false;
1014         try {
1015             result = shardedJedis.zadd(key, score, member);
1016         } catch (Exception e) {
1017             log.error(e.getMessage(), e);
1018             flag = true;
1019         } finally {
1020             redisService.returnResource(shardedJedis, flag);
1021         }
1022         return result;
1023     }
1024 
1025     public Set<String> zrange(String key, int start, int end) {
1026         Set<String> result = null;
1027         ShardedJedis shardedJedis = redisService.getRedisClient();
1028         if (shardedJedis == null) {
1029             return result;
1030         }
1031         boolean flag = false;
1032         try {
1033             result = shardedJedis.zrange(key, start, end);
1034         } catch (Exception e) {
1035             log.error(e.getMessage(), e);
1036             flag = true;
1037         } finally {
1038             redisService.returnResource(shardedJedis, flag);
1039         }
1040         return result;
1041     }
1042 
1043     public Long zrem(String key, String member) {
1044         Long result = null;
1045         ShardedJedis shardedJedis = redisService.getRedisClient();
1046         if (shardedJedis == null) {
1047             return result;
1048         }
1049         boolean flag = false;
1050         try {
1051             result = shardedJedis.zrem(key, member);
1052         } catch (Exception e) {
1053             log.error(e.getMessage(), e);
1054             flag = true;
1055         } finally {
1056             redisService.returnResource(shardedJedis, flag);
1057         }
1058         return result;
1059     }
1060 
1061     public Double zincrby(String key, double score, String member) {
1062         Double result = null;
1063         ShardedJedis shardedJedis = redisService.getRedisClient();
1064         if (shardedJedis == null) {
1065             return result;
1066         }
1067         boolean flag = false;
1068         try {
1069 
1070             result = shardedJedis.zincrby(key, score, member);
1071 
1072         } catch (Exception e) {
1073             log.error(e.getMessage(), e);
1074             flag = true;
1075         } finally {
1076             redisService.returnResource(shardedJedis, flag);
1077         }
1078         return result;
1079     }
1080 
1081     public Long zrank(String key, String member) {
1082         Long result = null;
1083         ShardedJedis shardedJedis = redisService.getRedisClient();
1084         if (shardedJedis == null) {
1085             return result;
1086         }
1087         boolean flag = false;
1088         try {
1089 
1090             result = shardedJedis.zrank(key, member);
1091 
1092         } catch (Exception e) {
1093             log.error(e.getMessage(), e);
1094             flag = true;
1095         } finally {
1096             redisService.returnResource(shardedJedis, flag);
1097         }
1098         return result;
1099     }
1100 
1101     public Long zrevrank(String key, String member) {
1102         Long result = null;
1103         ShardedJedis shardedJedis = redisService.getRedisClient();
1104         if (shardedJedis == null) {
1105             return result;
1106         }
1107         boolean flag = false;
1108         try {
1109 
1110             result = shardedJedis.zrevrank(key, member);
1111 
1112         } catch (Exception e) {
1113             log.error(e.getMessage(), e);
1114             flag = true;
1115         } finally {
1116             redisService.returnResource(shardedJedis, flag);
1117         }
1118         return result;
1119     }
1120 
1121     public Set<String> zrevrange(String key, int start, int end) {
1122         Set<String> result = null;
1123         ShardedJedis shardedJedis = redisService.getRedisClient();
1124         if (shardedJedis == null) {
1125             return result;
1126         }
1127         boolean flag = false;
1128         try {
1129 
1130             result = shardedJedis.zrevrange(key, start, end);
1131 
1132         } catch (Exception e) {
1133             log.error(e.getMessage(), e);
1134             flag = true;
1135         } finally {
1136             redisService.returnResource(shardedJedis, flag);
1137         }
1138         return result;
1139     }
1140 
1141     public Set<Tuple> zrangeWithScores(String key, int start, int end) {
1142         Set<Tuple> result = null;
1143         ShardedJedis shardedJedis = redisService.getRedisClient();
1144         if (shardedJedis == null) {
1145             return result;
1146         }
1147         boolean flag = false;
1148         try {
1149 
1150             result = shardedJedis.zrangeWithScores(key, start, end);
1151 
1152         } catch (Exception e) {
1153             log.error(e.getMessage(), e);
1154             flag = true;
1155         } finally {
1156             redisService.returnResource(shardedJedis, flag);
1157         }
1158         return result;
1159     }
1160 
1161     public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
1162         Set<Tuple> result = null;
1163         ShardedJedis shardedJedis = redisService.getRedisClient();
1164         if (shardedJedis == null) {
1165             return result;
1166         }
1167         boolean flag = false;
1168         try {
1169 
1170             result = shardedJedis.zrevrangeWithScores(key, start, end);
1171 
1172         } catch (Exception e) {
1173             log.error(e.getMessage(), e);
1174             flag = true;
1175         } finally {
1176             redisService.returnResource(shardedJedis, flag);
1177         }
1178         return result;
1179     }
1180 
1181     public Long zcard(String key) {
1182         Long result = null;
1183         ShardedJedis shardedJedis = redisService.getRedisClient();
1184         if (shardedJedis == null) {
1185             return result;
1186         }
1187         boolean flag = false;
1188         try {
1189 
1190             result = shardedJedis.zcard(key);
1191 
1192         } catch (Exception e) {
1193             log.error(e.getMessage(), e);
1194             flag = true;
1195         } finally {
1196             redisService.returnResource(shardedJedis, flag);
1197         }
1198         return result;
1199     }
1200 
1201     public Double zscore(String key, String member) {
1202         Double result = null;
1203         ShardedJedis shardedJedis = redisService.getRedisClient();
1204         if (shardedJedis == null) {
1205             return result;
1206         }
1207         boolean flag = false;
1208         try {
1209 
1210             result = shardedJedis.zscore(key, member);
1211 
1212         } catch (Exception e) {
1213             log.error(e.getMessage(), e);
1214             flag = true;
1215         } finally {
1216             redisService.returnResource(shardedJedis, flag);
1217         }
1218         return result;
1219     }
1220 
1221     public List<String> sort(String key) {
1222         List<String> result = null;
1223         ShardedJedis shardedJedis = redisService.getRedisClient();
1224         if (shardedJedis == null) {
1225             return result;
1226         }
1227         boolean flag = false;
1228         try {
1229 
1230             result = shardedJedis.sort(key);
1231 
1232         } catch (Exception e) {
1233             log.error(e.getMessage(), e);
1234             flag = true;
1235         } finally {
1236             redisService.returnResource(shardedJedis, flag);
1237         }
1238         return result;
1239     }
1240 
1241     public List<String> sort(String key, SortingParams sortingParameters) {
1242         List<String> result = null;
1243         ShardedJedis shardedJedis = redisService.getRedisClient();
1244         if (shardedJedis == null) {
1245             return result;
1246         }
1247         boolean flag = false;
1248         try {
1249 
1250             result = shardedJedis.sort(key, sortingParameters);
1251 
1252         } catch (Exception e) {
1253             log.error(e.getMessage(), e);
1254             flag = true;
1255         } finally {
1256             redisService.returnResource(shardedJedis, flag);
1257         }
1258         return result;
1259     }
1260 
1261     public Long zcount(String key, double min, double max) {
1262         Long result = null;
1263         ShardedJedis shardedJedis = redisService.getRedisClient();
1264         if (shardedJedis == null) {
1265             return result;
1266         }
1267         boolean flag = false;
1268         try {
1269 
1270             result = shardedJedis.zcount(key, min, max);
1271 
1272         } catch (Exception e) {
1273             log.error(e.getMessage(), e);
1274             flag = true;
1275         } finally {
1276             redisService.returnResource(shardedJedis, flag);
1277         }
1278         return result;
1279     }
1280 
1281     public Set<String> zrangeByScore(String key, double min, double max) {
1282         Set<String> result = null;
1283         ShardedJedis shardedJedis = redisService.getRedisClient();
1284         if (shardedJedis == null) {
1285             return result;
1286         }
1287         boolean flag = false;
1288         try {
1289 
1290             result = shardedJedis.zrangeByScore(key, min, max);
1291 
1292         } catch (Exception e) {
1293             log.error(e.getMessage(), e);
1294             flag = true;
1295         } finally {
1296             redisService.returnResource(shardedJedis, flag);
1297         }
1298         return result;
1299     }
1300 
1301     public Set<String> zrevrangeByScore(String key, double max, double min) {
1302         Set<String> result = null;
1303         ShardedJedis shardedJedis = redisService.getRedisClient();
1304         if (shardedJedis == null) {
1305             return result;
1306         }
1307         boolean flag = false;
1308         try {
1309 
1310             result = shardedJedis.zrevrangeByScore(key, max, min);
1311 
1312         } catch (Exception e) {
1313             log.error(e.getMessage(), e);
1314             flag = true;
1315         } finally {
1316             redisService.returnResource(shardedJedis, flag);
1317         }
1318         return result;
1319     }
1320 
1321     public Set<String> zrangeByScore(String key, double min, double max,
1322             int offset, int count) {
1323         Set<String> result = null;
1324         ShardedJedis shardedJedis = redisService.getRedisClient();
1325         if (shardedJedis == null) {
1326             return result;
1327         }
1328         boolean flag = false;
1329         try {
1330 
1331             result = shardedJedis.zrangeByScore(key, min, max, offset, count);
1332 
1333         } catch (Exception e) {
1334             log.error(e.getMessage(), e);
1335             flag = true;
1336         } finally {
1337             redisService.returnResource(shardedJedis, flag);
1338         }
1339         return result;
1340     }
1341 
1342     public Set<String> zrevrangeByScore(String key, double max, double min,
1343             int offset, int count) {
1344         Set<String> result = null;
1345         ShardedJedis shardedJedis = redisService.getRedisClient();
1346         if (shardedJedis == null) {
1347             return result;
1348         }
1349         boolean flag = false;
1350         try {
1351 
1352             result = shardedJedis
1353                     .zrevrangeByScore(key, max, min, offset, count);
1354 
1355         } catch (Exception e) {
1356             log.error(e.getMessage(), e);
1357             flag = true;
1358         } finally {
1359             redisService.returnResource(shardedJedis, flag);
1360         }
1361         return result;
1362     }
1363 
1364     public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
1365         Set<Tuple> result = null;
1366         ShardedJedis shardedJedis = redisService.getRedisClient();
1367         if (shardedJedis == null) {
1368             return result;
1369         }
1370         boolean flag = false;
1371         try {
1372 
1373             result = shardedJedis.zrangeByScoreWithScores(key, min, max);
1374 
1375         } catch (Exception e) {
1376             log.error(e.getMessage(), e);
1377             flag = true;
1378         } finally {
1379             redisService.returnResource(shardedJedis, flag);
1380         }
1381         return result;
1382     }
1383 
1384     public Set<Tuple> zrevrangeByScoreWithScores(String key, double max,
1385             double min) {
1386         Set<Tuple> result = null;
1387         ShardedJedis shardedJedis = redisService.getRedisClient();
1388         if (shardedJedis == null) {
1389             return result;
1390         }
1391         boolean flag = false;
1392         try {
1393 
1394             result = shardedJedis.zrevrangeByScoreWithScores(key, max, min);
1395 
1396         } catch (Exception e) {
1397             log.error(e.getMessage(), e);
1398             flag = true;
1399         } finally {
1400             redisService.returnResource(shardedJedis, flag);
1401         }
1402         return result;
1403     }
1404 
1405     public Set<Tuple> zrangeByScoreWithScores(String key, double min,
1406             double max, int offset, int count) {
1407         Set<Tuple> result = null;
1408         ShardedJedis shardedJedis = redisService.getRedisClient();
1409         if (shardedJedis == null) {
1410             return result;
1411         }
1412         boolean flag = false;
1413         try {
1414 
1415             result = shardedJedis.zrangeByScoreWithScores(key, min, max,
1416                     offset, count);
1417 
1418         } catch (Exception e) {
1419             log.error(e.getMessage(), e);
1420             flag = true;
1421         } finally {
1422             redisService.returnResource(shardedJedis, flag);
1423         }
1424         return result;
1425     }
1426 
1427     public Set<Tuple> zrevrangeByScoreWithScores(String key, double max,
1428             double min, int offset, int count) {
1429         Set<Tuple> result = null;
1430         ShardedJedis shardedJedis = redisService.getRedisClient();
1431         if (shardedJedis == null) {
1432             return result;
1433         }
1434         boolean flag = false;
1435         try {
1436 
1437             result = shardedJedis.zrevrangeByScoreWithScores(key, max, min,
1438                     offset, count);
1439 
1440         } catch (Exception e) {
1441             log.error(e.getMessage(), e);
1442             flag = true;
1443         } finally {
1444             redisService.returnResource(shardedJedis, flag);
1445         }
1446         return result;
1447     }
1448 
1449     public Long zremrangeByRank(String key, int start, int end) {
1450         Long result = null;
1451         ShardedJedis shardedJedis = redisService.getRedisClient();
1452         if (shardedJedis == null) {
1453             return result;
1454         }
1455         boolean flag = false;
1456         try {
1457 
1458             result = shardedJedis.zremrangeByRank(key, start, end);
1459 
1460         } catch (Exception e) {
1461             log.error(e.getMessage(), e);
1462             flag = true;
1463         } finally {
1464             redisService.returnResource(shardedJedis, flag);
1465         }
1466         return result;
1467     }
1468 
1469     public Long zremrangeByScore(String key, double start, double end) {
1470         Long result = null;
1471         ShardedJedis shardedJedis = redisService.getRedisClient();
1472         if (shardedJedis == null) {
1473             return result;
1474         }
1475         boolean flag = false;
1476         try {
1477 
1478             result = shardedJedis.zremrangeByScore(key, start, end);
1479 
1480         } catch (Exception e) {
1481             log.error(e.getMessage(), e);
1482             flag = true;
1483         } finally {
1484             redisService.returnResource(shardedJedis, flag);
1485         }
1486         return result;
1487     }
1488 
1489     public Long linsert(String key, LIST_POSITION where, String pivot,
1490             String value) {
1491         Long result = null;
1492         ShardedJedis shardedJedis = redisService.getRedisClient();
1493         if (shardedJedis == null) {
1494             return result;
1495         }
1496         boolean flag = false;
1497         try {
1498 
1499             result = shardedJedis.linsert(key, where, pivot, value);
1500 
1501         } catch (Exception e) {
1502             log.error(e.getMessage(), e);
1503             flag = true;
1504         } finally {
1505             redisService.returnResource(shardedJedis, flag);
1506         }
1507         return result;
1508     }
1509 
1510     
1511     @SuppressWarnings("deprecation")
1512     public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) {
1513         ShardedJedis shardedJedis = redisService.getRedisClient();
1514         List<Object> result = null;
1515         if (shardedJedis == null) {
1516             return result;
1517         }
1518         boolean flag = false;
1519         try {
1520             result = shardedJedis.pipelined(shardedJedisPipeline);
1521         } catch (Exception e) {
1522             log.error(e.getMessage(), e);
1523             flag = true;
1524         } finally {
1525             redisService.returnResource(shardedJedis, flag);
1526         }
1527         return result;
1528     }
1529 
1530     public Jedis getShard(String key) {
1531         ShardedJedis shardedJedis = redisService.getRedisClient();
1532         Jedis result = null;
1533         if (shardedJedis == null) {
1534             return result;
1535         }
1536         boolean flag = false;
1537         try {
1538             result = shardedJedis.getShard(key);
1539         } catch (Exception e) {
1540             log.error(e.getMessage(), e);
1541             flag = true;
1542         } finally {
1543             redisService.returnResource(shardedJedis, flag);
1544         }
1545         return result;
1546     }
1547 
1548     public JedisShardInfo getShardInfo(String key) {
1549         ShardedJedis shardedJedis = redisService.getRedisClient();
1550         JedisShardInfo result = null;
1551         if (shardedJedis == null) {
1552             return result;
1553         }
1554         boolean flag = false;
1555         try {
1556             result = shardedJedis.getShardInfo(key);
1557         } catch (Exception e) {
1558             log.error(e.getMessage(), e);
1559             flag = true;
1560         } finally {
1561             redisService.returnResource(shardedJedis, flag);
1562         }
1563         return result;
1564     }
1565 
1566     public String getKeyTag(String key) {
1567         ShardedJedis shardedJedis = redisService.getRedisClient();
1568         String result = null;
1569         if (shardedJedis == null) {
1570             return result;
1571         }
1572         boolean flag = false;
1573         try {
1574             result = shardedJedis.getKeyTag(key);
1575         } catch (Exception e) {
1576             log.error(e.getMessage(), e);
1577             flag = true;
1578         } finally {
1579             redisService.returnResource(shardedJedis, flag);
1580         }
1581         return result;
1582     }
1583 
1584     public Collection<JedisShardInfo> getAllShardInfo() {
1585         ShardedJedis shardedJedis = redisService.getRedisClient();
1586         Collection<JedisShardInfo> result = null;
1587         if (shardedJedis == null) {
1588             return result;
1589         }
1590         boolean flag = false;
1591         try {
1592             result = shardedJedis.getAllShardInfo();
1593 
1594         } catch (Exception e) {
1595             log.error(e.getMessage(), e);
1596             flag = true;
1597         } finally {
1598             redisService.returnResource(shardedJedis, flag);
1599         }
1600         return result;
1601     }
1602 
1603     public Collection<Jedis> getAllShards() {
1604         ShardedJedis shardedJedis = redisService.getRedisClient();
1605         Collection<Jedis> result = null;
1606         if (shardedJedis == null) {
1607             return result;
1608         }
1609         boolean flag = false;
1610         try {
1611             result = shardedJedis.getAllShards();
1612 
1613         } catch (Exception e) {
1614             log.error(e.getMessage(), e);
1615             flag = true;
1616         } finally {
1617             redisService.returnResource(shardedJedis, flag);
1618         }
1619         return result;
1620     }
1621 
1622 }
View Code

  使用的時候,直接調用此類中對應的方法即可。

  Redis的內部實現是C寫的,有必要好好看看里面究竟是怎么實現的。

6 參考

(1)Redis和MongoDB比較:

http://taotao1240.blog.51cto.com/731446/755173

(2)Redis官方下載地址:

http://redis.cn/download.html

(3) 按鍵通知事件

http://redis.mjplay.com.cn/topic/notification.html

(4)Redis內部實現

http://www.redisbook.com/en/latest/

(5)Redis持久化

http://blog.nosqlfan.com/html/3813.html

(6)Redis的AOF詳細解讀

http://www.wzxue.com/redis%E6%A0%B8%E5%BF%83%E8%A7%A3%E8%AF%BB-aof%E4%B8%8Erewrite%E6%9C%BA%E5%88%B6/

(7)Redis失效機制解析

http://blog.nosqlfan.com/html/4218.html

(8)Redis命令列表

http://redis.cn/commands.html

(9)Redis應用場景

http://www.coderli.com/redis-application-scenarios

(10)Jedis下載地址:

https://github.com/xetorthio/jedis

 

-------------------------------------------------------------------------------

如果您看了本篇博客,覺得對您有所收獲,請點擊右下角的 [推薦]

如果您想轉載本博客,請注明出處

如果您對本文有意見或者建議,歡迎留言

感謝您的閱讀,請關注我的后續博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM