import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
@Component
public class RedisUtils {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
public RedisUtils() {
}
@PostConstruct
public void init() {
this.redisTemplate.setKeySerializer(new StringRedisSerializer());
RedisSerializer rs = new Jackson2JsonRedisSerializer(Object.class);
this.redisTemplate.setValueSerializer(rs);
}
public String getString(String key) {
String value = (String)this.stringRedisTemplate.opsForValue().get(key);
return value;
}
public Long incr(String key) {
Long vaue = this.stringRedisTemplate.opsForValue().increment(key);
return vaue;
}
public void setString(String key, String value) {
this.stringRedisTemplate.opsForValue().set(key, value);
}
public void setString(String key, String value, long expire) {
this.stringRedisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
}
public void setString(String key, String value, long expire, TimeUnit timeUnit) {
this.stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
public Boolean isExist(String key) {
Boolean hasKey = this.stringRedisTemplate.hasKey(key);
return hasKey;
}
public void setExpire(String key, long expire) {
this.stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
public void setExpire(String key, long expire, TimeUnit timeUnit) {
this.stringRedisTemplate.expire(key, expire, timeUnit);
}
public Boolean deleteKey(String key) {
Boolean delete = this.stringRedisTemplate.delete(key);
return delete;
}
public void deleteKeyBatch(Collection<String> keys) {
this.stringRedisTemplate.delete(keys);
}
public Boolean setZset(String key, String value, double score) {
Boolean re = this.stringRedisTemplate.opsForZSet().add(key, value, score);
return re;
}
public Long zSetSize(String key) {
Long size = this.stringRedisTemplate.opsForZSet().size(key);
return size;
}
public Double incrZsetScore(String key, String value, double incr) {
Double score = this.stringRedisTemplate.opsForZSet().incrementScore(key, value, incr);
return score;
}
public Boolean zadd(String key, String value, double score) {
Boolean re = this.stringRedisTemplate.opsForZSet().add(key, value, score);
return re;
}
public Long countByScore(String key, double min, double max) {
Long count = this.stringRedisTemplate.opsForZSet().count(key, min, max);
return count;
}
public Double getZsetValueScore(String key, Object value) {
Double score = this.stringRedisTemplate.opsForZSet().score(key, value);
return score;
}
public void removeZsetValue(String key, Object value) {
this.stringRedisTemplate.opsForZSet().remove(key, new Object[]{value});
}
public Set<String> getZsetValueDesc(String key, long start, long end) {
Set<String> values = this.stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
return values;
}
public Set<String> getZsetValueAsc(String key, long start, long end) {
Set<String> values = this.stringRedisTemplate.opsForZSet().range(key, start, end);
return values;
}
public void removeZsetValueByRange(String key, long start, long end) {
this.stringRedisTemplate.opsForZSet().removeRange(key, start, end);
}
public void removeZsetValueByScore(String key, long min, long max) {
this.stringRedisTemplate.opsForZSet().removeRangeByScore(key, (double)min, (double)max);
}
public void setObject(Object key, Object value) {
this.redisTemplate.opsForValue().set(key, value);
}
public void setObject(Object key, Object value, long expire, TimeUnit timeUnit) {
this.redisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
public <T> T getObject(Object key, Class<T> Clazz) {
Object object = this.redisTemplate.opsForValue().get(key);
return object;
}
public void hPut(String key, String hashKey, String value) {
this.redisTemplate.opsForHash().put(key, hashKey, value);
}
public Map<Object, Object> hGetAll(String key) {
return this.redisTemplate.opsForHash().entries(key);
}
public void hPut(String key, String hashKey, Object value) {
this.redisTemplate.opsForHash().put(key, hashKey, value);
}
public void removeHashKey(String key, String hashKey) {
this.redisTemplate.opsForHash().delete(key, new Object[]{hashKey});
}
public <T, K> T luaExecute(RedisScript<T> script, List<Object> keys, Object... args) {
return this.redisTemplate.execute(script, keys, args);
}
public Set<TypedTuple<String>> getZsetTypedTupleDesc(String key, long start, long end) {
Set<TypedTuple<String>> reverseRangeWithScores = this.stringRedisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
return reverseRangeWithScores;
}
public Boolean lock(String key, Integer expire) {
String lockkey = "LOCK_" + key;
if (expire == null) {
expire = 120;
}
Boolean lock = this.stringRedisTemplate.opsForValue().setIfAbsent(lockkey, "1", (long)expire, TimeUnit.SECONDS);
return lock;
}
public Boolean unlock(String key) {
String lockkey = "LOCK_" + key;
Boolean unlock = this.stringRedisTemplate.delete(lockkey);
return unlock;
}
}
Redis工具类,用于统计访问量
最新推荐文章于 2024-04-06 09:25:24 发布