package leyan.website.util;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Component
@RequiredArgsConstructor
public class RedisUtils {
/**
* 日志对象
*/
private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
private static final Random random = new Random();
@Autowired
@Qualifier("redisCartTemplate")
private final RedisTemplate<String, String> redisTemplate;
@Autowired
@Qualifier("redisCacheTemplate")
private final RedisTemplate<String, String> redisTemplateCache;
/**
* 读取缓存
*/
public String get(final String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 读取缓存并续期
*/
public String get(final String key, long timeout) {
asyncExpire(List.of(key),timeout);
return redisTemplate.opsForValue().get(key);
}
/**
* 批量读取缓存
*
* @param keys
* @return
*/
public List<String> mGet(final Collection<String> keys) {
if (CollectionUtils.isEmpty(keys)) {
return new ArrayList<>();
}
return redisTemplate.opsForValue().multiGet(keys);
}
/**
* 批量读取缓存
*
* @param keys
* @return
*/
public List<String> mGet(final Collection<String> keys, long timeout) {
if (CollectionUtils.isEmpty(keys)) {
return new ArrayList<>();
}
List<String> values = redisTemplate.opsForValue().multiGet(keys);
asyncExpire(keys,timeout);
return values;
}
/**
* 写入缓存
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 批量写入缓存
*/
public void mSet(final Map<String, String> keyValue, long timeout) {
if (CollectionUtils.isEmpty(keyValue)) {
return;
}
redisTemplate.opsForValue().multiSet(keyValue);
for (String key : keyValue.keySet()) {
redisTemplate.expire(key, timeout + random.nextInt(15), TimeUnit.SECONDS); //随机增加0-15秒
}
}
/**
* 异步批量写入缓存
*/
@Async
public void asyncMSet(final Map<String, String> keyValue, long timeout) {
if (CollectionUtils.isEmpty(keyValue)) {
return;
}
redisTemplate.opsForValue().multiSet(keyValue);
for (String key : keyValue.keySet()) {
redisTemplate.expire(key, timeout + random.nextInt(15), TimeUnit.SECONDS); //随机增加0-15秒
}
}
/**
* 更新缓存
*/
public boolean getAndSet(final String key, String value) {
boolean result = false;
try {
redisTemplate.opsForValue().getAndSet(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 删除缓存
*/
public boolean delete(final String key) {
boolean result = false;
try {
if (key != null && key.length() > 0) {
redisTemplate.delete(key);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 指定缓存失效时间
*/
public void expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
} catch (Exception e) {
logger.error("RedisUtils expire(String key,long time) failure." + e.getMessage());
}
}
/**
* 指定缓存失效时间
*/
@Async
public void asyncExpire(Collection<String> keys, long time) {
try {
if (time > 0) {
for (String key : keys) {
redisTemplate.expire(key, time + random.nextInt(15), TimeUnit.SECONDS);
}
}
} catch (Exception e) {
logger.error("RedisUtils expire(String key,long time) failure." + e.getMessage());
}
}
/**
* 将list放入缓存
*/
public void listSet(String key, String value, long time) {
try {
redisTemplate.opsForList().rightPush(key, value);
if (time > 0) {
expire(key, time);
}
} catch (Exception e) {
logger.error("RedisUtils lSet(String key, Object value, long time) failure." + e.getMessage());
}
}
/**
* 根据索引修改list中的某条数据
*/
public void listUpdateIndex(String key, long index, String value) {
try {
redisTemplate.opsForList().set(key, index, value);
} catch (Exception e) {
logger.error("RedisUtils lUpdateIndex(String key, long index,Object value) failure." + e.getMessage());
}
}
/**
* 获取list缓存的内容
*/
public List<String> listGet(String key, long start, long end) {
try {
return redisTemplate.opsForList().range(key, start, end);
} catch (Exception e) {
logger.error("RedisUtils lGet(String key, long start, long end) failure." + e.getMessage());
return null;
}
}
/**
* 移除N个值为value
*/
public long deleteList(String key, long count, Object value) {
try {
return redisTemplate.opsForList().remove(key, count, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
/**
* HashSet 并设置时间
*/
public boolean setHash(String key, Map<String, Object> map, long time) {
try {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 获取hashKey对应的所有键值
*/
public Map<Object, Object> getHash(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* HashGet
*/
public Object getOneHash(String key, String item) {
return redisTemplate.opsForHash().get(key, item);
}
/**
* 删除hash表中的值
*/
public void deleteHash(String key, Object... item) {
redisTemplate.opsForHash().delete(key, item);
}
/**
* 判断redis是否存在
*/
public boolean exitsKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 判断key集合在redis存在的数量
*/
public Long exitsKeys(Collection<String> keys) {
return redisTemplate.countExistingKeys(keys);
}
/**
* 删除redis 2号库(分类与页面信息缓存)
*/
public void deleteRedisCache() {
Set<String> keys = redisTemplateCache.keys("*");
redisTemplateCache.delete(keys);
}
}
Redis工具类(批量异步同步)
于 2023-05-16 17:45:01 首次发布