|
@@ -5,12 +5,13 @@ package com.bootdo.common.redis.shiro;
|
|
* @version V1.0
|
|
* @version V1.0
|
|
*/
|
|
*/
|
|
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
-import redis.clients.jedis.Jedis;
|
|
|
|
-import redis.clients.jedis.JedisPool;
|
|
|
|
-import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
|
+import redis.clients.jedis.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -33,7 +34,8 @@ public class RedisManager {
|
|
@Value("${spring.redis.password}")
|
|
@Value("${spring.redis.password}")
|
|
private String password = "";
|
|
private String password = "";
|
|
|
|
|
|
- private static JedisPool jedisPool = null;
|
|
|
|
|
|
+ private static Jedis jedis = null;
|
|
|
|
+ private static JedisCluster jedisCluster = null;
|
|
|
|
|
|
public RedisManager() {
|
|
public RedisManager() {
|
|
|
|
|
|
@@ -43,18 +45,43 @@ public class RedisManager {
|
|
* 初始化方法
|
|
* 初始化方法
|
|
*/
|
|
*/
|
|
public void init() {
|
|
public void init() {
|
|
- if (jedisPool == null) {
|
|
|
|
- if (password != null && !"".equals(password)) {
|
|
|
|
- jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout, password);
|
|
|
|
- } else if (timeout != 0) {
|
|
|
|
- jedisPool = new JedisPool(new JedisPoolConfig(), host, port, timeout);
|
|
|
|
- } else {
|
|
|
|
- jedisPool = new JedisPool(new JedisPoolConfig(), host, port);
|
|
|
|
|
|
+ if (host.contains(",")) {
|
|
|
|
+ if(jedisCluster == null){
|
|
|
|
+ JedisPoolConfig config = new JedisPoolConfig();
|
|
|
|
+ config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
|
|
|
|
+ config.setLifo(true);
|
|
|
|
+ config.setMaxIdle(30);
|
|
|
|
+ config.setMaxTotal(30);
|
|
|
|
+ config.setMaxWaitMillis(-1);
|
|
|
|
+ config.setMinEvictableIdleTimeMillis(3600000);
|
|
|
|
+ config.setNumTestsPerEvictionRun(3);
|
|
|
|
+ config.setSoftMinEvictableIdleTimeMillis(3600000);
|
|
|
|
+ String[] addrs = host.split(",");
|
|
|
|
+ Set<HostAndPort> nodes = new HashSet<>();
|
|
|
|
+ for (int i = 0; i < addrs.length; i++) {
|
|
|
|
+ nodes.add(new HostAndPort(addrs[i].split(":")[0], Integer.parseInt(addrs[i].split(":")[1])));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ jedisCluster = new JedisCluster(nodes, 10000, 10000, 100, password, config);//xxxx是Redis的密码
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ } else {
|
|
|
|
+ if (jedis == null) {
|
|
|
|
+ if (password != null && !"".equals(password)) {
|
|
|
|
+ jedis = new JedisPool(new JedisPoolConfig(), host, port, timeout, password).getResource();
|
|
|
|
+ } else if (timeout != 0) {
|
|
|
|
+ jedis = new JedisPool(new JedisPoolConfig(), host, port, timeout).getResource();
|
|
|
|
+ } else {
|
|
|
|
+ jedis = new JedisPool(new JedisPoolConfig(), host, port).getResource();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* get value from redis
|
|
* get value from redis
|
|
*
|
|
*
|
|
@@ -63,9 +90,14 @@ public class RedisManager {
|
|
*/
|
|
*/
|
|
public byte[] get(byte[] key) {
|
|
public byte[] get(byte[] key) {
|
|
byte[] value = null;
|
|
byte[] value = null;
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- value = jedis.get(key);
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ value = jedisCluster.get(key);
|
|
|
|
+ } else {
|
|
|
|
+ value = jedis.get(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -82,12 +114,20 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] set(byte[] key, byte[] value) {
|
|
public byte[] set(byte[] key, byte[] value) {
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- jedis.set(key, value);
|
|
|
|
- if (this.expire != 0) {
|
|
|
|
- jedis.expire(key, this.expire);
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ jedisCluster.set(key, value);
|
|
|
|
+ if (this.expire != 0) {
|
|
|
|
+ jedisCluster.expire(key, this.expire);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ jedis.set(key, value);
|
|
|
|
+ if (this.expire != 0) {
|
|
|
|
+ jedis.expire(key, this.expire);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -105,12 +145,20 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] set(byte[] key, byte[] value, int expire) {
|
|
public byte[] set(byte[] key, byte[] value, int expire) {
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- jedis.set(key, value);
|
|
|
|
- if (expire != 0) {
|
|
|
|
- jedis.expire(key, expire);
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ jedisCluster.set(key, value);
|
|
|
|
+ if (expire != 0) {
|
|
|
|
+ jedisCluster.expire(key, expire);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ jedis.set(key, value);
|
|
|
|
+ if (expire != 0) {
|
|
|
|
+ jedis.expire(key, expire);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -125,9 +173,13 @@ public class RedisManager {
|
|
* @param key
|
|
* @param key
|
|
*/
|
|
*/
|
|
public void del(byte[] key) {
|
|
public void del(byte[] key) {
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
try {
|
|
try {
|
|
- jedis.del(key);
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ jedisCluster.del(key);
|
|
|
|
+ } else {
|
|
|
|
+ jedis.del(key);
|
|
|
|
+ }
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -139,9 +191,14 @@ public class RedisManager {
|
|
* flush
|
|
* flush
|
|
*/
|
|
*/
|
|
public void flushDB() {
|
|
public void flushDB() {
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
|
|
+
|
|
try {
|
|
try {
|
|
- jedis.flushDB();
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ jedisCluster.flushDB();
|
|
|
|
+ } else {
|
|
|
|
+ jedis.flushDB();
|
|
|
|
+ }
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -154,9 +211,13 @@ public class RedisManager {
|
|
*/
|
|
*/
|
|
public Long dbSize() {
|
|
public Long dbSize() {
|
|
Long dbSize = 0L;
|
|
Long dbSize = 0L;
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
try {
|
|
try {
|
|
- dbSize = jedis.dbSize();
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ dbSize = jedis.dbSize();
|
|
|
|
+ } else {
|
|
|
|
+ dbSize = jedis.dbSize();
|
|
|
|
+ }
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|
|
@@ -168,14 +229,30 @@ public class RedisManager {
|
|
/**
|
|
/**
|
|
* keys
|
|
* keys
|
|
*
|
|
*
|
|
- * @param regex
|
|
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Set<byte[]> keys(String pattern) {
|
|
public Set<byte[]> keys(String pattern) {
|
|
- Set<byte[]> keys = null;
|
|
|
|
- Jedis jedis = jedisPool.getResource();
|
|
|
|
|
|
+ Set<byte[]> keys = new HashSet<>();
|
|
try {
|
|
try {
|
|
- keys = jedis.keys(pattern.getBytes());
|
|
|
|
|
|
+ if(jedisCluster != null){
|
|
|
|
+ Set<String> keysStr = new HashSet<>();
|
|
|
|
+ String cursor = "0";
|
|
|
|
+ ScanParams scanParams = new ScanParams().match(pattern).count(100);
|
|
|
|
+ do {
|
|
|
|
+ ScanResult<String> scanResult = jedisCluster.scan(cursor, scanParams);
|
|
|
|
+ List<String> result = scanResult.getResult();
|
|
|
|
+ keysStr.addAll(result);
|
|
|
|
+ cursor = scanResult.getStringCursor();
|
|
|
|
+ } while (!"0".equals(cursor));
|
|
|
|
+
|
|
|
|
+ for (String str : keysStr) {
|
|
|
|
+ byte[] byteArray = str.getBytes(StandardCharsets.UTF_8); // 将字符串转换为UTF-8编码的字节数组
|
|
|
|
+ keys.add(byteArray); // 将字节数组添加到新的集合中
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ keys = jedis.keys(pattern.getBytes());
|
|
|
|
+ }
|
|
|
|
+
|
|
} finally {
|
|
} finally {
|
|
if (jedis != null) {
|
|
if (jedis != null) {
|
|
jedis.close();
|
|
jedis.close();
|