package com.wiz.redis; import com.wiz.redis.config.RedisConfig; import com.wiz.util.Tools; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.cglib.proxy.Callback; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.NoOp; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.params.SetParams; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class RedisClusterProvider { private JedisCluster jedisCluster; public RedisClusterProvider(RedisConfig redisConfig) { this.connect(redisConfig); } public boolean lock(String lockName, int expireTime) { String who = Thread.currentThread().getName() + "@" + Tools.getHostAddress(); SetParams params = new SetParams(); params.nx(); params.ex(expireTime); String result = jedisCluster.set(lockName, who, params); if ("ok".equalsIgnoreCase(result)) { return true; } return false; } public boolean unlock(String lockName) { String who = Thread.currentThread().getName() + "@" + Tools.getHostAddress(); String script = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end"; Object result = jedisCluster.eval(script, Collections.singletonList(lockName), Collections.singletonList(who)); if ("1".equalsIgnoreCase(String.valueOf(result))) { return true; } return false; } protected void close() { Class<RedisClusterProvider> clazz = RedisClusterProvider.class; synchronized (clazz) { if (null != jedisCluster) { jedisCluster.close(); } } } protected boolean connect(RedisConfig redisConfig) { if (null == redisConfig) { return false; } Set<HostAndPort> nodes = new HashSet<>(); String[] addrs = redisConfig.getClassNode().split(","); for (String addr : addrs) { String[] hostAndPort = addr.split(":"); nodes.add(new HostAndPort(hostAndPort[0], Integer.parseInt(hostAndPort[1]))); } JedisCluster cluster = newJedisCluster(nodes, redisConfig, newGenericObjectPoolConfig()); if (testConnection(cluster)) { jedisCluster = cluster; return true; } return false; } protected boolean testConnection(JedisCluster cluster) { String ping = cluster.ping(); return "PONG".equalsIgnoreCase(ping); } private JedisCluster newJedisCluster(Set<HostAndPort> nodes, RedisConfig redisConfig, GenericObjectPoolConfig poolConfig) { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(JedisCluster.class); Callback[] callbacks = new Callback[]{NoOp.INSTANCE}; enhancer.setCallbacks(callbacks); Class[] argumentsType = new Class[]{Set.class, Integer.TYPE, Integer.TYPE, Integer.TYPE, String.class, GenericObjectPoolConfig.class}; Object[] arguments = new Object[]{nodes, redisConfig.getConnectionTimeout(), redisConfig.getSoTimeout(), redisConfig.getMaxAttempts(), redisConfig.getPassword(), poolConfig}; return (JedisCluster) enhancer.create(argumentsType, arguments); } private GenericObjectPoolConfig newGenericObjectPoolConfig() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); return config; } }
redis锁工具类
最新推荐文章于 2024-04-22 15:06:23 发布