redis锁工具类

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值