From e2778109931e6f944e6bdd9f14dcb0bec8aa32da Mon Sep 17 00:00:00 2001 From: vincentlu <t1341870251@gmail.com> Date: 星期四, 13 二月 2025 14:00:51 +0800 Subject: [PATCH] # --- rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java | 84 ++++++++++++------------------------------ 1 files changed, 24 insertions(+), 60 deletions(-) diff --git a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java b/rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java similarity index 82% rename from rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java rename to rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java index 6886d97..87c8a4a 100644 --- a/rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java +++ b/rsf-server/src/main/java/com/vincent/rsf/server/common/service/RedisService.java @@ -1,13 +1,15 @@ -package com.vincent.rsf.common.utils; +package com.vincent.rsf.server.common.service; +import com.vincent.rsf.common.utils.Serialize; +import com.vincent.rsf.server.common.config.RedisProperties; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.Date; -import java.util.HashMap; -import java.util.Map; import java.util.Set; /** @@ -15,71 +17,38 @@ * Created by vincent on 2023-03-13 */ @Slf4j -public class RedisSupport { - - private static final Map<String, RedisSupport> REDIS_CACHE = new HashMap<>(); - - public static final String DEFAULT_FLAG = "redis"; +@Service +public class RedisService { private static final String LINK = "."; - public static RedisSupport defaultRedisSupport = RedisSupport.getRedis(DEFAULT_FLAG); - protected JedisPool pool; - - protected JedisPoolConfig config; Integer index = 0; - public Boolean initialize = false; + public Boolean initialize = true; - public synchronized static RedisSupport getRedis(String name){ - RedisSupport redisSupport = REDIS_CACHE.get(name); - if (null == redisSupport){ - redisSupport = new RedisSupport(name); - REDIS_CACHE.put(name, redisSupport); + @Autowired + private RedisProperties redisProperties; + + public JedisPool getPool() { + if (null == this.pool) { + JedisPoolConfig config = new JedisPoolConfig(); + config.setTestOnBorrow(false); + this.index = redisProperties.getIndex(); + this.pool = new JedisPool(config + , redisProperties.getHost() + , redisProperties.getPort() + , redisProperties.getTimeout() + , redisProperties.getPassword() + ); } - return redisSupport; - } - - private RedisSupport(String name) { - try { - boolean bAll = true; - String[] configKeys = {"host", "port", "max", "min", "timeout", "index"}; - for(String key : configKeys){ - if(!ConfigHelper.contains(name + "." + key)){ - bAll = false; - } - } - if (bAll) { - config = new JedisPoolConfig(); - config.setMaxIdle(ConfigHelper.getInteger(name + ".min")); - config.setMaxWaitMillis(ConfigHelper.getLong(name + ".timeout")); - // 鍦╞orrow涓�涓猨edis瀹炰緥鏃讹紝鏄惁鎻愬墠杩涜validate鎿嶄綔锛涘鏋滀负true锛屽垯寰楀埌鐨刯edis瀹炰緥鍧囨槸鍙敤鐨勶紱 - config.setTestOnBorrow(false); - // 褰撳墠jedis杩炴帴鐨勫簱鍙� - index = ConfigHelper.getInteger(name + ".index"); - // 瀹炰緥鍖杍edis姹� - String host = ConfigHelper.getString(name + ".host"); - int port = ConfigHelper.getInteger(name + ".port"); - int timeout = ConfigHelper.getInteger(name + ".timeout"); - String password = ConfigHelper.getString(name + ".password"); - - pool = new JedisPool(config, host , port, timeout, password); - Jedis jedis = this.getJedis(); - this.pool.returnResource(jedis); - initialize = true; - } else { - log.error("ERROR - 鍒濆鍖朢eids鏃跺嚭閿欙紝鍦ㄩ厤缃枃浠朵腑鏈壘鍒皗}.***灞炴��(host,port,max,min,timeout,index)", name); - } - } catch (Exception e) { - log.error(this.getClass().getSimpleName(), e); - } + return this.pool; } public Jedis getJedis(){ try{ - Jedis jedis = pool.getResource(); + Jedis jedis = this.getPool().getResource(); if(this.index != jedis.getDB().intValue()) { jedis.select(this.index); @@ -88,7 +57,6 @@ } catch (Exception e){ log.error(this.getClass().getSimpleName(), e); } - return null; } @@ -518,10 +486,6 @@ log.error(this.getClass().getSimpleName(), e); } return null; - } - - public static void main(String...strings){ - RedisSupport redis = RedisSupport.defaultRedisSupport; } } -- Gitblit v1.9.1