|
@@ -5,83 +5,40 @@ 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 redis.clients.jedis.*;
|
|
|
|
|
|
+import com.bootdo.common.utils.RedisUtil;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
*/
|
|
*/
|
|
|
|
+@Component
|
|
public class RedisManager {
|
|
public class RedisManager {
|
|
-
|
|
|
|
- @Value("${spring.redis.host}")
|
|
|
|
- private String host = "127.0.0.1";
|
|
|
|
-
|
|
|
|
- @Value("${spring.redis.port}")
|
|
|
|
- private int port = 6379;
|
|
|
|
-
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private RedisUtil redisUtil;
|
|
// 0 - never expire
|
|
// 0 - never expire
|
|
private int expire = 0;
|
|
private int expire = 0;
|
|
|
|
|
|
- //timeout for jedis try to connect to redis server, not expire time! In milliseconds
|
|
|
|
- @Value("${spring.redis.timeout}")
|
|
|
|
- private int timeout = 0;
|
|
|
|
-
|
|
|
|
- @Value("${spring.redis.password}")
|
|
|
|
- private String password = "";
|
|
|
|
-
|
|
|
|
- private static Jedis jedis = null;
|
|
|
|
- private static JedisCluster jedisCluster = null;
|
|
|
|
-
|
|
|
|
public RedisManager() {
|
|
public RedisManager() {
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
/**
|
|
/**
|
|
- * 初始化方法
|
|
|
|
|
|
+ * 获得byte[]型的key
|
|
|
|
+ * @param key
|
|
|
|
+ * @return
|
|
*/
|
|
*/
|
|
- public void init() {
|
|
|
|
- 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();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
|
|
+ private byte[] getByteKey(Object key){
|
|
|
|
+ if(key instanceof String){
|
|
|
|
+ return ((String)key).getBytes();
|
|
|
|
+ }else{
|
|
|
|
+ return SerializeUtils.serialize(key);
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* get value from redis
|
|
* get value from redis
|
|
*
|
|
*
|
|
@@ -89,20 +46,7 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] get(byte[] key) {
|
|
public byte[] get(byte[] key) {
|
|
- byte[] value = null;
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- if(jedisCluster != null){
|
|
|
|
- value = jedisCluster.get(key);
|
|
|
|
- } else {
|
|
|
|
- value = jedis.get(key);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } finally {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ byte[] value = redisUtil.get(key);
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -114,24 +58,11 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] set(byte[] key, byte[] value) {
|
|
public byte[] set(byte[] key, byte[] value) {
|
|
-
|
|
|
|
- try {
|
|
|
|
- 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 {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
|
|
+ redisUtil.set(key, value,this.expire);
|
|
|
|
+ if (this.expire != 0) {
|
|
|
|
+ redisUtil.set(key, value,this.expire);
|
|
|
|
+ } else {
|
|
|
|
+ redisUtil.set(key, value);
|
|
}
|
|
}
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
@@ -145,25 +76,7 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public byte[] set(byte[] key, byte[] value, int expire) {
|
|
public byte[] set(byte[] key, byte[] value, int expire) {
|
|
-
|
|
|
|
- try {
|
|
|
|
- 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 {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ redisUtil.set(key, value,expire);
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -173,56 +86,21 @@ public class RedisManager {
|
|
* @param key
|
|
* @param key
|
|
*/
|
|
*/
|
|
public void del(byte[] key) {
|
|
public void del(byte[] key) {
|
|
- try {
|
|
|
|
- if(jedisCluster != null){
|
|
|
|
- jedisCluster.del(key);
|
|
|
|
- } else {
|
|
|
|
- jedis.del(key);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } finally {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ redisUtil.del(key);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* flush
|
|
* flush
|
|
*/
|
|
*/
|
|
public void flushDB() {
|
|
public void flushDB() {
|
|
-
|
|
|
|
- try {
|
|
|
|
- if(jedisCluster != null){
|
|
|
|
- jedisCluster.flushDB();
|
|
|
|
- } else {
|
|
|
|
- jedis.flushDB();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } finally {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ //do nothing
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* size
|
|
* size
|
|
*/
|
|
*/
|
|
public Long dbSize() {
|
|
public Long dbSize() {
|
|
- Long dbSize = 0L;
|
|
|
|
- try {
|
|
|
|
- if(jedisCluster != null){
|
|
|
|
- dbSize = jedis.dbSize();
|
|
|
|
- } else {
|
|
|
|
- dbSize = jedis.dbSize();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- } finally {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ Long dbSize = redisUtil.dbSize();
|
|
return dbSize;
|
|
return dbSize;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -232,50 +110,10 @@ public class RedisManager {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public Set<byte[]> keys(String pattern) {
|
|
public Set<byte[]> keys(String pattern) {
|
|
- Set<byte[]> keys = new HashSet<>();
|
|
|
|
- try {
|
|
|
|
- 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 {
|
|
|
|
- if (jedis != null) {
|
|
|
|
- jedis.close();
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ Set<byte[]> keys = redisUtil.keys(pattern.getBytes());
|
|
return keys;
|
|
return keys;
|
|
}
|
|
}
|
|
|
|
|
|
- public String getHost() {
|
|
|
|
- return host;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void setHost(String host) {
|
|
|
|
- this.host = host;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public int getPort() {
|
|
|
|
- return port;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void setPort(int port) {
|
|
|
|
- this.port = port;
|
|
|
|
- }
|
|
|
|
|
|
|
|
public int getExpire() {
|
|
public int getExpire() {
|
|
return expire;
|
|
return expire;
|
|
@@ -285,21 +123,5 @@ public class RedisManager {
|
|
this.expire = expire;
|
|
this.expire = expire;
|
|
}
|
|
}
|
|
|
|
|
|
- public int getTimeout() {
|
|
|
|
- return timeout;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void setTimeout(int timeout) {
|
|
|
|
- this.timeout = timeout;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public String getPassword() {
|
|
|
|
- return password;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public void setPassword(String password) {
|
|
|
|
- this.password = password;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
}
|
|
}
|