两个java文件
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.oruit.common.utils.cache.redis;
import com.oruit.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanResult;
import java.util.*;
import java.util.Map.Entry;
@Slf4j
public class RedisUtil {
/**
* 成功,"OK"
*/
private static final String SUCCESS_OK = "OK";
/**
* 成功,1L
*/
private static final Long SUCCESS_STATUS_LONG = 1L;
/**
* 只用key不存在时才设置。Only set the key if it does not already exist
*/
private static final String NX = "NX";
/**
* XX -- 只有key存在时才设置。和NX相反。Only set the key if it already exist.
*/
private static final String XX = "XX";
/**
* EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
*/
private static final String EX = "EX";
/**
* EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds
*/
private static final String PX = "PX";
protected RedisUtil() {
}
/**
* 成功返回true
*
* @param key
* @param value
* @return
*/
public static boolean set(String key, String value) {
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
String statusCode = jedis.set(key, value);
if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
return true;
}
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return false;
}
/**
* 返回值
*
* @param key
* @return
*/
public static String get(String key) {
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
return jedis.get(key);
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return null;
}
/**
* 设置key值和过期时间
*
* @param key
* @param value
* @param seconds 秒数,不能小于0
* @return
*/
public static boolean setByTime(String key, String value, int seconds) {
if (seconds < 0) {
return false;
}
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
String statusCode = jedis.setex(key, seconds, value);
if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
return true;
}
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return false;
}
/**
* @param key
* @param value
* @param nxxx NX|XX 是否存在
* <li>NX -- Only set the key if it does not already exist.</li>
* <li>XX -- Only set the key if it already exist.</li>
* @param expx EX|PX, expire time units ,时间单位格式,秒或毫秒
* <li>EX = seconds;</li>
* <li>PX = milliseconds</li>
* @param time expire time in the units of expx,时间(long型),不能小于0
* @return
*/
public static boolean set(String key, String value,
String nxxx, String expx, long time) {
if (time < 0) {
return false;
}
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
String statusCode = jedis.set(key, value, nxxx, expx, time);
if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
return true;
}
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return false;
}
/**
* 设置key
*
* @param key
* @param value
* @param nxxx NX|XX 是否需要存在
* <li>NX -- Only set the key if it does not already exist.</li>
* <li>XX -- Only set the key if it already exist.</li>
* @return
*/
public static boolean set(String key, String value,
String nxxx) {
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
String statusCode = jedis.set(key, value, nxxx);
if (SUCCESS_OK.equalsIgnoreCase(statusCode)) {
return true;
}
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return false;
}
/**
* 当key不存在时,设置值,成功返回true
*
* @param key
* @param value
* @return
*/
public static boolean setIfNotExists(String key, String value) {
Jedis jedis = null;
try {
jedis = RedisPoolUtil.getJedis();
Long statusCode = jedis.setnx(key, value);
if (Objects.equals(SUCCESS_STATUS_LONG, statusCode)) {
return true;
}
} catch (Exception e) {
} finally {
if (jedis != null) {
RedisPoolUtil.returnResource(jedis);
}
}
return false;
}
/**
* 当key不存在时,设置值,成功返回true,同setIfNotExists
*
* @param key
* @param value
* @return
*/
public static bo