package com.product.datasource.service;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import com.google.common.collect.Maps;
|
import com.product.common.serialization.SerializationUtils;
|
import com.product.core.cache.util.CacheCommonUtil;
|
import com.product.core.exception.BaseException;
|
import com.product.datasource.config.ErrorCode;
|
import com.product.datasource.entity.DataBaseEntity;
|
import org.apache.commons.lang3.StringUtils;
|
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.JedisPool;
|
|
import java.nio.charset.Charset;
|
import java.nio.charset.StandardCharsets;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.stream.Collectors;
|
|
/**
|
* Copyright © 6c
|
*
|
* @Date 2022年07月14日 14:37
|
* @Author 6c
|
* @Description
|
*/
|
public class RedisService20220909 {
|
private Jedis jedis;
|
private String ip;
|
private int port;
|
private String password;
|
private int dbIndex;
|
public static final int CONNECTION_TIMEOUT = 60000;// 连接超时时间
|
public static final int SO_TIMEOUT = 60000;// 读取超时时间
|
private static JedisPool jedisPool;
|
|
public static void main(String[] args) throws InterruptedException {
|
test2();
|
}
|
|
public static void test2() throws InterruptedException {
|
DataBaseEntity dbe = new DataBaseEntity(5);
|
dbe.setIp("10.4.3.78");
|
dbe.setPort("6390");
|
RedisService20220909 redisService = new RedisService20220909(dbe);
|
|
String keyPrefix = "DC_STORE:tt_t_wip_*";
|
String[] keys = redisService.getKeys(keyPrefix);
|
if (keys != null) {
|
redisService.del(keys);
|
}
|
|
keyPrefix = "DC:tt_t_wip_*";
|
keys = redisService.getKeys(keyPrefix);
|
if (keys != null) {
|
redisService.del(keys);
|
}
|
redisService.close();
|
}
|
|
public RedisService20220909(DataBaseEntity dbe) {
|
try {
|
this.ip = dbe.getIp();
|
this.port = Integer.parseInt(dbe.getPort());
|
this.password = dbe.getPassWord();
|
this.dbIndex = StringUtils.isEmpty(dbe.getDbInstance()) ? 0 : Integer.parseInt(dbe.getDbInstance());
|
connect();
|
} catch (Exception e) {
|
throw new BaseException(ErrorCode.GET_REDIS_CONNECTION_FAIL);
|
}
|
}
|
|
/**
|
* 关闭redis连接
|
*/
|
public void close() {
|
if (jedis != null && jedis.isConnected()) {
|
jedis.close();
|
}
|
jedis = null;
|
}
|
|
/**
|
* 连接redis
|
*/
|
public void connect() {
|
if (jedis == null) {
|
try {
|
jedis = new Jedis(ip, port, CONNECTION_TIMEOUT, SO_TIMEOUT);
|
if (!StringUtils.isEmpty(password)) {
|
jedis.auth(password);
|
}
|
jedis.select(dbIndex);
|
jedis.connect();
|
} catch (Exception e) {
|
throw new BaseException(ErrorCode.GET_REDIS_CONNECTION_FAIL);
|
}
|
}
|
}
|
|
/*==================key-start==================*/
|
|
/**
|
* 模糊匹配key
|
*
|
* @param pattern 匹配正则表达式
|
* @return
|
*/
|
public String[] getKeys(String pattern) {
|
if (!StringUtils.isEmpty(pattern)) {
|
connect();
|
Set<byte[]> keyBytes = jedis.keys(pattern.getBytes(StandardCharsets.UTF_8));
|
if (!CollectionUtil.isEmpty(keyBytes)) {
|
return keyBytes.stream().map(item -> new String(item, StandardCharsets.UTF_8)).toArray(String[]::new);
|
}
|
}
|
return null;
|
}
|
|
|
/**
|
* 判定是否存在key
|
*
|
* @param key 关键字
|
* @return 是否存在
|
*/
|
public boolean exists(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return false;
|
}
|
connect();
|
return jedis.exists(key);
|
}
|
|
/**
|
* 删除key
|
*
|
* @param keys 关键字...
|
*/
|
public void del(String... keys) {
|
connect();
|
for (String key : keys) {
|
if (StringUtils.isEmpty(key)) {
|
continue;
|
}
|
jedis.del(key);
|
}
|
}
|
|
/**
|
* 设置超时时间
|
*
|
* @param key 关键字
|
* @param outTime 超时时间,单位:秒
|
* @param serializeFlag 是否序列化
|
* @return
|
*/
|
public boolean setOutTime(String key, int outTime, boolean serializeFlag) {
|
if (StringUtils.isEmpty(key)) {
|
return false;
|
}
|
connect();
|
Long result;
|
if (serializeFlag) {
|
result = jedis.expire(SerializationUtils.serialize(key), outTime);
|
} else {
|
result = jedis.expire(key, outTime);
|
}
|
return result != null && result == 1L;
|
}
|
public void setOutTime(String key, int minOutTime, int maxOutTime, boolean serializeFlag) {
|
if (StringUtils.isEmpty(key)) {
|
return;
|
}
|
if (maxOutTime < minOutTime || maxOutTime <= 0) {
|
return;
|
}
|
int outTime = (int) (Math.random() * (maxOutTime - minOutTime)) + minOutTime;
|
setOutTime(key, outTime,serializeFlag);
|
}
|
|
/**
|
* 移除超时时间
|
*
|
* @param key 关键字
|
*/
|
public void clearOutTime(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return;
|
}
|
connect();
|
jedis.persist(key);
|
}
|
|
/**
|
* 获取指定key的剩余过期时间
|
*
|
* @param key 关键字
|
* @return 剩余过期时间,单位:秒(特殊值如-2:没有这个key;-1:有key单没有设置过期时间,就是不过期)
|
*/
|
public Long getOutTime(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
return jedis.ttl(key);
|
}
|
|
/**
|
* 获取redis中的类型
|
*
|
* @param key 关键字
|
* @return redis的数据类型:String, hash, list, set, zset,若是key不存在,则返回"none"
|
*/
|
public String getType(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
return jedis.type(key);
|
}
|
/*==================key-final==================*/
|
|
/*==================string-start==================*/
|
/**
|
* 新增
|
* @param key 关键字
|
* @param value 值,非null
|
* @param serializeFlag 序列化标识
|
*/
|
public void set(String key, Object value, boolean serializeFlag) {
|
if (StringUtils.isEmpty(key)) {
|
return;
|
}
|
if (value == null) {
|
return;
|
}
|
connect();
|
if (!serializeFlag && value instanceof String) {
|
jedis.set(key, value.toString());
|
} else {
|
jedis.set(SerializationUtils.serialize(key), SerializationUtils.serialize(value));
|
}
|
}
|
|
/**
|
* 获取指定key的value
|
* @param key 关键字
|
* @param serializeFlag 序列化标识
|
* @return 值,不存在key会返回null
|
*/
|
public Object get(String key, boolean serializeFlag) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
if (serializeFlag) {
|
return jedis.get(SerializationUtils.serialize(key)) == null ? null : SerializationUtils.deserialize(jedis.get(SerializationUtils.serialize(key)));
|
} else {
|
return jedis.get(key);
|
}
|
}
|
/*==================string-final==================*/
|
|
/*==================hash-start==================*/
|
|
/**
|
* hash-新增
|
*
|
* @param key 关键字
|
* @param field 字段名
|
* @param value 字段值
|
*/
|
public void setHash(String key, String field, Object value) {
|
if (StringUtils.isEmpty(key)) {
|
return;
|
}
|
if (StringUtils.isEmpty(field)) {
|
return;
|
}
|
if (value == null) {
|
return;
|
}
|
connect();
|
jedis.hset(key.getBytes(StandardCharsets.UTF_8), field.getBytes(StandardCharsets.UTF_8), SerializationUtils.serialize(value));
|
}
|
|
/**
|
* hash-新增
|
*
|
* @param key 关键字
|
* @param map 内容Map
|
*/
|
public void setHash(String key, Map<String, Object> map) {
|
if (StringUtils.isEmpty(key)) {
|
return;
|
}
|
Map<byte[], byte[]> param = Maps.newHashMap();
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
param.put(entry.getKey().getBytes(StandardCharsets.UTF_8), SerializationUtils.serialize(entry.getValue()));
|
}
|
connect();
|
jedis.hmset(key.getBytes(StandardCharsets.UTF_8), param);
|
}
|
|
/**
|
* hash-判定是否存在
|
*
|
* @param key 关键字
|
* @param field 字段名
|
* @return 是否存在
|
*/
|
public boolean existsHash(String key, String field) {
|
if (StringUtils.isEmpty(key)) {
|
return false;
|
}
|
if (StringUtils.isEmpty(field)) {
|
return false;
|
}
|
connect();
|
return jedis.hexists(key.getBytes(StandardCharsets.UTF_8), field.getBytes(StandardCharsets.UTF_8));
|
}
|
|
/**
|
* hash-获取字段值
|
*
|
* @param key 关键字
|
* @param field 字段名
|
* @return 字段值
|
*/
|
public Object getHash(String key, String field) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
if (StringUtils.isEmpty(field)) {
|
return null;
|
}
|
connect();
|
return SerializationUtils.deserialize(jedis.hget(key.getBytes(StandardCharsets.UTF_8), field.getBytes(StandardCharsets.UTF_8)));
|
}
|
|
/**
|
* hash-获取所有字段内容集合
|
*
|
* @param key 关键字
|
* @return 所有字段内容集合
|
*/
|
public Map<String, Object> getHash(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
Map<byte[], byte[]> map = jedis.hgetAll(key.getBytes(StandardCharsets.UTF_8));
|
Map<String, Object> result = Maps.newHashMap();
|
Object field;
|
for (Map.Entry<byte[], byte[]> entry : map.entrySet()) {
|
field = StringUtils.toEncodedString(entry.getKey(), Charset.forName("UTF-8"));
|
result.put(field.toString(), SerializationUtils.deserialize(entry.getValue()));
|
}
|
return result;
|
}
|
|
/**
|
* hash-获取指定key的拥有的字段数
|
*
|
* @param key 关键字
|
* @return 拥有的字段数
|
*/
|
public Long getHashLength(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
return jedis.hlen(key.getBytes(StandardCharsets.UTF_8));
|
}
|
|
/**
|
* hash-获取指定key的字段名集合
|
*
|
* @param key 关键字
|
* @return 字段名集合
|
*/
|
public List<String> getHashFields(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
Set<byte[]> set = jedis.hkeys(key.getBytes(StandardCharsets.UTF_8));
|
if (set != null && set.size() > 0) {
|
return set.stream().collect(Collectors.toList()).stream().map(bytes -> StringUtils.toEncodedString(bytes, Charset.forName("UTF-8"))).collect(Collectors.toList());
|
}
|
return null;
|
}
|
|
/**
|
* hash-获取指定key的所有value集合
|
*
|
* @param key 关键字
|
* @return value集合
|
*/
|
public List<Object> getHashValues(String key) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
return getHashValues(key, Object.class);
|
}
|
|
/**
|
* hash-获取指定key的所有value集合
|
*
|
* @param key 关键字
|
* @return value集合
|
*/
|
public <T> List<T> getHashValues(String key, Class<T> t) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
connect();
|
return CacheCommonUtil.deserializeCollection(jedis.hvals(key.getBytes(StandardCharsets.UTF_8)), t);
|
}
|
|
/**
|
* hash-获取指定key的指定字段内容集合
|
*
|
* @param key 关键字
|
* @param fields 字段名集合
|
* @return 字段内容集合
|
*/
|
public List<Object> getHashAimFieldValues(String key, String... fields) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
return getHashAimFieldValues(key, Object.class, fields);
|
}
|
|
/**
|
* hash-获取指定key的指定字段内容集合
|
*
|
* @param key 关键字
|
* @param fields 字段名集合
|
* @return 字段内容集合
|
*/
|
public <T> List<T> getHashAimFieldValues(String key, Class<T> t, String... fields) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
byte[][] bytes = Arrays.stream(fields).map(field -> field.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
|
connect();
|
return CacheCommonUtil.deserializeCollection(jedis.hmget(key.getBytes(StandardCharsets.UTF_8), bytes), t);
|
}
|
|
/**
|
* hash-删除
|
*
|
* @param key 关键字
|
* @param fields 字段名...
|
* @return 删除个数
|
*/
|
public Long delHash(String key, String... fields) {
|
if (StringUtils.isEmpty(key)) {
|
return null;
|
}
|
byte[][] bytes = Arrays.stream(fields).map(field -> field.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new);
|
connect();
|
return jedis.hdel(key.getBytes(StandardCharsets.UTF_8), bytes);
|
}
|
/*==================hash-final==================*/
|
}
|