package com.my.config.redis;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Slf4j
@Service
public class RedisService {
@Autowired
private RedisTemplate<String,String> redisTemplate;
public boolean expire(String key,long time){
try{
if (time > 0){
redisTemplate.expire(key,time,TimeUnit.SECONDS);
}
return true;
}catch (Exception e){
log.error("Redis设置过期时间失败:"+e);
return false;
}
}
public long getExpire(String key){
return redisTemplate.getExpire(key,TimeUnit.SECONDS);
}
public boolean exists(String key){
return redisTemplate.hasKey(key);
}
public boolean remove(String... keys){
try {
for (int i = 0 ;i < keys.length;i++){
redisTemplate.delete(keys[i]);
}
return true;
}catch (Exception e){
log.error("Redis删除Key失败:"+e);
return false;
}
}
public boolean set(String key,String value){
try{
redisTemplate.opsForValue().set(key,value);
return true;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public boolean setObj(String key,Object value){
try{
redisTemplate.opsForValue().set(key, JSON.toJSONString(value));
return true;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public boolean set(String key,String value,Long expireTime){
try {
redisTemplate.opsForValue().set(key,value,expireTime,TimeUnit.SECONDS);
return true;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public String get(String key){
return key == null ? null : redisTemplate.opsForValue().get(key);
}
public Object getObj(String key){
return key == null ? null : redisTemplate.opsForValue().get(key);
}
public String hget(String key,String field){
return key == null ? null : (String) redisTemplate.opsForHash().get(key, field);
}
public Map<Object,Object> hget(String key){
return key == null ? null : redisTemplate.opsForHash().entries(key);
}
public boolean hset(String key, String field, String value) {
try {
redisTemplate.opsForHash().put(key, field, value);
return true;
} catch (Exception e) {
log.error("Redis保存数据失败:"+e);
return false;
}
}
public boolean hmset(String key,Map<String,Object> map){
try {
redisTemplate.opsForHash().putAll(key,map);
return true;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public boolean hmset(String key,Map<String,Object> map,long time){
try {
redisTemplate.opsForHash().putAll(key,map);
redisTemplate.expire(key,time,TimeUnit.SECONDS);
return true;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public boolean hset(String key, String field, String value,long time) {
try {
redisTemplate.opsForHash().put(key, field, value);
if (time > 0){
redisTemplate.expire(key,time,TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
log.error("Redis保存数据失败:"+e);
return false;
}
}
public void hdel(String key,String... fields){
redisTemplate.opsForHash().delete(key,fields);
}
public boolean hHasKey(String key ,String filed){
return redisTemplate.opsForHash().hasKey(key,filed);
}
public boolean sadd(String key,String... value){
try {
return redisTemplate.opsForSet().add(key,value) > 0 ? true : false;
}catch (Exception e){
log.error("Redis保存数据失败:"+e);
return false;
}
}
public Set<String> members(String key){
return redisTemplate.opsForSet().members(key);
}
public boolean isMember(String key,String value){
return redisTemplate.opsForSet().isMember(key,value);
}
public void srem(String key,String... value){
redisTemplate.opsForSet().remove(key,value);
}
}