package com.custom.redis.routing;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.custom.redis.ICustomRedis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.args.ListPosition;
import redis.clients.jedis.params.SortingParams;
import redis.clients.jedis.resps.Tuple;
/**
* 路由实现redis集群
* 提供各种redis操作接口
* <P>File name : .java </P>
* <P>Author : anytron </P>
* <P>Date : 2016-1-29 </P>
*/
public class RoutingCustomRedis implements ICustomRedis{
private static final Logger LOG = LoggerFactory.getLogger(RoutingCustomRedis.class);
private static RedisRoutingManager redisRoutingManager = RedisRoutingManager.getInstance();
private RedisCluster cluster;
public RoutingCustomRedis(String clusterId){
cluster = redisRoutingManager.getRedisCluster(clusterId);
if(cluster == null){
throw new RedisRoutingException("clusterId["+clusterId+"] is not");
}
}
/**
* 给指定key的字符串值追加 value,返回新字符串值的长度。
* <P>Author : anytron </P>
* <P>Date : 2016-1-17 </P>
* @param key
* @param value
* @return
*/
public Long append(String key, String value) {
Long l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.append(key, value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return l;
}
/**
* 对key的值做的是减减操作,decr一个不存在 key,则设置key为-1
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @return
* @see com.custom.redis.ICustomRedis#decr(java.lang.String)
*/
public Long decr(String key) {
Long l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.decr(key);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
debuglog("decr",key,"");
return l;
}
/**
* 对 key 减去指定值 , key 不存在时候会设置 key ,并认为原来的 value为0
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @param value
* @return
* @see com.custom.redis.ICustomRedis#decrBy(java.lang.String, long)
*/
public Long decrBy(String key, long value) {
Long l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.decrBy(key, value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
debuglog("decrBy",key,String.valueOf(value));
return l;
}
/**
* 判断指定键是否存在
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @return
* @see com.custom.redis.ICustomRedis#exists(java.lang.String)
*/
public Boolean exists(String key) {
Boolean l = false;
JedisPool pool = cluster.getReadRedisPool(key);
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.exists(key);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return l;
}
/**
* 设置一个key 的过期时间(单位:秒), 返回 1 成功 ,0 表示 key已经设置过过期时间或者不存在
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @param value
* @return
* @see com.custom.redis.ICustomRedis#expire(java.lang.String, int)
*/
public Long expire(String key, int value) {
Long l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.expire(key, value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return l;
}
/**
* 获取key 对应的string值,如果key 不存在返回 nil。
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @return
* @see com.custom.redis.ICustomRedis#get(java.lang.String)
*/
public String get(String key) {
String l = null;
JedisPool pool = cluster.getReadRedisPool(key);
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.get(key) ;
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return l;
}
/**
* 先获取 key的值,再设置 key的值。如果 key不存在返回 null 。
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @param value
* @return
* @see com.custom.redis.ICustomRedis#getSet(java.lang.String, java.lang.String)
*/
public String getSet(String key, String value) {
String l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.getSet(key, value);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
debuglog("getSet",key,String.valueOf(value));
return l;
}
public Boolean getbit(String arg0, long arg1) {
return null;
}
/**
* 返回 key 中字符串值的子字符串,字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。
* 负数偏移量表示从字符串最后开始计数, -1 表示最后一个字符, -2 表示倒数第二个,以此类推。
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @param start
* @param end
* @return
* @see com.custom.redis.ICustomRedis#getrange(java.lang.String, long, long)
*/
public String getrange(String key, long start, long end) {
String l = null;
JedisPool pool = cluster.getReadRedisPool(key);
Jedis jedis = null;
try {
jedis = pool.getResource();
l = jedis.getrange(key, start, end);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(jedis != null){
try {
pool.returnResource(jedis);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return l;
}
/**
* 删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
* <P>Author : anytron </P>.
* <P>Date : 2016-1-31 </P>
* @param key
* @param arg1
* @return
* @see com.custom.redis.ICustomRedis#hdel(java.lang.String, java.lang.String[])
*/
public Long hdel(String key, String... arg1) {
Long l = null;
List<JedisPool> pools = cluster.getWriteRedisPool(key);
for(JedisPool pool : pools){
Jedis je
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
custom-redis-master.zip (18个子文件)
custom-redis-master
pom.xml 2KB
src
main
resources
custom-redis.xml 2KB
java
com
custom
redis
CustomRedisFactory.java 7KB
routing
RedisClusterNode.java 3KB
RedisRoutingException.java 505B
RedisClusterNodeMonitor.java 5KB
RoutingCustomRedis.java 73KB
RedisRoutingManager.java 5KB
RedisCluster.java 8KB
RoutingConfigManager.java 2KB
RedisRoutingConstants.java 1KB
AppRedis.java 10KB
ICustomRedis.java 13KB
util
CustomRedisConstants.java 232B
CustomRedisNodeMonitor.java 4KB
ConsistentHash.java 2KB
LICENSE 11KB
.gitignore 212B
共 18 条
- 1
资源评论
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6742
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功