This commit is contained in:
lu7213 committed 2025-12-02 23:07:47 +08:00
1 parent 81eca8933a
commit 345f906c35
540 files changed
+494222 -90

No files matched your search

@@ -0,0 +1,352 @@
package com.ruoyi.common.redis;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* 手动添加缓存和移除缓存
*/
@Component
public class RedisUtil {
@Autowired
protected StringRedisTemplate redis;
public void setCacheObj(String value, String key, Object data) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, JSONObject.toJSONString(data));
}
public void zadd(String key, String value, double score) {
redis.opsForZSet().add(key, value, score);
}
public Set<String> rangeByScore(String key, double min, double max) {
Set<String> set = redis.opsForZSet().rangeByScore(key, min, max);
return set;
}
public Long removeRangeByScore(String key, double min, double max) {
Long result = redis.opsForZSet().removeRangeByScore(key, min, max);
return result;
}
public void set(String value, String key, String data) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, data);
}
public void set( String key, String data) {
redis.opsForValue().set(key, data);
}
public void set( String key, String data, long timeout, TimeUnit unit) {
redis.opsForValue().set(key, data,timeout,unit);
}
public void setCacheObj(String value, String key, Object data, long timeout, TimeUnit unit) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, JSONObject.toJSONString(data), timeout, unit);
}
public void set(String value, String key, String data, long timeout, TimeUnit unit) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, data, timeout, unit);
}
public <T> T getCacheObj(String value, String key, Class<T> tClass) {
String storeKey = value + ":" + key;
String result = redis.opsForValue().get(storeKey);
if (StringUtils.isBlank(result)) {
return null;
}
return JSONObject.parseObject(result, tClass);
}
public <T> T getCacheObj(String key, Class<T> tClass) {
String result = redis.opsForValue().get(key);
if (StringUtils.isBlank(result)) {
return null;
}
return JSONObject.parseObject(result, tClass);
}
public String get(String value, String key) {
String storeKey = value + ":" + key;
String result = redis.opsForValue().get(storeKey);
if (null == result) {
return null;
}
return result;
}
public Map<String, String> get(List<String> keys) {
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (int i = 0; i < keys.size(); i++) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
stringRedisConnection.get(keys.get(i));
}
return null;
}
});
Map<String, String> result = new HashMap();
for (int i = 0; i < redisResult.size(); i++) {
if ((redisResult.get(i) == null)) {
result.put(keys.get(i), null);
} else {
result.put(keys.get(i), redisResult.get(i).toString());
}
}
return result;
}
public Map<String, String> get(String value, List<String> keys) {
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (int i = 0; i < keys.size(); i++) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
String storeKey = value + ":" + keys.get(i);
stringRedisConnection.get(storeKey);
}
return null;
}
});
Map<String, String> result = new HashMap();
for (int i = 0; i < redisResult.size(); i++) {
if ((redisResult.get(i) == null)) {
result.put(keys.get(i), null);
} else {
result.put(keys.get(i), redisResult.get(i).toString());
}
}
return result;
}
public Map<String, Boolean> hasKey(String value, List<String> keys) {
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (int i = 0; i < keys.size(); i++) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
String storeKey = value + ":" + keys.get(i);
stringRedisConnection.exists(storeKey);
}
return null;
}
});
Map<String, Boolean> result = new HashMap();
for (int i = 0; i < redisResult.size(); i++) {
Boolean b = null;
if ((redisResult.get(i) == null) || (!Boolean.parseBoolean(redisResult.get(i).toString()))) {
b = Boolean.valueOf(false);
} else {
b = Boolean.valueOf(true);
}
result.put(keys.get(i), b);
}
return result;
}
public String get(String key) {
String result = redis.opsForValue().get(key);
return result;
}
public Long decrement(String value, String key) {
String storeKey = value + ":" + key;
Long result = redis.opsForValue().decrement(storeKey);
return result;
}
public Long decrement(String value, String key, long count) {
String storeKey = value + ":" + key;
Long result = redis.opsForValue().decrement(storeKey, count);
return result;
}
/**
* 设置数组类型 缓存
*
* @param value
* @param key
* @param data
* @param timeout
* @param unit
*/
public void setCacheArray(String value, String key, Object data, long timeout, TimeUnit unit) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, JSONArray.toJSONString(data), timeout, unit);
}
public void setCacheArray(String key, Object data, long timeout, TimeUnit unit) {
redis.opsForValue().set(key, JSONArray.toJSONString(data), timeout, unit);
}
/**
* 设置数组类型 缓存
*
* @param value
* @param key
* @param data
*/
public void setCacheArray(String value, String key, Object data) {
String storeKey = value + ":" + key;
redis.opsForValue().set(storeKey, JSONArray.toJSONString(data));
}
public <T> List<T> getCacheArray(String key, Class<T> tClass) {
String result = redis.opsForValue().get(key);
if (null == result) {
return null;
}
return JSONArray.parseArray(result, tClass);
}
public <T> List<T> getCacheArray(String value, String key, Class<T> tClass) {
String storeKey = value + ":" + key;
String result = redis.opsForValue().get(storeKey);
if (null == result) {
return null;
}
return JSONArray.parseArray(result, tClass);
}
public boolean hasKey( String key) {
Boolean result = redis.hasKey(key);
if (null == result) {
return false;
} else {
return result;
}
}
public boolean hasKey(String value, String key) {
String storeKey = value + ":" + key;
Boolean result = redis.hasKey(storeKey);
if (null == result) {
return false;
} else {
return result;
}
}
public void deleteByPre(String value) {
String storeKey = value;
Set<String> set = redis.keys(storeKey + "*");
redis.delete(set);
}
public Set<String> getByPre(String value) {
String storeKey = value;
Set<String> set = redis.keys(storeKey + "*");
return set;
}
public void zadd(String key, String value, Integer score) {
redis.opsForZSet().add(key, value, score);
}
public void leftPush(String key, String value) {
redis.opsForList().leftPush(key, value);
}
public List<Object> leftPush(final String key, final List<String> values) {
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (int i = 0; i < values.size(); i++) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
stringRedisConnection.lPush(key, values.get(i));
}
return null;
}
});
return redisResult;
}
public String leftPop(String key) {
return redis.opsForList().leftPop(key);
}
public List<String> leftPop(final String key, long length) {
Long size = redis.opsForList().size(key);
if (length > size) {
length = size;
}
final long l = length;
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (int i = 0; i < l; i++) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
stringRedisConnection.lPop(key);
}
return null;
}
});
List<String> result = new ArrayList<>();
for (int i = 0; i < redisResult.size(); i++) {
Object val = redisResult.get(i);
if (val == null) {
continue;
}
result.add(val.toString());
}
return result;
}
public Map<String, Boolean> hasKey(final List<String> keys) {
List<Object> redisResult = this.redis.executePipelined(new RedisCallback() {
public String doInRedis(RedisConnection redisConnection)
throws DataAccessException {
for (String e : keys) {
StringRedisConnection stringRedisConnection = (StringRedisConnection) redisConnection;
stringRedisConnection.exists(e);
}
return null;
}
});
Map<String, Boolean> result = new HashMap();
for (int i = 0; i < redisResult.size(); i++) {
Boolean b = null;
if ((redisResult.get(i) == null) || (!Boolean.parseBoolean(redisResult.get(i).toString()))) {
b = Boolean.valueOf(false);
} else {
b = Boolean.valueOf(true);
}
result.put(keys.get(i), b);
}
return result;
}
/**
* @param key key
* @return
* @Description: 获取自增长值
*/
public Long getIncr(String key) {
RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redis.getConnectionFactory());
Long increment = entityIdCounter.getAndIncrement();
return increment;
}
/**
* @param key key
* @param value 当前值
* @Description: 初始化自增长值
*/
public void setIncr(String key, int value) {
RedisAtomicLong counter = new RedisAtomicLong(key, redis.getConnectionFactory());
counter.set(value);
}
public void delete(String value, String key) {
String storeKey = value + ":" + key;
redis.delete(storeKey);
}
}