一、Redis工具类
package com.utils;
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.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
public boolean set(String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean set(String key, Object value, Long expireTime, TimeUnit timeUnit) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, timeUnit);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void remove(String... keys) {
for (String key : keys) {
remove(key);
}
}
public void removePattern(String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0) {
redisTemplate.delete(keys);
}
}
public void remove(String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
public Object get(String key) {
Object result = null;
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
public void hmSet(String key, Object hashKey, Object value) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.put(key, hashKey, value);
}
public void hmDelete(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
hash.delete(key, hashKey);
}
public Object hmGet(String key, Object hashKey) {
HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
return hash.get(key, hashKey);
}
public Long getHashSize(String key) {
return redisTemplate.opsForHash().size(key);
}
public void lPush(String k, Object v) {
ListOperations<String, Object> list = redisTemplate.opsForList();
list.leftPush(k, v);
}
public Object getRightPop(String k, Long t){
return redisTemplate.opsForList().rightPop(k, t, TimeUnit.SECONDS);
}
public Long getListSize(String k) {
return redisTemplate.opsForList().size(k);
}
public List<Object> lRange(String k, long l, long l1) {
ListOperations<String, Object> list = redisTemplate.opsForList();
return list.range(k, l, l1);
}
public void add(String key, Object value) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add(key, value);
}
public Set<Object> setMembers(String key) {
SetOperations<String, Object> set = redisTemplate.opsForSet();
return set.members(key);
}
public void zAdd(String key, Object value, double score) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add(key, value, score);
}
public Set<Object> rangeByScore(String key, double score, double score1) {
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
return zset.rangeByScore(key, score, score1);
}
}
二、示例
boolean exists = redisUtil.exists("projectorSerial");
if(exists){
String id = redisUtil.get("projectorSerial").toString();
}