256 lines
6.1 KiB
Java
Raw Normal View History

2025-11-17 10:03:05 +08:00
package com.cdzy.operations.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author attiya
* @since 2025-03-20
*/
@Component
public class RedisUtil {
private final RedisTemplate<String, Object> redisTemplate;
@Autowired
public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
/**
* 设置过期时间
* @param key
* @param timeout 超时时间
* @param unit 时间单位
*/
public Boolean expire(String key, long timeout, TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
* 获取过期时间
* @param key
* @param unit 时间单位
*/
public Long getExpire(String key, TimeUnit unit) {
return redisTemplate.getExpire(key, unit);
}
/**
* 判断key是否存在
* @param key
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 删除key
* @param key
*/
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
/* ------------------- String 相关操作 ------------------ */
/**
* 设置键值对
* @param key
* @param value
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 设置键值对并设置过期时间
* @param key
* @param value
* @param timeout 过期时间
* @param unit 时间单位
*/
public void set(String key, Object value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
/**
* 获取值
* @param key
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 自增操作增量=1
* @param key
*/
public Long increment(String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* 自增操作指定增量
* @param key
* @param delta 增量
*/
public Long increment(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/* ------------------- Hash 相关操作 ------------------ */
/**
* 设置Hash键值对
* @param key
* @param hashKey Hash键
* @param value
*/
public void hSet(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 批量设置Hash键值对
* @param key
* @param map 多个Hash键值对
*/
public void hSetAll(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 获取Hash值
* @param key
* @param hashKey Hash键
*/
public Object hGet(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 获取所有Hash键值对
* @param key
*/
public Map<Object, Object> hGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 删除Hash键
* @param key
* @param hashKeys Hash键集合
*/
public Long hDelete(String key, Object... hashKeys) {
return redisTemplate.opsForHash().delete(key, hashKeys);
}
/* ------------------- List 相关操作 ------------------ */
/**
* 左推入元素
* @param key
* @param value
*/
public Long lPush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 右推入元素
* @param key
* @param value
*/
public Long rPush(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* 获取列表范围
* @param key
* @param start 开始索引
* @param end 结束索引
*/
public List<Object> lRange(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/* ------------------- Set 相关操作 ------------------ */
/**
* 添加集合元素
* @param key
* @param values 值数组
*/
public Long sAdd(String key, Object... values) {
return redisTemplate.opsForSet().add(key, values);
}
/**
* 获取集合所有元素
* @param key
*/
public Set<Object> sMembers(String key) {
return redisTemplate.opsForSet().members(key);
}
/* ------------------- ZSet 相关操作 ------------------ */
/**
* 添加有序集合元素
* @param key
* @param value
* @param score 分数
*/
public Boolean zAdd(String key, Object value, double score) {
return redisTemplate.opsForZSet().add(key, value, score);
}
/**
* 获取有序集合范围
* @param key
* @param start 开始索引
* @param end 结束索引
*/
public Set<Object> zRange(String key, long start, long end) {
return redisTemplate.opsForZSet().range(key, start, end);
}
/* ------------------- 其他操作 ------------------ */
/**
* 获取匹配的键
* @param pattern 匹配模式
*/
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 分布式锁尝试获取锁
* @param key 锁键
* @param value 锁值
* @param timeout 超时时间
* @param unit 时间单位
*/
public Boolean tryLock(String key, Object value, long timeout, TimeUnit unit) {
return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit);
}
/**
* 释放锁
* @param key 锁键
*/
public Boolean releaseLock(String key) {
return redisTemplate.delete(key);
}
}