package cn.itrip.util;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* redis 数据缓存工具
*/
@Component
public class RedisUtil {
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> valOpsStr;
@Resource
private RedisTemplate<Object, Object> redisTemplate;
@Resource(name = "redisTemplate")
private ValueOperations<Object, Object> valOpsObj;
/**
* 设置字符串数据
* @param key
* @param val
*/
public void setString(String key, String val){
valOpsStr.set(key, val);
}
/**
* 设置字符串数据
* @param key 键
* @param val 值
* @param expire 有效时间
*/
public void setString(String key, String val, long expire){
valOpsStr.set(key, val, expire, TimeUnit.SECONDS);
}
/**
* 读取字符串数据
* @param key
* @return
*/
public String getString(String key){
return valOpsStr.get(key);
}
/**
* 判断token是否存在
* @param key
* @return
*/
public boolean exists(String key){
String value = getString(key);
if (value == null) return false;
return true;
}
}
|