当前位置: > 数据库 > Redis >

Redis安装部署学习笔记

时间:2016-05-23 17:07来源:linux.it.net.cn 作者:IT

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 }
 

  使用的时候,直接调用此类中对应的方法即可。

  Redis的内部实现是C写的,有必要好好看看里面究竟是怎么实现的。














(责任编辑:IT)
------分隔线----------------------------