| File was renamed from rsf-common/src/main/java/com/vincent/rsf/common/utils/RedisSupport.java |
| | |
| | | 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; |
| | | |
| | | /** |
| | |
| | | * 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); |
| | | } |
| | | return redisSupport; |
| | | } |
| | | @Autowired |
| | | private RedisProperties redisProperties; |
| | | |
| | | 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")); |
| | | // 在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; |
| | | public JedisPool getPool() { |
| | | if (null == this.pool) { |
| | | JedisPoolConfig config = new JedisPoolConfig(); |
| | | config.setTestOnBorrow(false); |
| | | // 当前jedis连接的库号 |
| | | index = ConfigHelper.getInteger(name + ".index"); |
| | | // 实例化jedis池 |
| | | 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 - 初始化Reids时出错,在配置文件中未找到{}.***属性(host,port,max,min,timeout,index)", name); |
| | | this.index = redisProperties.getIndex(); |
| | | this.pool = new JedisPool(config |
| | | , redisProperties.getHost() |
| | | , redisProperties.getPort() |
| | | , redisProperties.getTimeout() |
| | | , redisProperties.getPassword() |
| | | ); |
| | | } |
| | | } 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); |
| | |
| | | } catch (Exception e){ |
| | | log.error(this.getClass().getSimpleName(), e); |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | |
| | | log.error(this.getClass().getSimpleName(), e); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static void main(String...strings){ |
| | | RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | } |
| | | |
| | | } |