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
共41个文件
java:35个
groovy:3个
xml:1个
0 下载量 169 浏览量
2024-01-08
20:29:34
上传
评论
收藏 43KB ZIP 举报
温馨提示
基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip 基于java的开发源码-Java缓存工具 SimpleCache.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于java的开发源码-Java缓存工具 SimpleCache.zip (41个子文件)
yishh-cache-09860a6
README 10KB
pom.xml 5KB
src
test
java
cache
TestRedisCounter.java 1KB
AppTest.java 633B
TestRouteRedis.java 8KB
main
java
com
skymobi
sns
cache
tools
RedisReplicationTool.java 4KB
RedisCleanTool.java 1KB
NullCachedObject.java 349B
annotation
CacheKeyEl.java 288B
WriteListCache.java 674B
WriteCache.java 691B
CacheProxy.java 359B
SimpleCache.java 534B
Cache.java 513B
ListedCache.java 535B
CacheKey.java 421B
RemoveCache.java 586B
memcached
MemcachedInterceptor.java 2KB
AbstractInterceptor.java 10KB
route
ModFunction.java 851B
FunctionRouter.java 2KB
AbstractRouter.java 773B
Function.java 270B
DefaultKeyRouter.java 2KB
KeyRouter.java 439B
AbstractCounter.java 571B
CacheUtils.java 6KB
redis
BinaryJedisRunnable.java 371B
RedisClient.java 27KB
RedisInterceptor.java 3KB
RedisCounter.java 2KB
PipelineRunnable.java 416B
JedisRunnable.java 361B
hybrid
HybridInterceptor.java 2KB
Counter.java 564B
EasyCacheProxy.java 4KB
CacheInterceptor.java 1KB
groovy
redis_clean.groovy 683B
test_group.groovy 565B
redis_copy.groovy 3KB
cache.iml 3KB
共 41 条
- 1
资源评论
快乐无限出发
- 粉丝: 1126
- 资源: 7260
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功