import java.io.Serializable;
import java.util.*;
import org.apache.commons.lang3.SerializationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.*;
@Component(IRedisManager._IMPL)
public class RedisClusterManager implements IRedisManager {
private static Logger LOGGER= LoggerFactory.getLogger(RedisClusterManager.class);
@Autowired
JedisCluster jedisCluster;
public RedisClusterManager() {
}
public static void main(String[] args) {
Random random=new Random();
RedisClusterManager redisClusterManager=new RedisClusterManager();
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(2000
);
config.setMaxIdle(20);
Set<HostAndPort> hostAndPorts=new HashSet<>();
HostAndPort h1=new HostAndPort("127.0.0.1",7001);
HostAndPort h2=new HostAndPort("127.0.0.1",7002);
HostAndPort h3=new HostAndPort("127.0.0.1",7003);
HostAndPort h4=new HostAndPort("127.0.0.1",7004);
HostAndPort h5=new HostAndPort("127.0.0.1",7005);
HostAndPort h6=new HostAndPort("127.0.0.1",7006);
hostAndPorts.add(h1);
hostAndPorts.add(h2);
hostAndPorts.add(h3);
hostAndPorts.add(h4);
hostAndPorts.add(h5);
hostAndPorts.add(h6);
redisClusterManager.jedisCluster = new JedisCluster(hostAndPorts, config);
System.out.println(redisClusterManager.jedisCluster.set("session" + 333, "" + 444));
System.out.println(RedisResultStatus.OK.equals("OK"));
System.out.println("保存结束");
System.out.println(redisClusterManager.keys("session*").size());
}
@Override
public Set<byte[]> keys(byte[] pattern) {
HashSet<byte[]> keys = new HashSet();
Iterator jedisPoolIterator = this.jedisCluster.getClusterNodes().values().iterator();
while(jedisPoolIterator.hasNext()) {
JedisPool pool = (JedisPool)jedisPoolIterator.next();
Jedis jedis = null;
try {
jedis = pool.getResource();
keys.addAll(jedis.keys(pattern));
} finally {
if(jedis != null) {
jedis.close();
}
}
}
return keys;
}
@Override
public Set<String> keys(String pattern) {
HashSet<String> keys = new HashSet();
Iterator jedisPoolIterator = this.jedisCluster.getClusterNodes().values().iterator();
while(jedisPoolIterator.hasNext()) {
JedisPool pool = (JedisPool)jedisPoolIterator.next();
Jedis jedis = null;
try {
jedis = pool.getResource();
keys.addAll(jedis.keys(pattern));
} finally {
if(jedis != null) {
jedis.close();
}
}
}
return keys;
}
@Override
public boolean setString(String sKey, String sValue, int expiredInSeconds) {
return RedisResultStatus.OK.equals(this.jedisCluster.setex(sKey,expiredInSeconds,sValue));
}
@Override
public boolean setString(String sKey, String sValue) {
return RedisResultStatus.OK.equals(this.jedisCluster.set(sKey,sValue));
}
@Override
public String getString(String sKey) {
return this.jedisCluster.get(sKey);
}
@Override
public boolean setObject(String sKey, Serializable object, int expiredInSeconds) {
byte[] bk=sKey.getBytes();
byte[] bv= SerializationUtils.serialize(object);
return RedisResultStatus.OK.equals(this.jedisCluster.setex(bk,expiredInSeconds,bv));
}
@Override
public boolean setObject(String sKey, Serializable object) {
byte[] bk=sKey.getBytes();
byte[] bv= SerializationUtils.serialize(object);
return RedisResultStatus.OK.equals(this.jedisCluster.set(bk,bv));
}
@Override
public <T> T getObject(String sKey) {
byte[] bk=sKey.getBytes();
byte[] bv=this.jedisCluster.get(bk);
if (bv==null)
return null;
return SerializationUtils.deserialize(bv);
}
@Override
public void putMap(String sKey, Map<String, String> oMap, int expiredInSeconds) {
oMap.forEach((k,v)->{
this.jedisCluster.hset(sKey,k,v);
});
this.jedisCluster.expire(sKey,expiredInSeconds);
}
@Override
public void putMap(String sKey, Map<String, String> oMap) {
oMap.forEach((k,v)->{
this.jedisCluster.hset(sKey,k,v);
});
}
@Override
public boolean hset(String sKey, String sField, String sValue, int expiredInSeconds) {
boolean result= this.jedisCluster.hset(sKey,sField,sValue)>0;
if (result)
this.jedisCluster.expire(sKey,expiredInSeconds);
return result;
}
@Override
public boolean hset(String sKey, String sField, String sValue) {
return this.jedisCluster.hset(sKey,sField,sValue)>0;
}
@Override
public Map<String, String> getMap(String sKey) {
return this.jedisCluster.hgetAll(sKey);
}
@Override
public Map<String, String> getMap(String sKey, String[] fields) {
return null;
}
@Override
public boolean existsMapItem(String sKey, String sField) {
return false;
}
@Override
public boolean hset(String sKey, String sField, Serializable oValue, int expiredInSeconds) {
return false;
}
@Override
public boolean hset(String sKey, String sField, Serializable oValue) {
byte[] bv=SerializationUtils.serialize(oValue);
return this.jedisCluster.hset(sKey.getBytes(),sField.getBytes(),bv)>0;
}
@Override
public <T> T getMapValueAsObject(String sKey, String sField, Class<T> oClass) {
return null;
}
@Override
public void del(byte[] key){
jedisCluster.del(key);
}
@Override
public void del(String sKey) {
jedisCluster.del(sKey);
}
}
JEDIS访问集群模式的api
最新推荐文章于 2021-03-08 13:42:39 发布