import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import javax.annotation.PostConstruct;
import java.util.Iterator;
import java.util.Set;
/**
* <p>redis工具类</p>
*
* @author admin
*/
@Component
public final class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
/**
* Redis服务器IP
*/
@Value("${redis.server.host}")
private String ADDR;
/**
* Redis的端口号
*/
@Value("${redis.server.port}")
private String PORT;
/**
* Redis的验证密码
*/
@Value("${redis.server.auth}")
private String AUTH;
@Value("${redis.pool.maxIdle}")
private String MAX_IDLE;
@Value("${redis.pool.maxWaitMillis}")
private String TIMEOUT;
private boolean TEST_ON_BORROW = true;
private JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
@PostConstruct
public void init() {
try {
JedisPoolConfig config = new JedisPoolConfig();
// config.setMaxActive(MAX_ACTIVE);
config.setMaxIdle(Integer.parseInt(MAX_IDLE));
// config.setMaxWait(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR, Integer.parseInt(PORT), Integer.parseInt(TIMEOUT), AUTH);
logger.info("redis init success");
} catch (Exception e) {
logger.error("init redis error", e);
}
}
/**
* 获取Jedis实例
*
* @return redis一个实例
*/
public Jedis getJedis() {
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
return null;
}
} catch (Exception e) {
logger.error("get jedis error", e);
return null;
}
}
/**
* 获取Jedis实例并且切换到那个指定的db库进行批量删除
*
* @return long 被删除的数量
*/
public Long batchDelJedisdb(int index, String key) {
Jedis resource = null;
try {
long ret = 0;
resource = jedisPool.getResource();
resource.select(index);
Set<String> set = resource.keys(key);
logger.info("redis已切换到db{},根据{},模糊匹配结果:{}", index, key, JSON.toJSONString(set));
if (!CollectionUtils.isEmpty(set)) {
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
String keyStr = iterator.next();
logger.info("删掉的key:{}", keyStr);
ret = ret + resource.del(keyStr);
}
}
return ret;
} catch (Exception e) {
logger.error("delItemKey error", e);
returnResource(resource);
} finally {
returnResource(resource);
}
return null;
}
/**
* 释放jedis资源
*
* @param jedis jedis
*/
public void returnResource(Jedis jedis) {
if (jedis != null) {
jedis.close();
logger.info("redis连接已关闭。");
}
}
/**
* 往redis里填充设置数据
*
* @param itemCode key
* @param itemString value
* @return 返回OK
*/
public String setItemKey(String itemCode, String itemString) {
logger.info("setItemKey {}", itemCode);
if (StringUtils.isBlank(itemCode) || StringUtils.isBlank(itemString)) {
return null;
}
Jedis jedis = getJedis();
try {
return jedis.setex(itemCode, 3600 * 24, itemString);
} catch (Exception e) {
logger.error("setItemKey error", e);
returnResource(jedis);
} finally {
returnResource(jedis);
}
return null;
}
/**
* 通过key获取redis里面的value
*
* @param key 需要传入的key
* @return 返回key对应的value
*/
public String getItemKey(String key) {
logger.info("getItemKey {}", key);
if (StringUtils.isBlank(key)) {
return null;
}
Jedis jedis = getJedis();
try {
String ret = jedis.get(key);
return ret;
} catch (Exception e) {
logger.error("getItemKey error", e);
returnResource(jedis);
} finally {
returnResource(jedis);
}
return null;
}
/**
* 删除redis里面的item
*
* @param key 需要删除的键
* @return 影响条数
*/
public Long delItemKey(String key) {
logger.info("delItemKey {}", key);
if (StringUtils.isBlank(key)) {
return null;
}
Jedis jedis = getJedis();
try {
Long ret = jedis.del(key);
return ret;
} catch (Exception e) {
logger.error("delItemKey error", e);
returnResource(jedis);
} finally {
returnResource(jedis);
}
return null;
}
/**
* 用于判断redis中是否存在key
*
* @param key 需要判断的key
* @return boolean
*/
public Boolean existsKey(String key) {
logger.info("delItemKey {}", key);
if (StringUtils.isBlank(key)) {
return false;
}
Jedis jedis = getJedis();
try {
Boolean ret = jedis.exists(key);
return ret;
} catch (Exception e) {
logger.error("existKey error", e);
returnResource(jedis);
} finally {
returnResource(jedis);
}
return null;
}
/**
* redis 模糊匹配获取key数组
*
* @param pattern 匹配模式 *,?,[]三种
* @return Set
*/
public Set<String> keys(String pattern) {
Jedis jedis = getJedis();
try {
logger.info("开始进行模糊匹配:{}", pattern);
Set<String> set = jedis.keys(pattern);
return set;
} catch (Exception e) {
logger.error("redis 模糊匹配出现异常:", e);
returnResource(jedis);
return null;
} finally {
returnResource(jedis);
}
}
}