redis缓存数据库
1.了解nosql起源
nosql:非关系型数据库,解决关系型数据库的数据存取瓶颈,大数据量的存储,频繁的查询行为。
NoSQL用于超大规模数据的存储。(例如谷歌或Facebook每天为他们的用户收集万亿比特的数据)。这些类型的数据存储不需要固定的模式,无需多余操作就可以横向扩展。
没有统一的标准,做不到学了一个nosql,其他的都可以会。
nosql数据大多数基于内存,但是因为内存不可靠,一般都会提供持久化机制,在启动服务端的时候,把持久化数据再次加载到内存。
2.nosql的几种常用的数据库.
KV数据库:key-value键值对数据库,ehcache与项目耦合,memcache独立部署服务器,redis
文档数据库:存的是bson对象数据,mongodb,能够取代mysql,基于内存,数据字段可扩展。尤其适合于数据分析行业。
列式数据库:HBase,大数据平台。尤其适合于超大数据量的存储。
图形数据库:数据关系拓扑图,适合于社交平台,论文系统。
3. Redis介绍
- Redis介绍
Redis是一个key-value存储系统。
Redis是一种面向“键/值”对类型数据的分布式NoSQL数据库系统。
它支持存储的类型包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。
- Redis特点
高性能,持久存储,适应高并发的应用场景。
相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。
Redis数据库完全在内存中,使用磁盘仅用于持久性。
Redis可以将数据复制到任意数量的从服务器
4. redis的使用场景以及带来的相关问题:
使用场景:解决分布式场景下数据共享问题(HttpSession共享);存储临时数据(验证码,文件下载数,用户积分,购物车);存储系统中常用数据缓存(系统用户信息,用户权限列表信息,字典项数据);消息队列(发短信,发邮件等耗时间的操作统统都可以放消息队列进行异步操作)
常用数据的缓存带来的问题:多数据源数据同步问题
方案1:(增删改操作mysql数据库,清理缓存对应的数据;查询操作先查缓存,缓存没有,则查询数据库,把查询结果放缓存)
方案2:(增删改操作mysql数据;查询操作先查缓存,缓存没有,则查询数据库,把查询结果放缓存,放缓存的时候给缓存数据设置有效期)
5.redis的环境搭建
-
服务端:
redis-server.exe:加载redis-windows.conf启动redis服务端
redis-server.exe redis.windows.conf //可建一个6379.bat 里面放redis-server.exe redis.windows.conf 双击这个即可启动redis服务端
redis-cli.exe:命令行客户端程序,用来连接服务端,执行指令
redis-cli.exe -h ip地址 -p port端口 -a password密码 //下面设置好的
redis-check-aof.exe:检测aof持久化文件,修复该文件
check-check-aof.exe --fix aop文件名
redis-check-dump.exe:检测dump持久化文件,修改该文件
redis-check-dump.exe --fix dump文件名
redis-windows.conf:核心配置文件,配置端口,密码等信息。
端口号(41): port 6379 默认数据库个数(113): databases 16 RDB持久化策略(137): (900秒内有一个数据发生改变做一次持久化 300秒内有10个数据发生改变做一次持久化 ....) save 900 1 save 300 10 save 60 10000 RDB持久化文件的名称和目录(172 182): dbfilename dump.rdb dir ./ 启动连接密码(386): requirepass foobared 开启aof持久化(521): appendonly yes aop持久化文件的名称(524): appendfilename "appendonly.aof" aop文件的重写策略(590): auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
-
桌面可视化客户端程序:
redis-desktop-manager
//补
redis的持久化策略
RDB持久化:存的是kv数据,按照策略保存,风险更大。
AOF持久化:存的是指令,每秒保存一次
6.redis的常用操作指令
redis的针对value的5种数据类型:
key:String
value:(1.string字符串 2.List列表 3.Hash哈希 4.Set集合 5.zSet排序集合),使用频率最多的就是String,Hash
相关指令:string
string:set/get/incr/decr/incrby/decrby/setex/setnx(分布式锁)/getset(复合指令)
1 SET key value
设置指定 key 的值
2 GET key
获取指定 key 的值。
3 INCR key
将 key 中储存的数字值增一。
4 INCRBY key increment
将 key 所储存的值加上给定的增量值(increment) 。
5 DECR key
将 key 中储存的数字值减一。
6 DECRBY key decrement
key 所储存的值减去给定的减量值(decrement) 。
7 SETEX key seconds value
将值 value 关联到 key ,并将 key 的过期时间设为 seconds (以秒为单位)。
8 SETNX key value
只有在 key 不存在时设置 key 的值。
9 GETSET key value
将给定 key 的值设为 value ,并返回 key 的旧值(old value)。
相关指令:list
适合存储列表性数据,消息队列,红包列表(特征:数据不改变只获取)
lpush/lpop/lrange/llen/lindex
1. LPUSH key value1 [value2] lpush e 1 2 3 4 5
将一个或多个值插入到列表头部
2 LPOP key
移出并获取列表的第一个元素
3 LRANGE key start stop
获取列表指定范围内的元素
4 LLEN key
获取列表长度
5 LINDEX key index
通过索引获取列表中的元素
相关指令:hash
适合于存储集合,系统用户集合(手机号做key,用户详情作为value)
hset/hget/hdel/hexists/hgetall/hkeys/hvals/hlen/hsetnx
0 HMSET key field1 value1 [field2 value2 ]
//hmset h 0 "user:xiaoming,pass:0" 1 "user:xiaoming,pass:1"
同时将多个 field-value (域-值)对设置到哈希表 key 中。
1 HSET key field value //hset g 1111 user:xiaoming,pass:1111
将哈希表 key 中的字段 field 的值设为 value 。
2 HGET key field
获取存储在哈希表中指定字段的值。
3 HDEL key field1 [field2]
删除一个或多个哈希表字段
4 HEXISTS key field
查看哈希表 key 中,指定的字段是否存在。
5 HGETALL key
获取在哈希表中指定 key 的所有字段和值
6 HKEYS key
获取所有哈希表中的字段
7 HVALS key
获取哈希表中所有值。
8 HLEN key
获取哈希表中字段的数量
9 HSETNX key field value
只有在字段 field 不存在时,设置哈希表字段的值。
set无序集合
相对于list来说,数据无序,适合于做集合的交并差
sadd/spop/sdiff(差集)/sunion(并集)/sinter(交集)/smembers
1 SADD key member1 [member2]
向集合添加一个或多个成员
2 SPOP key
移除并返回集合中的一个随机元素
3 SDIFF key1 [key2]
返回第一个集合与其他集合之间的差异。
4 SUNION key1 [key2]
返回所有给定集合的并集
5 SINTER key1 [key2]
返回给定所有集合的交集
6 SMEMBERS key
返回集合中的所有成员
zset(sortedSet):
适合于数据需要按照指定的字段进行排序的场景。(排名榜单)
zadd
zscore:取得成绩
zrank:升序排序后的排名
zrevrank:降序排序后的排名
zcount:获取成绩区间的成员个数
zrange:获取升序排名区间的成员
zrevrange:获取降序排名区间的成员
zrangebyscore:获取成绩区间的成员
1 ZADD key score1 member1 [score2 member2]
//zadd scores 81 a 75 b 64 c 99 d 55 e 11 f
向有序集合添加一个或多个成员,或者更新已存在成员的分数
2 ZSCORE key member
返回有序集中,成员的分数值
3 ZRANK key member
返回有序集合中指定成员的索引
4 ZREVRANK key member
返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
5 ZCOUNT key min max
计算在有序集合中指定区间分数的成员数
6 ZRANGE key start stop [WITHSCORES]
通过索引区间返回有序集合指定区间内的成员
7 ZREVRANGE key start stop [WITHSCORES]
返回有序集中指定区间内的成员,通过索引,分数从高到低
8 ZREVRANGEBYSCORE key max min [WITHSCORES]
返回有序集中指定分数区间内的成员,分数从高到低排序
相关指令:key
对key的操作:del/type/keys/ttl/expire/exists
1 DEL key
该命令用于在 key 存在时删除 key。
2 TYPE key
返回 key 所储存的值的类型。
3 KEYS pattern
查找所有符合给定模式( pattern)的 key 。
4 TTL key
以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
5 EXPIRE key seconds
为给定 key 设置过期时间,以秒计。
6 EXISTS key
检查给定 key 是否存在。
7.redis的javaApi:
jedis:客户端组件
- Jedis是Java连接Redis的驱动包,具备操作Redis的所有API,而且使用简单。
- 引入jar包:
- jedis-2.7.2.jar
- commons-pool2-2.3.jar
- jedis单机使用
- 运行代码
public class TestJedis {
public static void main(String[] args) {
Jedis j = new Jedis("127.0.0.1",6379);
j.auth("root");
//key使用:分割,进行便于分组显示
for(int i=0;i<1000;i++){
j.set("users:"+i,i+""); //"users:" 生成了一个users文件夹放users0,users0......
}
j.close();
}
}
- jedis多机使用
- 多redis实例启动
- 拷贝redis.windows.conf,修改新的端口为6380,启动第二个redis实例。
- 运行代码
- 多redis实例启动
public class TestSharedJedis {
public static void main(String[] args) {
//维护多台redis服务器的连接信息
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMinIdle(5); //最小空闲连接数
List<JedisShardInfo> infos = new ArrayList<>();
JedisShardInfo info1 =new JedisShardInfo("redis://xxx:root@127.0.0.1:6379/0");
infos.add(info1);
JedisShardInfo info2 = new JedisShardInfo("redis://xxx:root@127.0.0.1:6380/0");
infos.add(info2);
ShardedJedisPool pool = new ShardedJedisPool(poolConfig,infos);//分片连接池
//保存数据的时候,通过分片算法,把数据分到不同的服务器中进行存储。
ShardedJedis jedis = pool.getResource();
for(int i=0;i<1000;i++){
jedis.set("key:"+i,i+"");//
}
pool.close();
}
}
8.redis的集群搭建:
-
客户端的分片集群方案
服务端启动的多个redis服务之间没有任何关系,是独立的reids服务。
代码调用者,通过维护多台redis服务的连接。
缺点:服务器无法横向扩展(代码中维护服务器连接信息;当扩展了新服务器后,原服务器中的数据迁移难)
-
服务端哨兵集群方案,第四阶段,可以横线扩展
哨兵服务器(管理其他服务器)
服务端启动的多个redis服务,服务器之间有数据交互通道。
-
服务端cluster集群方案。
9. 客户端的分片集群方案简单应用
//redis.properties
jedis.minIdle=5
jedis.url1=redis://xxx:root@127.0.0.1:6379/0
jedis.url2=redis://xxx:root@127.0.0.1:6380/0
<!--redis.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:comtext="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--ignore-unresolvable="true" 忽略不能识别的单词 另一个引入property文件的xml也要写这个-->
<comtext:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"></comtext:property-placeholder>
<!--生成 new JedisPoolConfig() bean对象 并注入参数 minIdle=5-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="minIdle" value="${jedis.minIdle}"></property>
</bean>
<!--生成分片连接池对象 并注入参数(构造器注入)-->
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<!--注入第一个参数-->
<constructor-arg index="0" ref="poolConfig"></constructor-arg>
<!--注入第二个参数 -->
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${jedis.url1}"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${jedis.url2}"></constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
<!--spring.mvc 要引入redis.xml-->
<import resource="classpath:redis.xml"></import>
@Controller
@RequestMapping("user")
public class SysuserHandler {
private Logger l = LoggerFactory.getLogger(SysuserHandler.class);
@Resource
private ISysuserService us;
@PostMapping("login")
public ResponseEntity login(@RequestBody Map<String,String> map, HttpSession session){
String uphone = map.get("uphone");
String valicode = map.get("valicode");
//1. 校验验证按
boolean isok = us.checkValicode(uphone,valicode);
if(!isok){
throw new MvcException(StatusEnum.VALICODE_ERROR);
}
//2.根据手机号取获取用户对象
Sysuser loginuser = us.login(uphone);
if(loginuser!=null){
session.setAttribute("LOGIN_USER",loginuser);
loginuser.setUpwd("");
return ResponseEntity.ok(new ResponseBean(StatusEnum.LOGIN_SUC,loginuser));
}
throw new MvcException(StatusEnum.LOGIN_ERROR);
}
}
@Service
public class SysuserServiceImpl implements ISysuserService {
@Resource
private SysuserMapper sm;
@Resource
private RedisService redisService;
@Override
//1. 校验验证码 RedisKey.VALI_CODE枚举类redis的数据key词头
public boolean checkValicode(String uphone, String valicode) {
//通过key从缓存取数据
String serverCode = redisService.get(RedisKey.VALI_CODE + uphone);
//为空返回false
if(serverCode==null){
return false;
}
//判断传来的验证码是否跟缓存里的验证码相同
boolean isok = serverCode.equals(valicode);
if(isok){
//相同删除缓存里的验证码 保障安全性
redisService.del(RedisKey.VALI_CODE+uphone);
return true;
}
return false;
}
@Override
public Sysuser login(String uphone) {
Sysuser loginuser = null;
//先查询缓存,查不到再查数据库,查到放缓存;
/**
* 要把用户集合保存hash
* redisService.hset(RedisKey.USERS_HASH,uphone,JSON.toJsonString(Sysuser))
*/
String userinfo = redisService.hget(RedisKey.USERS_HASH, uphone);
if(userinfo==null){
loginuser = sm.selectUserByPhone(uphone);
if(loginuser==null)
{
//该手机号未注册
loginuser = new Sysuser();
loginuser.setUphone(uphone);
loginuser.setUname("小哥哥");
loginuser.setNewUser(1);
loginuser.setUpwd("1111");
loginuser.setRid(11);
sm.addUserAndGetUserId(loginuser);
}
redisService.hset(RedisKey.USERS_HASH,uphone, JSON.toJSONString(loginuser));
}else {
//已有对象情况下
//把json字符串转成对象
//fastjson工具
Sysuser sysuser = JSON.parseObject(userinfo,Sysuser.class);
//Gson工具
Gson g = new Gson();
loginuser = g.fromJson(userinfo,Sysuser.class);
}
return loginuser;
}
}
//补充
//1.json字符串转单个对象
//将json字符串转JSONObject对象
JSONObject jsonObject = JSON.parseObject(userinfo);
//将json字符串转具体对象
Sysuser sysuser = JSON.parseObject(userinfo,Sysuser.class);
//2.json集合字符串转集合对象
//fastjson工具转集合对象
List<Sysuser> list = JSON.parseArray(userinfo,Sysuser.class);
//Gson工具字符串转集合对象
Gson g = new Gson();
String s = g.toJson(userinfo);
//老师写的一个RedisService服务类 收藏好
package com.javasm.common.service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
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.ShardedJedisPool;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.Tuple;
/**
* Redis基类
*/
@Service
public class RedisService {
private static final Logger log = LogManager.getLogger(RedisService.class);
@Resource
private ShardedJedisPool shardedJedisPool;
/**
*
* @Title: set @Description: 设置单个值 @param @param key @param @param
* value @param @return @return String @throws
*/
public String set(String key, String value) {
ShardedJedis shardedJedis = shardedJedisPool.getResource();
String result = null;
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: get @Description: 获取单个值 @param @param key @param @return @return
* String @throws
*/
public String get(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: exists @Description: 确认一个key是否存在 @param @param
* key @param @return @return Boolean @throws
*/
public Boolean exists(String key) {
Boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: type @Description: 返回值的类型 @param @param key @param @return @return
* String @throws
*/
public String type(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.type(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: expire @Description: 设定一个key的活动时间(s) @param @param key @param @param
* seconds @param @return @return Long @throws
*/
public Long expire(String key, int seconds) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.expire(key, seconds);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: expireAt @Description: 在某个时间点失效 @param @param key @param @param
* unixTime @param @return @return Long @throws
*/
public Long expireAt(String key, long unixTime) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.expireAt(key, unixTime);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: ttl @Description: 获得一个key的活动时间 @param @param
* key @param @return @return Long @throws
*/
public Long ttl(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.ttl(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public boolean setbit(String key, long offset, boolean value) {
boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setbit(key, offset, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public boolean getbit(String key, long offset) {
boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getbit(key, offset);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public long setrange(String key, long offset, String value) {
long result = 0;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setrange(key, offset, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String getrange(String key, long startOffset, long endOffset) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getrange(key, startOffset, endOffset);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String getSet(String key, String value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long setnx(String key, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setnx(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String setex(String key, int seconds, String value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long decrBy(String key, long integer) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.decrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long decr(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.decr(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long incrBy(String key, long integer) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.incrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long incr(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.incr(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long append(String key, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.append(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String substr(String key, int start, int end) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.substr(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hset(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hset(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String hget(String key, String field) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hget(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hsetnx(String key, String field, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hsetnx(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String hmset(String key, Map<String, String> hash) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hmset(key, hash);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<String> hmget(String key, String... fields) {
List<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hmget(key, fields);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hincrBy(String key, String field, long value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hincrBy(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Boolean hexists(String key, String field) {
Boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hexists(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long del(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.del(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hdel(String key, String field) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hdel(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hlen(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hlen(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> hkeys(String key) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hkeys(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<String> hvals(String key) {
List<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hvals(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Map<String, String> hgetAll(String key) {
Map<String, String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hgetAll(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
// ================list ====== l表示 list或 left, r表示right====================
/**
*
* @Title: rpush @Description: 在指定Key所关联的List
* Value的尾部插入参数中给出的所有Values。如果该Key不存在,该命令将在插入之前创建一个与该Key关联的空链表,之后再将数据从链表的尾部插入。如果该键的Value不是链表类型,该命令将返回相关的错误信息。 @param @param
* key @param @param string @param @return @return Long @throws
*/
public Long rpush(String key, String string) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.rpush(key, string);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/**
*
* @Title: lpush @Description: 在指定Key所关联的List
* Value的头部插入参数中给出的所有Values。如果该Key不存在,该命令将在插入之前创建一个与该Key关联的空链表,之后再将数据从链表的头部插入。如果该键的Value不是链表类型,该命令将返回相关的错误信息。 @param @param
* key @param @param string @param @return @return Long @throws
*/
public Long lpush(String key, String string) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lpush(key, string);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long lpushx(String key, String string) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lpushx(key, string);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long llen(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.llen(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<String> lrange(String key, long start, long end) {
List<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String ltrim(String key, long start, long end) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.ltrim(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String lindex(String key, long index) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lindex(key, index);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String lset(String key, long index, String value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lset(key, index, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long lrem(String key, long count, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lrem(key, count, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String lpop(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lpop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String rpop(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.rpop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
// return 1 add a not exist value ,
// return 0 add a exist value
public Long sadd(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sadd(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> smembers(String key) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.smembers(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long srem(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.srem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String spop(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.spop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long scard(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.scard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Boolean sismember(String key, String member) {
Boolean result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sismember(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String srandmember(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.srandmember(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zadd(String key, double score, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zadd(key, score, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrange(String key, int start, int end) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrem(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Double zincrby(String key, double score, String member) {
Double result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zincrby(key, score, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrank(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrank(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrevrank(String key, String member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrank(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrevrange(String key, int start, int end) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeWithScores(String key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeWithScores(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeWithScores(String key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeWithScores(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zcard(String key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zcard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Double zscore(String key, String member) {
Double result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zscore(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<String> sort(String key) {
List<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sort(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<String> sort(String key, SortingParams sortingParameters) {
List<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sort(key, sortingParameters);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zcount(String key, double min, double max) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zcount(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrangeByScore(String key, double min, double max) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScore(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrevrangeByScore(String key, double max, double min) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScore(key, max, min);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrangeByScore(String key, double min, double max, int offset, int count) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScore(key, min, max, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<String> zrevrangeByScore(String key, double max, double min, int offset, int count) {
Set<String> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScore(key, max, min, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScoreWithScores(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScoreWithScores(key, max, min);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScoreWithScores(key, min, max, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zremrangeByRank(String key, int start, int end) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zremrangeByRank(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zremrangeByScore(String key, double start, double end) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zremrangeByScore(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long linsert(String key, LIST_POSITION where, String pivot, String value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.linsert(key, where, pivot, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String set(byte[] key, byte[] value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] get(byte[] key) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Boolean exists(byte[] key) {
Boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String type(byte[] key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.type(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long expire(byte[] key, int seconds) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.expire(key, seconds);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long expireAt(byte[] key, long unixTime) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.expireAt(key, unixTime);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long ttl(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.ttl(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] getSet(byte[] key, byte[] value) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getSet(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long setnx(byte[] key, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setnx(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String setex(byte[] key, int seconds, byte[] value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.setex(key, seconds, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long decrBy(byte[] key, long integer) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.decrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long decr(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.decr(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long incrBy(byte[] key, long integer) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.incrBy(key, integer);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long incr(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.incr(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long append(byte[] key, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.append(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] substr(byte[] key, int start, int end) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.substr(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hset(byte[] key, byte[] field, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hset(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] hget(byte[] key, byte[] field) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hget(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hsetnx(byte[] key, byte[] field, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hsetnx(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String hmset(byte[] key, Map<byte[], byte[]> hash) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hmset(key, hash);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<byte[]> hmget(byte[] key, byte[]... fields) {
List<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hmget(key, fields);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hincrBy(byte[] key, byte[] field, long value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hincrBy(key, field, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Boolean hexists(byte[] key, byte[] field) {
Boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hexists(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hdel(byte[] key, byte[] field) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hdel(key, field);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long hlen(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hlen(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> hkeys(byte[] key) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hkeys(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Collection<byte[]> hvals(byte[] key) {
Collection<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hvals(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Map<byte[], byte[]> hgetAll(byte[] key) {
Map<byte[], byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.hgetAll(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long rpush(byte[] key, byte[] string) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.rpush(key, string);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long lpush(byte[] key, byte[] string) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lpush(key, string);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long llen(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.llen(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<byte[]> lrange(byte[] key, int start, int end) {
List<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String ltrim(byte[] key, int start, int end) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.ltrim(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] lindex(byte[] key, int index) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lindex(key, index);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String lset(byte[] key, int index, byte[] value) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lset(key, index, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long lrem(byte[] key, int count, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lrem(key, count, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] lpop(byte[] key) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.lpop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] rpop(byte[] key) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.rpop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long sadd(byte[] key, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sadd(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> smembers(byte[] key) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.smembers(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long srem(byte[] key, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.srem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] spop(byte[] key) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.spop(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long scard(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.scard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Boolean sismember(byte[] key, byte[] member) {
Boolean result = false;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sismember(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public byte[] srandmember(byte[] key) {
byte[] result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.srandmember(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zadd(byte[] key, double score, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zadd(key, score, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrange(byte[] key, int start, int end) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrem(byte[] key, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrem(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Double zincrby(byte[] key, double score, byte[] member) {
Double result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zincrby(key, score, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrank(byte[] key, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrank(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zrevrank(byte[] key, byte[] member) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrank(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrevrange(byte[] key, int start, int end) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrange(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeWithScores(byte[] key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeWithScores(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeWithScores(byte[] key, int start, int end) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeWithScores(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zcard(byte[] key) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zcard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Double zscore(byte[] key, byte[] member) {
Double result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zscore(key, member);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<byte[]> sort(byte[] key) {
List<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sort(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public List<byte[]> sort(byte[] key, SortingParams sortingParameters) {
List<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.sort(key, sortingParameters);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zcount(byte[] key, double min, double max) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zcount(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrangeByScore(byte[] key, double min, double max) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScore(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrangeByScore(byte[] key, double min, double max, int offset, int count) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScore(key, min, max, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScoreWithScores(key, min, max);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrangeByScoreWithScores(byte[] key, double min, double max, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrangeByScoreWithScores(key, min, max, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrevrangeByScore(byte[] key, double max, double min) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScore(key, max, min);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<byte[]> zrevrangeByScore(byte[] key, double max, double min, int offset, int count) {
Set<byte[]> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScore(key, max, min, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeByScoreWithScores(byte[] key, double max, double min) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScoreWithScores(key, max, min);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Set<Tuple> zrevrangeByScoreWithScores(byte[] key, double max, double min, int offset, int count) {
Set<Tuple> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zremrangeByRank(byte[] key, int start, int end) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zremrangeByRank(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long zremrangeByScore(byte[] key, double start, double end) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.zremrangeByScore(key, start, end);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Long linsert(byte[] key, LIST_POSITION where, byte[] pivot, byte[] value) {
Long result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.linsert(key, where, pivot, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
/*
* public List<Object> pipelined(ShardedJedisPipeline shardedJedisPipeline) {
* List<Object> result = null; if (shardedJedis == null) { return result; }
* boolean broken = false; try { result =
* shardedJedis.pipelined(shardedJedisPipeline); } catch (Exception e) {
* log.error(e.getMessage(), e); broken = true; } finally {
* redisDataSource.returnResource(shardedJedis, broken); } return result; }
*/
public Jedis getShard(byte[] key) {
Jedis result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getShard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Jedis getShard(String key) {
Jedis result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getShard(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public JedisShardInfo getShardInfo(byte[] key) {
JedisShardInfo result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getShardInfo(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public JedisShardInfo getShardInfo(String key) {
JedisShardInfo result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getShardInfo(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public String getKeyTag(String key) {
String result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getKeyTag(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Collection<JedisShardInfo> getAllShardInfo() {
Collection<JedisShardInfo> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getAllShardInfo();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
public Collection<Jedis> getAllShards() {
Collection<Jedis> result = null;
ShardedJedis shardedJedis = shardedJedisPool.getResource();
if (shardedJedis == null) {
return result;
}
try {
result = shardedJedis.getAllShards();
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
shardedJedis.close();
}
return result;
}
}