package com.product.datasource.service; import cn.hutool.core.collection.CollectionUtil; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import com.google.common.collect.Sets; import com.product.common.serialization.SerializationUtils; import com.product.core.cache.util.CacheCommonUtil; import com.product.core.cache.util.RedisUtil; import com.product.core.exception.BaseException; import com.product.datasource.config.ErrorCode; import com.product.datasource.entity.DataBaseEntity; import com.product.datasource.utils.RedisSentinelUtil; import org.apache.commons.lang3.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisSentinelPool; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; /** * Copyright © 6c * * @Date 2022年07月14日 14:37 * @Author 6c * @Description */ public class RedisService { private JSONObject curObj; private static final int CONNECTION_TIMEOUT = 60000;// 连接超时时间 private static final int SO_TIMEOUT = 60000;// 读取超时时间 private boolean localFlag; private static Map map = Maps.newHashMap(); private static Map sentinelPoolMap = Maps.newHashMap(); private static Map redisSentinelUtil = Maps.newHashMap(); public RedisService(DataBaseEntity dbe) { try { curObj = new JSONObject(); curObj.put("ip", dbe.getIp()); curObj.put("port", dbe.getPort()); curObj.put("password", dbe.getPassWord()); curObj.put("dbIndex", StringUtils.isEmpty(dbe.getDbInstance()) ? 0 : Integer.parseInt(dbe.getDbInstance())); curObj.put("db_type", dbe.getDbTypeByInt()); curObj.put("user_name", dbe.getUserName()); } catch (Exception e) { throw new BaseException(ErrorCode.GET_REDIS_CONNECTION_FAIL); } } public RedisService() { this.localFlag = true; } public void close() { if (curObj != null) { curObj = null; } } public static void destory() { map.forEach((k, v) -> { v.close(); }); } /** * 获取jedis * * @return Jedis */ public Jedis getJedis() { if (localFlag) { return RedisUtil.getJedis(); } String dbType = curObj.getString("db_type"); Jedis jedis; if ("6".equals(dbType)) { jedis = getSentinelPool().getResource(); } else { jedis = getJedisPool().getResource(); } jedis.select(curObj.getIntValue("dbIndex")); return jedis; } /** * 获取redis连接 * * @param readOnly 只读 * @return */ public Jedis getJedis(boolean readOnly) { // if (false && "6".equals(curObj.getString("db_type")) && readOnly) { RedisSentinelUtil redisSentinelUtil = this.redisSentinelUtil.get(this.curObj); if (redisSentinelUtil == null) { this.getSentinelPool(); redisSentinelUtil = this.redisSentinelUtil.get(this.curObj); } Jedis jedis = redisSentinelUtil.getResource(); jedis.select(curObj.getIntValue("dbIndex")); return jedis; } else { return getJedis(); } } /** * 主从哨兵获取jedis * * @return */ private JedisSentinelPool getSentinelPool() { JedisSentinelPool jedisPool = sentinelPoolMap.get(curObj); if (jedisPool == null) { synchronized (curObj.toJSONString().intern()) { if (sentinelPoolMap.containsKey(curObj)) { jedisPool = sentinelPoolMap.get(curObj); } if (jedisPool == null) { //哨兵模式 String[] ip = curObj.getString("ip").split(","); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig<>(); //线程池最大数量 poolConfig.setMaxIdle(100); //等待时间 poolConfig.setMaxWaitMillis(60000); poolConfig.setTestOnBorrow(true); int connectionTimeout = 5000; int soTimeout = 5000; String password = null; int database = 0; HashSet sentinels = Sets.newHashSet(ip); jedisPool = new JedisSentinelPool(curObj.getString("user_name"), sentinels, poolConfig, connectionTimeout, soTimeout, password, database); sentinelPoolMap.put(curObj, jedisPool); redisSentinelUtil.put(curObj, new RedisSentinelUtil(sentinels, jedisPool, poolConfig, curObj.getString("user_name"))); } } } return jedisPool; } /** * 单机获取 * * @return */ private JedisPool getJedisPool() { JedisPool jedisPool = map.get(curObj); if (jedisPool == null) { synchronized (curObj.toJSONString().intern()) { if (!map.containsKey(curObj)) { // 设置配置 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxTotal(100);// 最大连接数 jedisPoolConfig.setMaxIdle(20);// 最大空闲数 jedisPoolConfig.setMaxWaitMillis(CONNECTION_TIMEOUT); jedisPoolConfig.setTestOnBorrow(true); jedisPoolConfig.setTestOnReturn(true); // 初始化JedisPool jedisPool = new JedisPool( jedisPoolConfig, curObj.getString("ip"), curObj.getIntValue("port"), SO_TIMEOUT, StringUtils.isEmpty(curObj.getString("password")) ? null : curObj.getString("password")); map.put(curObj, jedisPool); } else { jedisPool = map.get(curObj); } } } // Jedis jedis = jedisPool.getResource(); // jedis.select(curObj.getIntValue("dbIndex")); return jedisPool; } /*==================key-start==================*/ public static void main(String[] args) throws InterruptedException { } public static void test2() throws InterruptedException { DataBaseEntity dbe = new DataBaseEntity(5); dbe.setIp("10.4.3.78"); dbe.setPort("6390"); RedisService redisService = new RedisService(dbe); if (redisService.existsHash("DC:" + ("TT_T_WIP_REPAIR").toLowerCase() + ":4", "6a662ff2-c124-40ef-950f-4d1329ec624b")) { redisService.delHash("DC:" + ("TT_T_WIP_REPAIR").toLowerCase() + ":4", "6a662ff2-c124-40ef-950f-4d1329ec624b"); System.out.println("清除"); } } /** * 模糊匹配key * * @param pattern 匹配正则表达式 * @return */ public String[] getKeys(String pattern, boolean flag) { if (!StringUtils.isEmpty(pattern)) { try (Jedis jedis = getJedis(true)) { if (flag) { Set 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); } } else { Set keys = jedis.keys(pattern); if (!CollectionUtil.isEmpty(keys)) { return keys.toArray(new String[0]); } } } } return null; } public String[] getKeys(String pattern) { return getKeys(pattern, true); } /** * 判定是否存在key * * @param key 关键字 * @return 是否存在 */ public boolean exists(String key) { if (StringUtils.isEmpty(key)) { return false; } try (Jedis jedis = getJedis(true)) { return jedis.exists(key); } } /** * 删除key * * @param keys 关键字... */ public void del(String... keys) { try (Jedis jedis = getJedis(false)) { for (String key : keys) { if (StringUtils.isEmpty(key)) { continue; } jedis.del(key); } } } /** * 删除key * * @param keys 关键字... */ public void del(boolean serializeFlag, String... keys) { try (Jedis jedis = getJedis(false)) { if (!serializeFlag) { del(keys); } for (String key : keys) { if (StringUtils.isEmpty(key)) { continue; } if (serializeFlag) { jedis.del(SerializationUtils.serialize(key)); } } } } /** * 设置超时时间 * * @param key 关键字 * @param outTime 超时时间,单位:秒 * @param serializeFlag 是否序列化 * @return */ public boolean setOutTime(String key, int outTime, boolean serializeFlag) { if (StringUtils.isEmpty(key)) { return false; } try (Jedis jedis = getJedis(false)) { 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; } try (Jedis jedis = getJedis(false)) { jedis.persist(key); } } /** * 获取指定key的剩余过期时间 * * @param key 关键字 * @return 剩余过期时间,单位:秒(特殊值如-2:没有这个key;-1:有key单没有设置过期时间,就是不过期) */ public Long getOutTime(String key) { if (StringUtils.isEmpty(key)) { return null; } try (Jedis jedis = getJedis(false)) { 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; } try (Jedis jedis = getJedis(true)) { 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; } try (Jedis jedis = getJedis(false)) { 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; } try (Jedis jedis = getJedis(true)) { 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; } try (Jedis jedis = getJedis(false)) { 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 map) { if (StringUtils.isEmpty(key)) { return; } Map param = Maps.newHashMap(); for (Map.Entry entry : map.entrySet()) { param.put(entry.getKey().getBytes(StandardCharsets.UTF_8), SerializationUtils.serialize(entry.getValue())); } try (Jedis jedis = getJedis(false)) { 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; } try (Jedis jedis = getJedis(true)) { 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; } try (Jedis jedis = getJedis(true)) { return SerializationUtils.deserialize(jedis.hget(key.getBytes(StandardCharsets.UTF_8), field.getBytes(StandardCharsets.UTF_8))); } } /** * hash-获取所有字段内容集合 * * @param key 关键字 * @return 所有字段内容集合 */ public Map getHash(String key) { if (StringUtils.isEmpty(key)) { return null; } try (Jedis jedis = getJedis(true)) { Map map = jedis.hgetAll(key.getBytes(StandardCharsets.UTF_8)); Map result = Maps.newHashMap(); Object field; for (Map.Entry 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; } try (Jedis jedis = getJedis(true)) { return jedis.hlen(key.getBytes(StandardCharsets.UTF_8)); } } /** * hash-获取指定key的字段名集合 * * @param key 关键字 * @return 字段名集合 */ public List getHashFields(String key) { if (StringUtils.isEmpty(key)) { return null; } try (Jedis jedis = getJedis(true)) { Set 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 getHashValues(String key) { if (StringUtils.isEmpty(key)) { return null; } return getHashValues(key, Object.class); } /** * hash-获取指定key的所有value集合 * * @param key 关键字 * @return value集合 */ public List getHashValues(String key, Class t) { if (StringUtils.isEmpty(key)) { return null; } try (Jedis jedis = getJedis(true)) { return CacheCommonUtil.deserializeCollection(jedis.hvals(key.getBytes(StandardCharsets.UTF_8)), t); } } /** * hash-获取指定key的指定字段内容集合 * * @param key 关键字 * @param fields 字段名集合 * @return 字段内容集合 */ public List 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 List getHashAimFieldValues(String key, Class t, String... fields) { if (StringUtils.isEmpty(key)) { return null; } byte[][] bytes = Arrays.stream(fields).map(field -> field.getBytes(StandardCharsets.UTF_8)).toArray(byte[][]::new); try (Jedis jedis = getJedis(true)) { 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); try (Jedis jedis = getJedis(false)) { return jedis.hdel(key.getBytes(StandardCharsets.UTF_8), bytes); } } /*==================hash-final==================*/ }