package com.skymobi.sns.cache.redis;
import com.google.common.collect.Lists;
import com.google.common.primitives.Chars;
import com.skymobi.sns.cache.route.KeyRouter;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.util.ClassUtils;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.*;
import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.util.Pool;
import redis.clients.util.SafeEncoder;
import java.util.*;
public class RedisClient {
private List<JedisShardInfo> shards;
private List<Jedis> jedis;
private ShardedJedisPool defaultPool;
private Map<String, JedisPool> jedisPoolMap = new HashMap<String, JedisPool>();
private KeyRouter keyRouter;
// private Kryo kryo = new Kryo();
public RedisClient() {
}
public RedisClient(String addresses) {
this(addresses, null);
}
public RedisClient(String addresses, GenericObjectPool.Config poolConfig) {
shards = new ArrayList<JedisShardInfo>();
String[] addressArray = addresses.split(" ");
jedis = new ArrayList<Jedis>();
for (String address : addressArray) {
String[] parts = address.split(":");
JedisShardInfo si = new JedisShardInfo(parts[0], Integer.parseInt(parts[1]));
shards.add(si);
jedis.add(new Jedis(parts[0], Integer.parseInt(parts[1])));
}
if (poolConfig == null) {
poolConfig = getDefaultPoolConfig();
}
defaultPool = new ShardedJedisPool(poolConfig, shards);
}
public RedisClient(KeyRouter keyRouter) {
this(keyRouter, null);
}
public RedisClient(KeyRouter keyRouter, GenericObjectPool.Config poolConfig) {
this.keyRouter = keyRouter;
if (poolConfig == null) {
poolConfig = getDefaultPoolConfig();
}
for (String host : this.keyRouter.getHosts()) {
JedisPool pool = getJedisPool(poolConfig, host);
jedisPoolMap.put(host, pool);
}
if (!jedisPoolMap.containsKey(keyRouter.getDefaultHost())) {
jedisPoolMap.put(keyRouter.getDefaultHost(), getJedisPool(poolConfig, keyRouter.getDefaultHost()));
}
}
private JedisPool getJedisPool(GenericObjectPool.Config poolConfig, String host) {
JedisPool pool;
int passwordSplit = host.indexOf("/");
if (passwordSplit == -1) {
String[] parts = host.split(":");
pool = new JedisPool(poolConfig, parts[0], Integer.parseInt(parts[1]));
} else {
String[] parts = host.substring(0, passwordSplit).split(":");
String password = host.substring(passwordSplit + 1);
pool = new JedisPool(poolConfig, parts[0], Integer.parseInt(parts[1]), Protocol.DEFAULT_TIMEOUT, password);
}
return pool;
}
private GenericObjectPool.Config getDefaultPoolConfig() {
GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
poolConfig.testWhileIdle = true;
poolConfig.minEvictableIdleTimeMillis = 60000;
poolConfig.timeBetweenEvictionRunsMillis = 30000;
poolConfig.numTestsPerEvictionRun = -1;
return poolConfig;
}
public ShardedJedisPool getPool() {
return defaultPool;
}
//在队列尾部增加
public <T> long rpush(String key, T o) {
final byte[] keyByte = toByte(key, true);
final byte[] value = toByte(o, false);
return execTask(key, new BinaryJedisRunnable<Long>() {
@Override
public Long run(BinaryJedisCommands jedis) {
return jedis.rpush(keyByte, value);
}
});
}
public <T> String setex(String key, int time, T o) {
final byte[] keyByte = toByte(key, true);
final byte[] value = toByte(o, false);
final int alive = time;
return execTask(key, new BinaryJedisRunnable<String>() {
@Override
public String run(BinaryJedisCommands jedis) {
return jedis.setex(keyByte, alive, value);
}
});
}
public long remove(final String key) {
if (jedisPoolMap.isEmpty() && keyRouter == null) {
ShardedJedis shardedJedis = defaultPool.getResource();
try {
return shardedJedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
defaultPool.returnResource(shardedJedis);
}
} else {
String host = keyRouter.getHost(key);
JedisPool pool = jedisPoolMap.get(host);
Jedis jedis = pool.getResource();
try {
return jedis.del(key);
} catch (Exception e) {
e.printStackTrace();
} finally {
pool.returnResource(jedis);
}
}
return -1;
}
public <T> boolean exists(final String key) {
final byte[] keyByte = toByte(key, true);
return execTask(key, new BinaryJedisRunnable<Boolean>() {
@Override
public Boolean run(BinaryJedisCommands jedis) {
return jedis.exists(keyByte);
}
});
}
//再头部添加元素
public <T> long lpush(String key, T o) {
final byte[] keyByte = toByte(key, true);
final byte[] value = toByte(o, false);
return execTask(key, new BinaryJedisRunnable<Long>() {
@Override
public Long run(BinaryJedisCommands jedis) {
// jedis.
return jedis.lpush(keyByte, value);
}
});
}
// add by whz 出队列 添加 丛尾部溢出元素
public <T> T rpop(String key, final Class<T> c) {
final byte[] keyByte = toByte(key, true);
return execTask(key, new BinaryJedisRunnable<T>() {
@Override
public T run(BinaryJedisCommands jedis) {
byte[] value = jedis.rpop(keyByte);
return (T) fromByte(value, c);
}
});
}
//头部溢出元素
public <T> T lpop(String key, final Class<T> c) {
final byte[] keyByte = toByte(key, true);
return execTask(key, new BinaryJedisRunnable<T>() {
@Override
public T run(BinaryJedisCommands jedis) {
byte[] value = jedis.lpop(keyByte);
return (T) fromByte(value, c);
}
});
}
//添加元素set中
public <T> long sadd(String key, T o) {
final byte[] keyByte = toByte(key, true);
final byte[] value = toByte(o, false);
return execTask(key, new BinaryJedisRunnable<Long>() {
@Override
public Long run(BinaryJedisCommands jedis) {
return jedis.sadd(keyByte, value);
}
});
}
public <T> long hset(String key,Integer hash, T o) {
final byte[] keyByte = toByte(key, true);
final byte[] value = toByte(o, false);
final byte[] hashbyte = toByte(hash, false);
return execTask(key, new BinaryJedisRunnable<Long>() {
@Override
public Long run(BinaryJedisCommands jedis) {
return jedis.hset(keyByte, hashbyte, value);
}
});
}
public <T> T hget(String key,Integer hash, final Class<T> c) {
final byte[] keyByte = toByte(key, true);
final byte[] hashbyte = toByte(hash, false);
return execTask(key, new BinaryJedisRunnable<T>() {
@Override
public T run(BinaryJedisCommands jedis) {
byte[] value = jedis.hget(keyByte,hashbyte);
return (T) fromByte(value, c);
}
});
}
public <T> long zadd(String key, T o,final double order) {
final byte[] keyByte = toByte(key, true);
final b
基于Java的Java缓存工具 SimpleCache.zip
116 浏览量
2023-06-15
01:08:14
上传
评论
收藏 43KB ZIP 举报
快乐无限出发
- 粉丝: 1127
- 资源: 7265
最新资源
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
- 以下是一个简化的示例,它使用pygame库来模拟烟花动画的框架.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈