|
2 | 2 |
|
3 | 3 | import redis.clients.jedis.HostAndPort; |
4 | 4 | import redis.clients.jedis.JedisPoolConfig; |
5 | | -import redis.clients.jedis.Protocol; |
6 | 5 | import tomcat.request.session.data.cache.DataCache; |
7 | | -import tomcat.request.session.data.cache.DataCacheConstants; |
8 | | -import tomcat.request.session.data.cache.DataCacheConstants.RedisConfigType; |
9 | | -import tomcat.request.session.data.cache.DataCacheFactory; |
| 6 | +import tomcat.request.session.model.Config; |
| 7 | +import tomcat.request.session.model.Config.RedisConfigType; |
10 | 8 |
|
11 | 9 | import java.util.ArrayList; |
12 | 10 | import java.util.Collection; |
13 | 11 | import java.util.HashSet; |
14 | 12 | import java.util.List; |
15 | | -import java.util.Properties; |
16 | 13 | import java.util.Set; |
17 | 14 |
|
18 | 15 | /** author: Ranjith Manickam @ 12 Jul' 2018 */ |
19 | 16 | public class RedisCache implements DataCache { |
20 | 17 |
|
21 | 18 | private DataCache dataCache; |
22 | 19 |
|
23 | | - public RedisCache(Properties properties) { |
24 | | - initialize(properties); |
| 20 | + public RedisCache(Config config) { |
| 21 | + initialize(config); |
25 | 22 | } |
26 | 23 |
|
27 | 24 | /** {@inheritDoc} */ |
@@ -54,73 +51,51 @@ public Long delete(String key) { |
54 | 51 | return dataCache.delete(key); |
55 | 52 | } |
56 | 53 |
|
57 | | - private void initialize(Properties properties) { |
58 | | - RedisConfigType configType; |
59 | | - if (Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_CLUSTER_ENABLED))) { |
60 | | - configType = RedisConfigType.CLUSTER; |
61 | | - } else if (Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_SENTINEL_ENABLED))) { |
62 | | - configType = RedisConfigType.SENTINEL; |
63 | | - } else { |
64 | | - configType = RedisConfigType.DEFAULT; |
65 | | - } |
66 | | - |
67 | | - String hosts = DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_HOSTS, String.format("%s:%s", Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT)); |
68 | | - Collection<?> nodes = getJedisNodes(hosts, configType); |
69 | | - |
70 | | - String password = DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_PASSWORD); |
71 | | - password = (password != null && !password.isEmpty()) ? password : null; |
72 | | - |
73 | | - int database = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_DATABASE)); |
74 | | - |
75 | | - int timeout = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TIMEOUT)); |
76 | | - timeout = Math.max(timeout, Protocol.DEFAULT_TIMEOUT); |
77 | | - |
78 | | - JedisPoolConfig poolConfig = getPoolConfig(properties); |
79 | | - switch (configType) { |
| 54 | + private void initialize(Config config) { |
| 55 | + Collection<?> nodes = getJedisNodes(config.getRedisHosts(), config.getRedisConfigType()); |
| 56 | + JedisPoolConfig poolConfig = getPoolConfig(config); |
| 57 | + switch (config.getRedisConfigType()) { |
80 | 58 | case CLUSTER: |
81 | | - dataCache = new RedisClusterManager((Set<HostAndPort>) nodes, password, timeout, poolConfig); |
| 59 | + this.dataCache = new RedisClusterManager((Set<HostAndPort>) nodes, |
| 60 | + config.getRedisPassword(), |
| 61 | + config.getRedisTimeout(), |
| 62 | + poolConfig); |
82 | 63 | break; |
83 | 64 | case SENTINEL: |
84 | | - String masterName = String.valueOf(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_SENTINEL_MASTER)); |
85 | | - dataCache = new RedisSentinelManager((Set<String>) nodes, masterName, password, database, timeout, poolConfig); |
| 65 | + this.dataCache = new RedisSentinelManager((Set<String>) nodes, |
| 66 | + config.getRedisSentinelMaster(), |
| 67 | + config.getRedisPassword(), |
| 68 | + config.getRedisDatabase(), |
| 69 | + config.getRedisTimeout(), |
| 70 | + poolConfig); |
86 | 71 | break; |
87 | 72 | default: |
88 | | - dataCache = new RedisStandardManager(((List<String>) nodes).get(0), Integer.parseInt(((List<String>) nodes).get(1)), password, database, timeout, poolConfig); |
| 73 | + this.dataCache = new RedisStandardManager(((List<String>) nodes).get(0), |
| 74 | + Integer.parseInt(((List<String>) nodes).get(1)), |
| 75 | + config.getRedisPassword(), |
| 76 | + config.getRedisDatabase(), |
| 77 | + config.getRedisTimeout(), |
| 78 | + poolConfig); |
89 | 79 | break; |
90 | 80 | } |
91 | 81 | } |
92 | 82 |
|
93 | 83 | /** |
94 | 84 | * To get redis pool config. |
95 | 85 | * |
96 | | - * @param properties - Redis data cache properties. |
| 86 | + * @param config - Application config. |
97 | 87 | * @return - Returns the redis pool config. |
98 | 88 | */ |
99 | | - private JedisPoolConfig getPoolConfig(Properties properties) { |
| 89 | + private JedisPoolConfig getPoolConfig(Config config) { |
100 | 90 | JedisPoolConfig poolConfig = new JedisPoolConfig(); |
101 | | - int maxActive = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MAX_ACTIVE)); |
102 | | - poolConfig.setMaxTotal(maxActive); |
103 | | - |
104 | | - boolean testOnBorrow = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_ONBORROW)); |
105 | | - poolConfig.setTestOnBorrow(testOnBorrow); |
106 | | - |
107 | | - boolean testOnReturn = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_ONRETURN)); |
108 | | - poolConfig.setTestOnReturn(testOnReturn); |
109 | | - |
110 | | - int maxIdle = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MAX_IDLE)); |
111 | | - poolConfig.setMaxIdle(maxIdle); |
112 | | - |
113 | | - int minIdle = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_MIN_IDLE)); |
114 | | - poolConfig.setMinIdle(minIdle); |
115 | | - |
116 | | - boolean testWhileIdle = Boolean.parseBoolean(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_WHILEIDLE)); |
117 | | - poolConfig.setTestWhileIdle(testWhileIdle); |
118 | | - |
119 | | - int testNumPerEviction = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TEST_NUMPEREVICTION)); |
120 | | - poolConfig.setNumTestsPerEvictionRun(testNumPerEviction); |
121 | | - |
122 | | - long timeBetweenEviction = Long.parseLong(DataCacheFactory.getProperty(properties, DataCacheConstants.REDIS_TIME_BETWEENEVICTION)); |
123 | | - poolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEviction); |
| 91 | + poolConfig.setMaxTotal(config.getRedisMaxActive()); |
| 92 | + poolConfig.setTestOnBorrow(config.getRedisTestOnBorrow()); |
| 93 | + poolConfig.setTestOnReturn(config.getRedisTestOnReturn()); |
| 94 | + poolConfig.setMaxIdle(config.getRedisMaxIdle()); |
| 95 | + poolConfig.setMinIdle(config.getRedisMinIdle()); |
| 96 | + poolConfig.setTestWhileIdle(config.getRedisTestWhileIdle()); |
| 97 | + poolConfig.setNumTestsPerEvictionRun(config.getRedisTestNumPerEviction()); |
| 98 | + poolConfig.setTimeBetweenEvictionRunsMillis(config.getRedisTimeBetweenEviction()); |
124 | 99 | return poolConfig; |
125 | 100 | } |
126 | 101 |
|
|
0 commit comments