package com.stylefeng.guns.modular.system.utils;
|
|
import com.stylefeng.guns.core.util.ToolUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.JedisPool;
|
|
import java.util.Objects;
|
|
|
/**
|
* Redis工具类
|
*/
|
@Component
|
public class RedisUtil {
|
|
|
@Autowired
|
private JedisPool jedisPool;
|
|
|
/**
|
* 向redis中存储字符串没有过期时间
|
* @param key
|
* @param value
|
*/
|
public void setStrValue(String key, String value){
|
if(Objects.nonNull(key)){
|
Jedis resource = jedisPool.getResource();
|
resource.auth("123456");
|
String set = resource.set(key, value);
|
resource.close();
|
}
|
}
|
|
|
/**
|
* 以分钟为单位设置存储值(设置过期时间)
|
* @param key
|
* @param value
|
* @param time 秒
|
*/
|
public void setStrValue(String key, String value, int time){
|
if(ToolUtil.isNotEmpty(key)){
|
Jedis resource = jedisPool.getResource();
|
String setex = resource.setex(key, time, value);
|
resource.close();
|
}
|
}
|
|
|
/**
|
* 从redis中获取值
|
* @param key
|
* @return
|
*/
|
public String getValue(String key){
|
if(ToolUtil.isNotEmpty(key)){
|
Jedis resource = jedisPool.getResource();
|
resource.auth("123456");
|
String data = resource.get(key);
|
resource.close();
|
return data;
|
}
|
return null;
|
}
|
|
|
/**
|
* 删除key
|
* @param key
|
*/
|
public void remove(String key){
|
if(ToolUtil.isNotEmpty(key)){
|
Jedis resource = jedisPool.getResource();
|
resource.auth("123456");
|
Long del = resource.del(key);
|
resource.close();
|
}
|
}
|
}
|