Redis存储与取出对象
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
@Log4j2
public class RedisServiceImpl{
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setStr(String key, String value) {
setStr(key, value, null);
}
public void setStr(String key, Object value, Long time){
if(value == null){
return;
}
if(value instanceof String){
String obj = (String) value;
stringRedisTemplate.opsForValue().set(key, obj);
}else if(value instanceof List){
List obj = (List) value;
stringRedisTemplate.opsForList().leftPushAll(key,obj);
}else if(value instanceof Map){
Map obj = (Map) value;
stringRedisTemplate.opsForHash().putAll(key,obj);
}
if (time != null)
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}
public Object getKey(String key){
return stringRedisTemplate.opsForValue().get(key);
}
public void delKey(String key) {
stringRedisTemplate.delete(key);
}
public void setKeyOutTime(String key,Long value) {
stringRedisTemplate.expire(key,value,TimeUnit.SECONDS);
}
}