Files
aida-buyer/src/main/java/com/aida/buyer/util/RedisUtil.java
litianxiang ac7de27099 first
2026-05-15 15:09:21 +08:00

93 lines
2.6 KiB
Java

package com.aida.buyer.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
public Boolean setWithExpire(String key, Object value, long time) {
return redisTemplate.opsForValue().setIfAbsent(key, value, time, TimeUnit.SECONDS);
}
public Boolean setWithExpire(String key, Object value, long time, TimeUnit timeUnit) {
return redisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
}
public Boolean increment(String key) {
return redisTemplate.opsForValue().increment(key) != null;
}
public Long increment(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
public Long decrement(String key) {
return redisTemplate.opsForValue().decrement(key);
}
public Long decrement(String key, long delta) {
return redisTemplate.opsForValue().decrement(key, delta);
}
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
public Long delete(Collection<String> keys) {
return redisTemplate.delete(keys);
}
public void hSet(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
public Object hGet(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
public Map<Object, Object> hGetAll(String key) {
return redisTemplate.opsForHash().entries(key);
}
public Boolean hDelete(String key, Object... hashKeys) {
return redisTemplate.opsForHash().delete(key, hashKeys) > 0;
}
public Long hIncrement(String key, String hashKey, long delta) {
return redisTemplate.opsForHash().increment(key, hashKey, delta);
}
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
public Boolean expire(String key, long time) {
return redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public Collection<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
}