【Redis学习】Redis的安装、管理、适用场合以及使用

分手后的思念是犯贱 2022-08-21 03:27 213阅读 0赞

1、Redis概述

我们知道,内存是电脑主板上的存储部件,用于存储当前正在使用的数据和程序,CPU可以与内存直接沟通,所以访问速速非常高;而外存数据必须加载到内存以后程序才能使用。如果把CPU当做一个吃货,那么内存是碗,而外存是锅,这个吃货再能吃,也得先把饭从锅里盛到碗里再下嘴,而不能直接跳到锅里大快朵颐。但是很多时候CPU吃的并不爽,一是因为碗不够大,没吃两嘴就没了;二是从锅里往碗里盛饭是个比较耗时的过程,等待很痛苦。正经点说,就是内存大小、I/O速度、网络响应时间等常常成为应用系统的性能瓶颈。

传统的关系型数据库如MySQL、Oracle、DB2等,数据存储在磁盘的文件系统中,增删改查需要频繁地在内存与外存之间交换数据,很费时间。

在以上背景下,小巧而功能强大的存储结构,用于在内存中管理数据量不太大但是访问量特别大的热点数据—Redis应运而生。

Redis是一个开源的使用C语言编写、开源、支持网络、可基于内存亦可持久化的日志型、高性能的Key-Value数据库,并提供多种语言的API。它通常被称为数据结构服务器,因为值(value)可以是 字符串(String)、哈希(Map)、 列表(list)、集合(sets) 和 有序集合(sorted sets)等类型

Redis是Remote Dictionary Server的简称,是一个由意大利人Salvatore Sanfilippo开发的key-value存储系统,具有极高的读写性能,读的速度可达110000次/s,写的速度可达81000次/s 。

与Redis类似的产品还有memcache,同样是一个基于内存的key-value存储系统,但是由于memcache数据结构单一,数据安全性低下等原因,大有被Redis取而代之的趋势。

Redis 与其他 key - value 缓存产品相比,有以下特点:

1、Redis支持数据的持久化,周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,重启的时候可以再次加载进行使用。

3、Redis支持数据的备份,即master-slave模式的数据备份。

5、Redis还支持 publish/subscribe, 通知, key过期等高级特性。

互联网发展到现在,仅靠传统的关系型数据库已经远不能应对各种变态的需求,一个大型的互联网应用往往需要各类数据库相互合作,才能达到高可用、高性能的标准。

这里写图片描述

比如,使用MySQL/Oracle/DB2管理核心数据,使用Memcache/Redis管理热点数据,使用Hbase/Hadoop管理海量数据……总之,在合适的地方选择合适的数据库。

2、Redis的安装和部署

以2.8.6为例进行下述的操作及说明。

2.1 Centos安装部署

安装步骤如下:

第一步:
//将redis-2.8.6.tar.gz安装包解压缩
tar -zxvf redis-2.8.17.tar.gz

第二步:
//)进入redis-2.8.6目录
cd redis-2.8.17

第三步:
//执行make命令进行编译
make

第四步:
编译结束之后,配置redis.conf文件,将“daemonize”属性设置为“yes”,表示我们会以后台进程形式启动Redis服务;将“port”属性设置为指定的端口,这里默认为“6379”;将“logfile”属性设置为指定的日志路径,其余的属性可以保持默认

第五步:
使用命令src/redis-server ./redis.conf启动Redis服务,启动之后,可以在刚才配置的日志路径中看到如下类似信息:

这里写图片描述

第六步:
执行客户端命令,验证服务是否正常:

这里写图片描述

第七步:
执行以下命令src/redis-cli shutdown,停止Redis服务。

  1. $ps -ef | grep redis
  2. root 23422 19813 0 10:59 pts/5 00:00:08 redis-server *:6379
  3. $sudo redis-cli shutdown
  4. [23422] 05 Mar 12:11:29.301 # User requested shutdown...
  5. [23422] 05 Mar 12:11:29.301 * Saving the final RDB snapshot before exiting.
  6. [23422] 05 Mar 12:11:29.314 * DB saved on disk
  7. [23422] 05 Mar 12:11:29.314 # Redis is now ready to exit, bye bye...
  8. [1]+ Done sudo redis-server (wd: ~/soft/redis-2.10.3)
  9. (wd now: ~/soft/redis-2.8.17)
  10. $ps -ef | grep redis
  11. jihite 30563 19813 0 12:11 pts/5 00:00:00 grep redis

注意:如果设置了密码后,redis-cli是无法关掉Redis服务的,必须加上ip、port、passwd:

  1. sudo redis-cli -h host -p port -a passwd shutdown
  2. //退出客户端
  3. QUIT

3、redis.conf配置文件说明

就像pom.xml是maven最重要的配置文件一样,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大文件写入对磁盘的操作次数

4、Redis管理工具

Redis安装好之后,进入安装目录的src文件夹,会发现里面有6个可执行文件:

它们对应着6个管理Redis的工具:

redis-server
redis-cli
redis-benchmark
redis-check-aof
redis-check-dump
redis-sentinel

4.1 redis-server

该工具用于启动Redis服务器,处理与客户端的对话,一个服务器可以与多个客户端连接。

在终端输入该命令,如果启动成功,就会看到Redis那幅标志性的图片.

4.2 redis-cli

该工具用于启动Redis客户端,发起与服务器的对话。可以使用参数来指定目标服务器的详细信息,例如使用参数“-h”指定服务器IP地址、“-p”指定服务器端口、“-a”指定登录密码等,如:
Redis-cli –h 192.168.1.2 –p 6300

如不指定任何参数,则默认连接127.0.0.1的6379端口。

与Redis服务器取得连接后,就通过指令进行数据的存取、更改等操作了:

  1. 127.0.0.1:6379 > set name tom
  2. OK
  3. 127.0.0.1:6379 > get name
  4. "tom"

4.3 redis-benchmark

该工具用于测试Redis在本机的性能,类似于鲁大师的跑分程序。运行之后会得到一组数据存取效率的报告:

4.4 redis-check-aof

Redis虽然是基于内存的数据库,但是会创建并更新在硬盘上的备份,备份有两种方式,一个是把内存的数据导入dump.rdb文件中,另一中就是将每个执行过的命令记录到AOF文件中。

该工具用于检查、修复AOF文件。

4.5 redis-check-dump

与Redis-check-aof类似,该工具用于检查dump.rdb文件。

4.6 redis-sentinel

该工具提供Redis实例的监控管理、通知和实例失效备援服务,是Redis集群的管理工具,监控各个其他节点的工作情况并且进行故障恢复,来提高集群的高可用性。

5、redis的适用场合

redis开创了一种新的数据存储思路,使用redis我们可以利用redis灵活多变的数据结构和数据操作来进行一些复杂的操作。

下面是redis使用的一些场景:

1、取最新N个数据的操作
比如典型的区你网站的最新评论。通过下面的方式,我们可以将最新的5000条评论的ID放在redis的List集合中,并将超出集合的部分从数据库中获取。

使用LPUSH latest.comments命令,向list集合中插入数据;
插入完成后再用LTRIM latest.comments 0 5000命令使其永远只保存最近5000个ID,然后我们在客户端获取某一页评论时可以用下面的逻辑。

  1. FUNCTION get_latest_comments(start,num_items):
  2. id_list = redis.lrange("latest.comments",start,start+num_items-1)
  3. IF id_list.length < num_items
  4. id_list=SQL_DB("SELECT … ORDER BY time LIMIT …")
  5. END
  6. RETURN id_list
  7. END

如果你还有不同的筛选维度,比如某个分类的最新N条,那么你可以再建一个按此分类的List。只存ID的话,redis是非常高效的。

2、排行榜应用,取TOP N操作
这个需求与上面需求的不同之处在于,前面操作以时间为权重,这个是以某个条件为权重。比如按顶的次数排序,这时候就需要我们的sorted set出来了。将你要排序的值设置成sorted set的score,将具体的数据设置成相应的value,每次只需要执行一条ZADD命令即可。

  1. redis 127.0.0.1:6379> ZADD myset 1 "hello"
  2. (integer) 1
  3. redis 127.0.0.1:6379> ZADD myset 1 "foo"
  4. (integer) 1
  5. redis 127.0.0.1:6379> ZADD myset 2 "world" 3 "bar"
  6. (integer) 2
  7. redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
  8. 1) "hello"
  9. 2) "1"
  10. 3) "foo"
  11. 4) "1"
  12. 5) "world"
  13. 6) "2"
  14. 7) "bar"
  15. 8) "3"

3、需要精准设定过期时间的应用
比如你可以把上面说到的sorted set的score值设置成过期时间的时间戳,那么就可以简单的通过过期时间排序,定时清除过期数据了。不仅是清除redis中的过期数据,你完全可以把redis里这个过期时间当成是对数据库中数据的索引,用redis来找出哪些数据需要过期删除,然后再精确的从数据库中删除相应的记录。

  1. redis 127.0.0.1:6379> ZADD myset 20171101 "hello"
  2. (integer) 1
  3. redis 127.0.0.1:6379> ZADD myset 20171101 "foo"
  4. (integer) 1
  5. redis 127.0.0.1:6379> ZADD myset 20171103 "world" 20171104 "bar"
  6. (integer) 2
  7. redis 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
  8. 1) "hello"
  9. 2) "20171101"
  10. 3) "foo"
  11. 4) "20171101"
  12. 5) "world"
  13. 6) "20171103"
  14. 7) "bar"
  15. 8) "20171104"

4、计数器应用
redis的命令是原子性的,你可以轻松地利用INCR,DECR命令来构建计数器系统。

  1. redis> SET page_view 20
  2. OK
  3. redis> INCR page_view
  4. (integer) 21
  5. redis> GET page_view # 数字值在 Redis 中以字符串的形式保存
  6. "21"

–对存在的数字值 key 进行 DECR

  1. redis> SET failure_times 10
  2. OK
  3. redis> DECR failure_times
  4. (integer) 9

5、Uniq操作,获取某段时间所有数据排重值
这个实用redis的set数据结构最合适了,只需要不断讲数据往set中扔就行了,set意为集合,所以会自动排重。

6、实时系统,反垃圾系统
通过上面说的set功能,你可以知道一个终端用户是否进行了某个操作,可以找到其操作的集合并进行分析统计对比等。没有做不到,只有想不到。

7、Pub/Sub构建实时消息系统
redis的Pub/Sub可以构建实时的消息系统,比如很多用Pub/Sub构建的实时聊天系统的例子。

8、构建队列系统
使用List可以构建队列系统,使用sorted set甚至可以构建有优先级的队列系统。

9、缓存
这个不必说了,性能优于memcached,数据结构也更多样化。

6、Java操作Redis

我们平时项目中使用最多的就是这个了,redis安装部署完成之后,我们怎么使用Java操作redis呢?

目前Jedis是官方推荐的比较好的Redis操作API包,我们这里结合Spring来看下如何使用Redis这一神器。

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

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

在Spring的resource目录中增加Redis的配置文件redis.properties

  1. # Redis settings
  2. redis.host=192.168.50.71
  3. redis.port=6379
  4. redis.pass=
  5. redis.timeout=0
  6. redis.maxIdle=300
  7. redis.maxActive=600
  8. redis.maxWait=1000
  9. 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. <context:property-placeholder location="classpath:redis.properties" />
  11. <context:component-scan base-package="com.scott.demo" />
  12. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
  13. <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  14. <property name="maxActive" value="50" />
  15. <property name="maxIdle" value="8" />
  16. <property name="maxWait" value="1000" />
  17. <property name="testOnBorrow" value="true"/>
  18. <property name="testOnReturn" value="true"/>
  19. <!-- <property name="testWhileIdle" value="true"/> -->
  20. </bean>
  21. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
  22. <constructor-arg index="0" ref="jedisPoolConfig" />
  23. <constructor-arg index="1">
  24. <list>
  25. <bean class="redis.clients.jedis.JedisShardInfo">
  26. <constructor-arg name="host" value="${redis.host}" />
  27. <constructor-arg name="port" value="${redis.port}" />
  28. <constructor-arg name="timeout" value="${redis.timeout}" />
  29. <constructor-arg name="weight" value="1" />
  30. </bean>
  31. </list>
  32. </constructor-arg>
  33. </bean>
  34. </beans>

获取Redis客户端对象的类:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Repository;
  5. import com.scott.demo.redis.service.RedisService;
  6. import redis.clients.jedis.ShardedJedis;
  7. import redis.clients.jedis.ShardedJedisPool;
  8. @Repository("redisService")
  9. public class RedisServiceImpl implements RedisService {
  10. private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);
  11. @Autowired
  12. private ShardedJedisPool shardedJedisPool;
  13. public ShardedJedis getRedisClient() {
  14. try {
  15. ShardedJedis shardJedis = shardedJedisPool.getResource();
  16. return shardJedis;
  17. } catch (Exception e) {
  18. log.error("getRedisClent error", e);
  19. }
  20. return null;
  21. }
  22. public void returnResource(ShardedJedis shardedJedis) {
  23. shardedJedisPool.returnResource(shardedJedis);
  24. }
  25. public void returnResource(ShardedJedis shardedJedis, boolean broken) {
  26. if (broken) {
  27. shardedJedisPool.returnBrokenResource(shardedJedis);
  28. } else {
  29. shardedJedisPool.returnResource(shardedJedis);
  30. }
  31. }
  32. }

实现redis相关操作的RedisClient类:

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

发表评论

表情:
评论列表 (有 0 条评论,213人围观)

还没有评论,来说两句吧...

相关阅读

    相关 volatile适用场合

    1对变量的写入操作不依赖其当前值 不满足:number++/count = count\5等 满足Boolean变量、记录温度变化的变量等 2.该变量没有包含在具有其他变

    相关 Redis适用场景

    Redis最适合所有数据in-momory的场景,虽然Redis也提供持久化功能,但实际更多的是一个disk-backed的功能,跟传统意义上的持久化有比较大的差别,那么可能大