记录一个redis工具类
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
@Component
public class RedisUtil {
@Autowired
private final RedisTemplate<String, Object> redisTemplate;
private static final int MILLISECOND = 1000;
private static final Long GET_TIME_OUT = 2L;
private static final Long LOCK_TIME_OUT = 30L;
private static final String LOCK_PREFIX = "DCS_LOCK_";
public RedisUtil(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public Boolean lock(String key, String value, Long lockTimeOut) {
return this.redisTemplate.opsForValue().setIfAbsent("DCS_LOCK_" + key, value, lockTimeOut, TimeUnit.SECONDS);
}
public Boolean tryLock(String key, String value, Long lockTimeOut, Long getTimeOut) {
boolean flag = true;
long begin = System.currentTimeMillis();
Boolean lock;
do {
lock = this.lock(key, value, lockTimeOut);
if (!lock) {
try {
Thread.sleep(100L);
} catch (InterruptedException var11) {
var11.printStackTrace();
}
long endTime = System.currentTimeMillis();
if (endTime - begin > getTimeOut * 1000L) {
flag = false;
}
} else {
flag = false;
}
} while(flag);
return lock;
}
public Boolean tryLock(String key) {
return this.tryLock(key, key, LOCK_TIME_OUT, GET_TIME_OUT);
}
public Boolean tryLock(String key, Long getTimeOut) {
return this.tryLock(key, key, LOCK_TIME_OUT, getTimeOut);
}
public Boolean tryLock(String key, String value, Long getTimeOut) {
return this.tryLock(key, value, LOCK_TIME_OUT, getTimeOut);
}
public Boolean tryLock(String key, String value) {
return this.tryLock(key, value, LOCK_TIME_OUT, GET_TIME_OUT);
}
public Boolean unLock(String key) {
return this.redisTemplate.delete("DCS_LOCK_" + key);
}
public long expire(String key) {
return this.redisTemplate.opsForValue().getOperations().getExpire(key);
}
public boolean expire(String key, long time) {
try {
if (time > 0L) {
this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
public Long incr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("递增因子必须大于0");
} else {
return this.redisTemplate.opsForValue().increment(key, delta);
}
}
public Long decr(String key, long delta) {
if (delta < 0L) {
throw new RuntimeException("递减因子必须大于0");
} else {
return this.redisTemplate.opsForValue().increment(key, -delta);
}
}
public Object hGet(String key, String item) {
return this.redisTemplate.opsForHash().get(key, item);
}
public Map<Object, Object> hmGet(String key) {
return this.redisTemplate.opsForHash().entries(key);
}
public boolean hmSet(String key, Map<String, Object> map) {
try {
this.redisTemplate.opsForHash().putAll(key, map);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean hmSet(String key, Map<String, Object> map, long time) {
try {
this.redisTemplate.opsForHash().putAll(key, map);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean hSet(String key, String item, Object value) {
try {
this.redisTemplate.opsForHash().put(key, item, value);
return true;
} catch (Exception var5) {
var5.printStackTrace();
return false;
}
}
public boolean hSet(String key, String item, Object value, long time) {
try {
this.redisTemplate.opsForHash().put(key, item, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var7) {
var7.printStackTrace();
return false;
}
}
public void hDel(String key, Object... item) {
this.redisTemplate.opsForHash().delete(key, item);
}
public boolean hHasKey(String key, String item) {
return this.redisTemplate.opsForHash().hasKey(key, item);
}
public double hIncr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment(key, item, by);
}
public double hDecr(String key, String item, double by) {
return this.redisTemplate.opsForHash().increment(key, item, -by);
}
public Set<Object> sGet(String key) {
try {
return this.redisTemplate.opsForSet().members(key);
} catch (Exception var3) {
var3.printStackTrace();
return null;
}
}
public boolean sHasKey(String key, Object value) {
try {
return this.redisTemplate.opsForSet().isMember(key, value);
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public long sSet(String key, Object... values) {
try {
return this.redisTemplate.opsForSet().add(key, values);
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
public long sSetAndTime(String key, long time, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().add(key, values);
if (time > 0L) {
this.expire(key, time);
}
return count;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
public long sGetSetSize(String key) {
try {
return this.redisTemplate.opsForSet().size(key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
public long setRemove(String key, Object... values) {
try {
Long count = this.redisTemplate.opsForSet().remove(key, values);
return count;
} catch (Exception var4) {
var4.printStackTrace();
return 0L;
}
}
public List<Object> lGet(String key, long start, long end) {
try {
return this.redisTemplate.opsForList().range(key, start, end);
} catch (Exception var7) {
var7.printStackTrace();
return null;
}
}
public long lGetListSize(String key) {
try {
return this.redisTemplate.opsForList().size(key);
} catch (Exception var3) {
var3.printStackTrace();
return 0L;
}
}
public Object lGetIndex(String key, long index) {
try {
return this.redisTemplate.opsForList().index(key, index);
} catch (Exception var5) {
var5.printStackTrace();
return null;
}
}
public boolean lSet(String key, Object value) {
try {
this.redisTemplate.opsForList().rightPush(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean lSet(String key, Object value, long time) {
try {
this.redisTemplate.opsForList().rightPush(key, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value) {
try {
this.redisTemplate.opsForList().rightPushAll(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean lSet(String key, List<Object> value, long time) {
try {
this.redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0L) {
this.expire(key, time);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public boolean lUpdateIndex(String key, long index, Object value) {
try {
this.redisTemplate.opsForList().set(key, index, value);
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
public long lRemove(String key, long count, Object value) {
try {
Long remove = this.redisTemplate.opsForList().remove(key, count, value);
return remove;
} catch (Exception var6) {
var6.printStackTrace();
return 0L;
}
}
public long getExpire(String key) {
return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
public boolean hasKey(String key) {
try {
Boolean hasKey = this.redisTemplate.hasKey(key);
return null != hasKey && hasKey;
} catch (Exception var3) {
var3.printStackTrace();
return false;
}
}
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
this.redisTemplate.delete(key[0]);
} else {
this.redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
public Object getObject(String key) {
return key == null ? null : this.redisTemplate.opsForValue().get(key);
}
public String get(String key) {
return key == null ? null : (String)this.redisTemplate.opsForValue().get(key);
}
public boolean set(String key, Object value) {
try {
this.redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception var4) {
var4.printStackTrace();
return false;
}
}
public boolean set(String key, Object value, long time) {
try {
if (time > 0L) {
this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
this.set(key, value);
}
return true;
} catch (Exception var6) {
var6.printStackTrace();
return false;
}
}
}