package com.orchard.common.support.redis;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
public class RedisUtil {
@Autowired
RedisTemplate redisTemplate;
public boolean set(String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception var5) {
JSONObject json = new JSONObject();
String jsonStr = key + ":" + value.toString();
json.put("redis设置值异常", jsonStr);
log.info(json.toJSONString());
}
return result;
}
public boolean set(String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
operations.set(key, value);
this.redisTemplate.expire(key, expireTime.longValue(), TimeUnit.SECONDS);
result = true;
} catch (Exception var6) {
JSONObject json = new JSONObject();
String jsonStr = key + ":" + value.toString() + "时间戳-" + expireTime ;
json.put("redis设置时间范围值异常", jsonStr);
log.info(json.toJSONString());
}
return result;
}
public void remove(String... keys) {
String[] var2 = keys;
int var3 = keys.length;
for(int var4 = 0; var4 < var3; ++var4) {
String key = var2[var4];
this.remove(key);
}
}
public void removePattern(String pattern) {
Set<Serializable> keys = this.redisTemplate.keys(pattern);
if(keys.size() > 0) {
this.redisTemplate.delete(keys);
}
}
public void remove(String key) {
if(this.exists(key)) {
this.redisTemplate.delete(key);
}
}
public boolean exists(String key) {
return this.redisTemplate.hasKey(key).booleanValue();
}
public Object get(String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = this.redisTemplate.opsForValue();
try{
result = operations.get(key);
}catch (Exception e ){
log.error(
"从Redis获取数据异常--Date:{},key:{},errorMsg:{}",
new Date(),
key,
e.getMessage());
return null;
}
return result;
}
public void hmSet(String key, Object hashKey, Object value) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
public Object hmGet(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
public Set<Object> hmKeys(String key) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
return hash.keys(key);
}
public Boolean hmhasKey(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
return hash.hasKey(key, hashKey);
}
public void hmDel(String masterKey, Object... hashKey) {
HashOperations<String, Object, Object> hash = this.redisTemplate.opsForHash();
hash.delete(masterKey, hashKey);
}
public void lPush(String k, Object v) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
list.rightPush(k, v);
}
public void lPush(String k, Object v,long timeout) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
this.redisTemplate.expire(k, timeout, TimeUnit.SECONDS);
list.rightPush(k, v);
}
public List<Object> lRange(String k, long l, long l1) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.range(k, l, l1);
}
public Object blpop(String k) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.leftPop(k);
}
public Long lLen(String k) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.size(k);
}
public void add(String key, Object value) {
SetOperations<String, Object> set = this.redisTemplate.opsForSet();
set.add(key, value);
}
public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = this.redisTemplate.opsForSet();
return set.members(key);
}
public void zAdd(String key, Object value, double scoure) {
ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
zset.add(key, value, scoure);
}
public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
ZSetOperations<String, Object> zset = this.redisTemplate.opsForZSet();
return zset.rangeByScore(key, scoure, scoure1);
}
public Object lPop(String k) {
ListOperations<String, Object> list = this.redisTemplate.opsForList();
return list.leftPop(k);
}
public boolean expire(String key, long timeout) {
return this.redisTemplate.expire(key, timeout, TimeUnit.SECONDS).booleanValue();
}
public long ttl(String key) {
return this.redisTemplate.getExpire(key).longValue();
}
/**
* 增量的方式将long值存储在变量中
* @param key
* @param num
* @return
*/
public Long increment(String key,Integer num) {
return this.redisTemplate.opsForValue().increment(key,num);
}
}
redis工具类
最新推荐文章于 2022-10-11 14:13:34 发布