项目中如果需要临时保存一个变量,可以直接保存在redis中,这样下次要调用的时候就特别方便了。
redis的使用莫非就是set get delete这几个。
1、首先在数据保存的时候,需要注意三点:
(1)定义一下redis的前缀
(2)找寻一个唯一的值作为key
(3)设置失效时间,同时要注意失效时间的单位
具体如下:
private static final String CANCEL_PREX = "global_mall_cancel_";
//serialNo保存到redis中
JedisUtils.set(CANCEL_PREX + req.getUserId(), serialNo, 30000);
2、在数据获取的时候
//从redis中获取到serialNo
String serialNo = JedisUtils.get(CANCEL_PREX + req.getUserId());
3、删除数据,如果此数据只是为这个业务使用,处理完业务就没有用了,可以删除,减少redis的脏数据。
//处理完之后,删除redis中的值
JedisUtils.del(CANCEL_PREX + req.getUserId());
JedisUtils的配置如下,当然redis中pom等配置省略了。
@Slf4j
public class JedisUtils {
private static JedisPool jedisPool;
private final static String KEY_PREFIX = JedisConfig.APP_PREFIX;
static {
try {
jedisPool = SpringContextUtil.getBean(JedisPool.class);
} catch (Exception e) {
throw new RuntimeException("请检查applicationContext-cache.xml是否启用");
}
}
private static String addKeyPrefix(String key){
if(StringUtils.isNotEmpty(KEY_PREFIX)){
return KEY_PREFIX + ":" + key;
}
return key;
}
/**
* 获取缓存
* @param key 键
* @return 值
*/
public static String get(String key) {
String value = null;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
if (jedis.exists(key)) {
value = jedis.get(key);
value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;
//log.info("get {} = {}", key, value);
}
} catch (Exception e) {
log.warn("get {} = {}", key, value, e);
} finally {
returnResource(jedis);
}
return value;
}
/**
* 设置缓存
* @param key 键
* @param value 值
* @param cacheSeconds 超时时间,0为不超时
* @return
*/
public static String set(String key, String value, int cacheSeconds) {
String result = null;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.set(key, value);
if (cacheSeconds != 0) {
jedis.expire(key, cacheSeconds);
}
//log.info("set {} = {}", key, value);
} catch (Exception e) {
log.warn("set {} = {}", key, value, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 自增
* @param key 键
* @return
*/
public static Long incr(String key) {
Long result = null;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.incr(key);
log.info("incr {} ", key);
} catch (Exception e) {
log.warn("incr {} ", key);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 设置缓存过期时间
* @param key 键
* @param cacheSeconds 超时时间 单位(秒)
* @return
*/
public static Long setCacheSeconds(String key, int cacheSeconds) {
Long result = null;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.expire(key, cacheSeconds);
log.info("expire {} = {} ", key, cacheSeconds);
} catch (Exception e) {
log.warn("expire {} = {} ", key, cacheSeconds);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 判断Map缓存中的Key是否存在
* @param key 键
* @param mapKey 值
* @return
*/
public static boolean mapObjectExists(String key, String mapKey) {
boolean result = false;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));
//log.info("mapObjectExists {} {}", key, mapKey);
} catch (Exception e) {
log.warn("mapObjectExists {} {}", key, mapKey, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 删除缓存
* @param key 键
* @return
*/
public static long del(String key) {
long result = 0;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
if (jedis.exists(key)){
result = jedis.del(key);
//log.info("del {}", key);
}else{
//log.info("del {} not exists", key);
}
} catch (Exception e) {
log.warn("del {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 删除缓存
* @param key 键
* @return
*/
public static long delObject(String key) {
long result = 0;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
if (jedis.exists(getBytesKey(key))){
result = jedis.del(getBytesKey(key));
//log.info("delObject {}", key);
}else{
//log.info("delObject {} not exists", key);
}
} catch (Exception e) {
log.warn("delObject {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 缓存是否存在
* @param key 键
* @return
*/
public static boolean exists(String key) {
boolean result = false;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.exists(key);
log.info("exists {}", key);
} catch (Exception e) {
log.warn("exists {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 缓存是否存在
* @param key 键
* @return
*/
public static boolean existsObject(String key) {
boolean result = false;
Jedis jedis = null;
key = addKeyPrefix(key);
try {
jedis = getResource();
result = jedis.exists(getBytesKey(key));
//log.info("existsObject {}", key);
} catch (Exception e) {
log.warn("existsObject {}", key, e);
} finally {
returnResource(jedis);
}
return result;
}
/**
* 获取资源
* @return
* @throws JedisException
*/
public static Jedis getResource() throws JedisException {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
// //log.info("getResource.", jedis);
} catch (JedisException e) {
log.warn("getResource.", e);
returnBrokenResource(jedis);
throw e;
}
return jedis;
}
/**
* 归还资源
* @param jedis
*/
public static void returnBrokenResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnBrokenResource(jedis);
}
}
/**
* 释放资源
* @param jedis
*/
public static void returnResource(Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}