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缓存工具 SimpleCache.7z
共41个文件
java:35个
groovy:3个
iml:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 44 浏览量
2022-07-06
11:10:43
上传
评论
收藏 17KB 7Z 举报
温馨提示
java缓存工具 SimpleCache.7z
资源推荐
资源详情
资源评论
收起资源包目录
java缓存工具 SimpleCache.7z (41个子文件)
java缓存工具 SimpleCache
yishh-cache-09860a6
pom.xml 5KB
src
test
java
cache
TestRedisCounter.java 1KB
TestRouteRedis.java 8KB
AppTest.java 633B
main
java
com
skymobi
sns
cache
hybrid
HybridInterceptor.java 2KB
EasyCacheProxy.java 4KB
CacheInterceptor.java 1KB
route
FunctionRouter.java 2KB
DefaultKeyRouter.java 2KB
Function.java 270B
AbstractRouter.java 773B
KeyRouter.java 439B
ModFunction.java 851B
tools
RedisReplicationTool.java 4KB
RedisCleanTool.java 1KB
AbstractCounter.java 571B
annotation
CacheKeyEl.java 288B
CacheProxy.java 359B
WriteCache.java 691B
CacheKey.java 421B
Cache.java 513B
SimpleCache.java 534B
ListedCache.java 535B
WriteListCache.java 674B
RemoveCache.java 586B
Counter.java 564B
memcached
MemcachedInterceptor.java 2KB
NullCachedObject.java 349B
redis
RedisInterceptor.java 3KB
JedisRunnable.java 361B
BinaryJedisRunnable.java 371B
PipelineRunnable.java 416B
RedisClient.java 27KB
RedisCounter.java 2KB
AbstractInterceptor.java 10KB
CacheUtils.java 6KB
groovy
redis_clean.groovy 683B
redis_copy.groovy 3KB
test_group.groovy 565B
README 10KB
cache.iml 3KB
共 41 条
- 1
资源评论
BryanDing
- 粉丝: 297
- 资源: 5587
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功