1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
| import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.*; import redis.clients.util.JedisClusterCRC16;
import java.util.*;
public class RedisCacheDelegate extends AbstractCache implements CacheManager, Cache { private static Logger logger = LoggerFactory.getLogger(RedisCacheDelegate.class);
private String clusterNodes;
private int maxAttempts;
private int timeout;
private JedisCluster jedisCluster; private JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
private static RedisCacheDelegate redisCacheDelegate = null; private JedisClusterInfoCache cache;
private final static String WARM_KEY = "warm_key"; private final static String WARM_VALUE = "value";
public static RedisCacheDelegate getInstant(CacheProperties cacheProperties) { if (redisCacheDelegate == null) { synchronized (RedisCacheDelegate.class) { if (redisCacheDelegate == null) { redisCacheDelegate = new RedisCacheDelegate(cacheProperties.getNodes(), cacheProperties.getTimeout(), cacheProperties.getMaxAttempts()); } } }
return redisCacheDelegate; }
private RedisCacheDelegate(String clusterNodes, int timeout, int maxAttempts) { this.clusterNodes = clusterNodes; this.timeout = timeout; this.maxAttempts = maxAttempts; init(); }
private JedisPoolConfig getJedisPoolConfig() {
jedisPoolConfig.setMaxWaitMillis(200); jedisPoolConfig.setMaxTotal(50); jedisPoolConfig.setMinIdle(10); jedisPoolConfig.setMaxIdle(30); jedisPoolConfig.setBlockWhenExhausted(true); jedisPoolConfig.setJmxEnabled(true); jedisPoolConfig.setTestWhileIdle(true); jedisPoolConfig.setMinEvictableIdleTimeMillis(60000); jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000); jedisPoolConfig.setNumTestsPerEvictionRun(-1);
return jedisPoolConfig; }
@Override public void init() { String[] serverArray = clusterNodes.split(","); Set<HostAndPort> nodes = new HashSet<>(); for (String ipPort : serverArray) { String[] ipPortPair = ipPort.split(":"); nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()))); }
jedisCluster = new JedisCluster(nodes, timeout * 1000, maxAttempts, getJedisPoolConfig()); MetaObject metaObject = SystemMetaObject.forObject(jedisCluster); cache = (JedisClusterInfoCache) metaObject.getValue("connectionHandler.cache"); warm(); }
@Override public void warm() { set(WARM_KEY, WARM_VALUE, 60); for (int i = 0; i < jedisPoolConfig.getMinIdle(); i++) { ttl(WARM_KEY); } }
@Override public void set(String key, String value) { jedisCluster.set(key, value); }
@Override public void set(String key, String value, int expiredTime) { jedisCluster.setex(key, expiredTime, value); }
@Override public void mSet(Map<String, String> data) { if (data != null && data.size() > 0) { data.forEach((key, value) -> jedisCluster.set(key, value)); } }
@Override public void mSetPipLine(Map<String, String> data) { setPipLine(data, 0); }
private void setPipLine(Map<String, String> data, int expiredTime) { if (data.size() < 1) { return; }
Map<JedisPool, Map<String, String>> jedisPoolMap = new HashMap<>(); JedisPool currentJedisPool = null;
for (String key : data.keySet()) { int crc = JedisClusterCRC16.getSlot(key); currentJedisPool = cache.getSlotPool(crc);
if (jedisPoolMap.containsKey(currentJedisPool)) { jedisPoolMap.get(currentJedisPool).put(key, data.get(key)); } else { Map<String, String> inner = new HashMap<>(); inner.put(key, data.get(key)); jedisPoolMap.put(currentJedisPool, inner); } } Map<String, String> map = null; for (Map.Entry<JedisPool, Map<String, String>> entry : jedisPoolMap.entrySet()) { try { currentJedisPool = entry.getKey(); map = entry.getValue(); Jedis jedis = currentJedisPool.getResource(); Pipeline currentPipeline = jedis.pipelined(); if (expiredTime > 0) { map.forEach((k, v) -> currentPipeline.setex(k, expiredTime, v)); } else { map.forEach((k, v) -> currentPipeline.set(k, v)); } currentPipeline.sync(); currentPipeline.close(); jedis.close(); } catch (Exception e) { logger.error("setPipline error.", e); }
} }
@Override public void mSet(Map<String, String> data, int expiredTime) { if (data != null && data.size() > 0) { data.forEach((key, value) -> jedisCluster.setex(key, expiredTime, value)); } }
@Override public void mSetPipLine(Map<String, String> data, int expiredTime) { setPipLine(data, expiredTime); }
@Override public String get(String key) { return jedisCluster.get(key); }
@Override public List<String> mGet(List<String> keys) { if (keys.size() < 1) { return null; } List<String> result = new ArrayList<>(keys.size()); for (String key : keys) { result.add(jedisCluster.get(key)); } return result; }
@Override public List<String> mGetPipLine(List<String> key) { return getPipLine(key); }
@Override public long ttl(String key) { return jedisCluster.ttl(key); }
private List<String> getPipLine(List<String> keys) { if (keys.size() < 1) { return null; } List<String> result = new ArrayList<>(keys.size()); Map<String, String> resultMap = new HashMap<>(keys.size()); if (keys.size() == 1) { result.add(jedisCluster.get(keys.get(0))); return result; }
Map<JedisPool, List<String>> jedisPoolMap = new HashMap<>();
List<String> keyList = null; JedisPool currentJedisPool = null; Pipeline currentPipeline = null;
for (String key : keys) { int crc = JedisClusterCRC16.getSlot(key); currentJedisPool = cache.getSlotPool(crc);
if (jedisPoolMap.containsKey(currentJedisPool)) { jedisPoolMap.get(currentJedisPool).add(key); } else { keyList = new ArrayList<>(); keyList.add(key); jedisPoolMap.put(currentJedisPool, keyList); } }
List<Object> res; for (Map.Entry<JedisPool, List<String>> entry : jedisPoolMap.entrySet()) { try { currentJedisPool = entry.getKey(); keyList = entry.getValue(); Jedis jedis = currentJedisPool.getResource(); currentPipeline = jedis.pipelined(); for (String key : keyList) { currentPipeline.get(key); } res = currentPipeline.syncAndReturnAll(); currentPipeline.close(); jedis.close(); for (int i = 0; i < keyList.size(); i++) { resultMap.put(keyList.get(i), res.get(i) == null ? null : res.get(i).toString()); } } catch (Exception e) { logger.error("getPipLine error.", e); } } for (String key : keys) { result.add(resultMap.containsKey(key) ? resultMap.get(key) : null); }
return result; }
@Override public void destroy() { try { jedisCluster.close(); } catch (Exception e) {
} } }
|