目前多数的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.8.6,我们就以它为例进行下述的操作及说明。
安装步骤如下:
(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
刚才在部署的时候,提到了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大文件写入对磁盘的操作次数 |
Redis是C编写的,对外开放的List、Set、SortedSet等数据结构都有着自己独到的实现和算法,其内部的实现原理见参考4,这里就不卖弄了,我也在学习。
需求推动技术的进步,Redis可以应用在如下几种场景中:
(1)取最新N个数据操作
(2)排行榜操作,Top N
(3)需要精准设定过期时间的应用
(4)计数器应用
(5)获取某段时间内所有数据的排重值,uniq操作
(6)实时系统
(7)发布/订阅系统
(8)队列系统
(9)缓存
具体的场景分析见参考9。
使用src/redis-cli可以打开客户端对话框,输入命令即可。目前Redis支持String、Map、List、Set、SortedSets等五种数据结构以及支持订阅、事务等操作。每种命令的操作及详细解释见参考8。
目前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的相关操作:
package com.scott.demo.redis; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import com.scott.demo.redis.service.impl.RedisServiceImpl; import redis.clients.jedis.BinaryClient.LIST_POSITION; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPipeline; import redis.clients.jedis.SortingParams; import redis.clients.jedis.Tuple; /** * @author Scott * @date 2014年2月23日 * @description */ public class RedisClient { private static final Logger log = LoggerFactory.getLogger(RedisClient.class); @Autowired private RedisServiceImpl redisService; public void disconnect() { ShardedJedis shardedJedis = redisService.getRedisClient(); shardedJedis.disconnect(); } /** * 设置单个值 */ public String set(String key, String value) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.set(key, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 获取单个值 */ public String get(String key) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.get(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Boolean exists(String key) { Boolean result = false; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.exists(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String type(String key) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.type(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 在某段时间后失效 */ public Long expire(String key, int seconds) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.expire(key, seconds); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 在某个时间点失效 */ public Long expireAt(String key, long time) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.expireAt(key, time); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long ttl(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.ttl(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public boolean setbit(String key, long offset, boolean value) { ShardedJedis shardedJedis = redisService.getRedisClient(); boolean result = false; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.setbit(key, offset, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public boolean getbit(String key, long offset) { ShardedJedis shardedJedis = redisService.getRedisClient(); boolean result = false; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getbit(key, offset); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public long setrange(String key, long offset, String value) { ShardedJedis shardedJedis = redisService.getRedisClient(); long result = 0; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.setrange(key, offset, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String getrange(String key, long startOffset, long endOffset) { ShardedJedis shardedJedis = redisService.getRedisClient(); String result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getrange(key, startOffset, endOffset); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String getSet(String key, String value) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getSet(key, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long setnx(String key, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.setnx(key, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String setex(String key, int seconds, String value) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.setex(key, seconds, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long decrBy(String key, long integer) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.decrBy(key, integer); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long decr(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.decr(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long incrBy(String key, long integer) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.incrBy(key, integer); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long incr(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.incr(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long append(String key, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.append(key, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String substr(String key, int start, int end) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.substr(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long hset(String key, String field, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hset(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String hget(String key, String field) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hget(key, field); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long hsetnx(String key, String field, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hsetnx(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String hmset(String key, Map<String, String> hash) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hmset(key, hash); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public List<String> hmget(String key, String... fields) { List<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hmget(key, fields); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long hincrBy(String key, String field, long value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hincrBy(key, field, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Boolean hexists(String key, String field) { Boolean result = false; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hexists(key, field); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long del(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.del(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long hdel(String key, String field) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hdel(key, field); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long hlen(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hlen(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> hkeys(String key) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hkeys(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public List<String> hvals(String key) { List<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hvals(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Map<String, String> hgetAll(String key) { Map<String, String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.hgetAll(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 在redis list尾部增加一个String * */ public Long rpush(String key, String string) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.rpush(key, string); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 在redis list头部增加一个String * */ public Long lpush(String key, String string) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lpush(key, string); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long llen(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.llen(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public List<String> lrange(String key, long start, long end) { List<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lrange(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String ltrim(String key, long start, long end) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.ltrim(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String lIndex(String key, long index) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lindex(key, index); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String lset(String key, long index, String value) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lset(key, index, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long lrem(String key, long count, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lrem(key, count, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 从redis list头部取出一个key * */ public String lpop(String key) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.lpop(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } /** * 从redis list尾部取出一个key * */ public String rpop(String key) { String result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.rpop(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long sadd(String key, String member) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.sadd(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> smembers(String key) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.smembers(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long srem(String key, String member) { ShardedJedis shardedJedis = redisService.getRedisClient(); Long result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.srem(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String spop(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); String result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.spop(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long scard(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); Long result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.scard(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Boolean sismember(String key, String member) { ShardedJedis shardedJedis = redisService.getRedisClient(); Boolean result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.sismember(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String srandmember(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); String result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.srandmember(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zadd(String key, double score, String member) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zadd(key, score, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrange(String key, int start, int end) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrange(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zrem(String key, String member) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrem(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Double zincrby(String key, double score, String member) { Double result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zincrby(key, score, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zrank(String key, String member) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrank(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zrevrank(String key, String member) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrank(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrevrange(String key, int start, int end) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrange(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrangeWithScores(String key, int start, int end) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrangeWithScores(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrevrangeWithScores(String key, int start, int end) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrangeWithScores(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zcard(String key) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zcard(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Double zscore(String key, String member) { Double result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zscore(key, member); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public List<String> sort(String key) { List<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.sort(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public List<String> sort(String key, SortingParams sortingParameters) { List<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.sort(key, sortingParameters); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zcount(String key, double min, double max) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zcount(key, min, max); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrangeByScore(String key, double min, double max) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrangeByScore(key, min, max); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrevrangeByScore(String key, double max, double min) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrangeByScore(key, max, min); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrangeByScore(String key, double min, double max, int offset, int count) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrangeByScore(key, min, max, offset, count); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<String> zrevrangeByScore(String key, double max, double min, int offset, int count) { Set<String> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis .zrevrangeByScore(key, max, min, offset, count); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrangeByScoreWithScores(key, min, max); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrangeByScoreWithScores(key, max, min); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int count) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrangeByScoreWithScores(key, min, max, offset, count); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int count) { Set<Tuple> result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zrevrangeByScoreWithScores(key, max, min, offset, count); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zremrangeByRank(String key, int start, int end) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zremrangeByRank(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long zremrangeByScore(String key, double start, double end) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.zremrangeByScore(key, start, end); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Long linsert(String key, LIST_POSITION where, String pivot, String value) { Long result = null; ShardedJedis shardedJedis = redisService.getRedisClient(); if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.linsert(key, where, pivot, value); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } @SuppressWarnings("deprecation") public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) { ShardedJedis shardedJedis = redisService.getRedisClient(); List<Object> result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.pipelined(shardedJedisPipeline); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Jedis getShard(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); Jedis result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getShard(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public JedisShardInfo getShardInfo(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); JedisShardInfo result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getShardInfo(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public String getKeyTag(String key) { ShardedJedis shardedJedis = redisService.getRedisClient(); String result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getKeyTag(key); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Collection<JedisShardInfo> getAllShardInfo() { ShardedJedis shardedJedis = redisService.getRedisClient(); Collection<JedisShardInfo> result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getAllShardInfo(); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } public Collection<Jedis> getAllShards() { ShardedJedis shardedJedis = redisService.getRedisClient(); Collection<Jedis> result = null; if (shardedJedis == null) { return result; } boolean flag = false; try { result = shardedJedis.getAllShards(); } catch (Exception e) { log.error(e.getMessage(), e); flag = true; } finally { redisService.returnResource(shardedJedis, flag); } return result; } }
使用的时候,直接调用此类中对应的方法即可。
Redis的内部实现是C写的,有必要好好看看里面究竟是怎么实现的。
(1)Redis和MongoDB比较:
http://taotao1240.blog.51cto.com/731446/755173
(2)Redis官方下载地址:
http://redis.cn/download.html
(3) 按键通知事件
http://redis.mjplay.com.cn/topic/notification.html
(4)Redis内部实现
http://www.redisbook.com/en/latest/
(5)Redis持久化
http://blog.nosqlfan.com/html/3813.html
(6)Redis的AOF详细解读
http://www.wzxue.com/redis%E6%A0%B8%E5%BF%83%E8%A7%A3%E8%AF%BB-aof%E4%B8%8Erewrite%E6%9C%BA%E5%88%B6/
(7)Redis失效机制解析
http://blog.nosqlfan.com/html/4218.html
(8)Redis命令列表
http://redis.cn/commands.html
(9)Redis应用场景
http://www.coderli.com/redis-application-scenarios
(10)Jedis下载地址:
https://github.com/xetorthio/jedis