JedisCluster如何添加keys方法-walker小飞java博客

JedisCluster如何添加keys方法

需求:目前项目是老项目,还是使用的jedisCluster,发现里面根本不支持keys方法,大家也知道keys方法数据量大会有性能问题,但是我这边有一个场景,实时获取到用户的在线状态,我这边会把某个前缀的redis中的key提取出来,然后在做一定的逻辑修改,大家可以复制过去直接用。

步骤:
1.为了扩展JedisCluster大家可以单独弄一个接口出来。

代码:

public interface JedisClusterService {

JedisPool getJedisPool();

String set(String key, String value, String nxxx, String expx, long time);

String set(String key, String value);

String setByte(byte[] key, byte[] value);

String setObj(String key, Object value);

String set(String key, String value, int seconds);

String setexByte(byte[] key, byte[] value, int seconds);

String setObj(String key, Object value, int seconds);


String get(String key);

byte[] getByte(byte[] key);

Object getObj(String key);

long del(String key);

long delByte(byte[] key);

long delObj(String key);

Boolean exists(String key);

Boolean existsByte(byte[] key);

Boolean existsObj(String key);

long hset(String key, String field, String value);

long hincr(String key, String field, long step);

long hsetByte(byte[] key, byte[] field, byte[] value);

long hsetObj(String key, String field, Object value);

String hget(String key, String field);

byte[] hgetByte(byte[] key, byte[] field);

Object hgetObj(String key, String field);

long incr(String key);

long incr(String key, long step);

long decr(String key);

long decr(String key, long step);

String spop(String key);

Object spopObj(String key);

long sadd(String key, final String... member);

long saddObj(String key, final Object member);

long llen(String key);

long llenByte(byte[] key);

long llenObj(String key);

String hmset(String key, Map<String, String> hash);

String hmsetByte(byte[] key, Map<byte[], byte[]> hash);

String hmsetObj(String key, Map<String, ? extends Object> hash);

List<String> hmget(String key, String... fields);

List<byte[]> hmgetByte(byte[] key, byte[]... fields);

Map<String, String> hgetAll(String key);

Map<byte[], byte[]> hgetAllByte(byte[] key);

long hdel(String key, String field);

long hdelByte(byte[] key, byte[] field);

long hdelObj(String key, String field);

List<String> hvals(String key);

Collection<byte[]> hvalsByte(byte[] key);

long lpush(String key, String... value);

long lpushByte(byte[] key, byte[]... value);

String lpop(String key);

byte[] lpopByte(byte[] key);

long rpush(String key, String... value);

long rpushByte(byte[] key, byte[]... value);

String rpop(String key);

byte[] rpopByte(byte[] key);

long expire(String key, int seconds);

long expireByte(byte[] key, int seconds);

long expireObj(String key, int seconds);

long pexpire(String key, long milliseconds);

long pexpireByte(byte[] key, long milliseconds);

Boolean hexists(String key, String field);

Boolean hexistsByte(byte[] key, byte[] field);

Boolean hexistsObj(String key, String field);

Set<String> hkeys(String key);

long setnx(String key, String value);

/**
 * 设置单个值
 *
 * @param key
 * @param value
 * @return
 */
String vset(String key, Object value);

/**
 * 设置单个值
 *
 * @param key
 * @param value
 * @return
 */
String vset(String key, Object value, int time);

/**
 * 获取单个值
 *
 * @param key
 * @return
 */
<T> T vget(Class<T> cls, String key);

long vhset(String key, String field, Object value);

<T> T vhget(Class<T> cls, String key, String field);

<T> List<T> vgetArray(Class cls, String key);

long hincrBy(String key, String mapKey, int value);

String srandmember(String key);

List<String> srandmember(String key, int count);

String type(String key);

String typeByte(byte[] key);

List<String> lrange(String key, long start, long end);

List<byte[]> lrangeByte(byte[] key, long start, long end);

Set<String> smembers(String key);

Set<byte[]> smembersByte(byte[] key);

Boolean sismember(String key, String member);

Long srem(String key, String... member);

Set<String> keys(String keyPrifex);

}

2.接口的实现类
package com.tpjk.support.redis.service.impl;

import com.alibaba.fastjson.JSON;
import com.tpjk.support.redis.service.JedisClusterService;
import com.tpjk.support.redis.util.SerializeUtil;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

@Slf4j
public class JedisClusterServiceImpl implements JedisClusterService {

private JedisPool jedisPool;

private static final String KEY_SUFFIX = "*";

@Override
public JedisPool getJedisPool() {
    return jedisPool;
}

public JedisClusterServiceImpl(JedisPool jedisPool) {
    this.jedisPool = jedisPool;
}


@Override
public String set(String key, String value, String nxxx, String expx, long time) {
    if (null == value || null == key || null == nxxx || null == expx) {
        return null;
    }
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.set(key, value, nxxx, expx, time);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String set(String key, String value) {
    if (null == value || null == key) {
        return null;
    }
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.set(key, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String setByte(byte[] key, byte[] value) {
    if (null == value || null == key) {
        return null;
    }
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.set(key, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }

}

@Override
public String setObj(String key, Object value) {
    if (null == value || null == key) {
        return null;
    }
    return setByte(SerializeUtil.serialize(key), SerializeUtil.serialize(value));
}

@Override
public String set(String key, String value, int seconds) {
    if (null == value || null == key) {
        return null;
    }
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.setex(key, seconds, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String setexByte(byte[] key, byte[] value, int seconds) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.setex(key, seconds, value);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public String setObj(String key, Object value, int seconds) {
    if (null == value || null == key) {
        return null;
    }
    return setexByte(SerializeUtil.serialize(key), SerializeUtil.serialize(value), seconds);
}


@Override
public String get(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.get(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public byte[] getByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.get(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Object getObj(String key) {
    return SerializeUtil.unserialize(getByte(SerializeUtil.serialize(key)));
}

@Override
public long del(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.del(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}


@Override
public long delByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.del(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long delObj(String key) {
    return delByte(SerializeUtil.serialize(key));
}

@Override
public Boolean exists(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.exists(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Boolean existsByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.exists(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Boolean existsObj(String key) {
    return existsByte(SerializeUtil.serialize(key));
}


@Override
public long hset(String key, String field, String value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hset(key, field, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long hincr(String key, String field, long step) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hincrBy(key, field, step);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long hsetByte(byte[] key, byte[] field, byte[] value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hset(key, field, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long hsetObj(String key, String field, Object value) {
    if (null == value || null == field || null == key) {
        return -1;
    }
    return hsetByte(SerializeUtil.serialize(key), SerializeUtil.serialize(field), SerializeUtil.serialize(value));
}

@Override
public String hget(String key, String field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hget(key, field);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public byte[] hgetByte(byte[] key, byte[] field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hget(key, field);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Object hgetObj(String key, String field) {
    return SerializeUtil.unserialize(hgetByte(SerializeUtil.serialize(key), SerializeUtil.serialize(field)));
}

@Override
public long incr(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.incr(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long incr(String key, long step) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.incrBy(key, step);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long decr(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.decr(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long decr(String key, long step) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.decrBy(key, step);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public String spop(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.spop(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Object spopObj(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return SerializeUtil.unserialize(jedis.spop(SerializeUtil.serialize(key)));
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public long sadd(String key, final String... member) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.sadd(key, member);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long saddObj(String key, final Object member) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.sadd(SerializeUtil.serialize(key), SerializeUtil.serialize(member));
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long llen(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.llen(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long llenByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.llen(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long llenObj(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.llen(SerializeUtil.serialize(key));
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public String hmset(String key, Map<String, String> hash) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hmset(key, hash);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String hmsetByte(byte[] key, Map<byte[], byte[]> hash) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hmset(key, hash);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String hmsetObj(String key, Map<String, ? extends Object> map) {
    log.info("redis map,redisKey:" + key + ",size:" + map.size());
    Map<byte[], byte[]> hash = new HashMap<byte[], byte[]>();
    for (Map.Entry<String, ? extends Object> me : map.entrySet()) {
        hash.put(SerializeUtil.serialize(me.getKey()), SerializeUtil.serialize(me.getValue()));
    }
    return hmsetByte(SerializeUtil.serialize(key), hash);
}

@Override
public List<String> hmget(String key, String... fields) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hmget(key, fields);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public List<byte[]> hmgetByte(byte[] key, byte[]... fields) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hmget(key, fields);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Map<String, String> hgetAll(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hgetAll(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Map<byte[], byte[]> hgetAllByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hgetAll(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public long hdel(String key, String field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hdel(key, field);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long hdelByte(byte[] key, byte[] field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hdel(key, field);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long hdelObj(String key, String field) {
    return hdelByte(SerializeUtil.serialize(key), SerializeUtil.serialize(field));
}

@Override
public List<String> hvals(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hvals(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Collection<byte[]> hvalsByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hvals(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public long lpush(String key, String... value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lpush(key, value);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return -1;
    }
}

@Override
public long lpushByte(byte[] key, byte[]... value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lpush(key, value);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public String lpop(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lpop(key);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public byte[] lpopByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lpop(key);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public long rpush(String key, String... value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.rpush(key, value);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

public long hincrBy(String key, String mapKey, int value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hincrBy(key, mapKey, value);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}


@Override
public long rpushByte(byte[] key, byte[]... value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.rpush(key, value);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public String rpop(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.rpop(key);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public byte[] rpopByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.rpop(key);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public long expire(String key, int seconds) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.expire(key, seconds);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public long expireByte(byte[] key, int seconds) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.expire(key, seconds);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public long expireObj(String key, int seconds) {
    return expireByte(SerializeUtil.serialize(key), seconds);
}

@Override
public long pexpire(String key, long milliseconds) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.pexpire(key, milliseconds);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public long pexpireByte(byte[] key, long milliseconds) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.pexpire(key, milliseconds);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return 0;
    }
}

@Override
public Boolean hexists(String key, String field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hexists(key, field);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return false;
    }
}

@Override
public Boolean hexistsByte(byte[] key, byte[] field) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hexists(key, field);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return false;
    }
}

@Override
public Boolean hexistsObj(String key, String field) {
    return hexistsByte(SerializeUtil.serialize(key), SerializeUtil.serialize(field));
}

@Override
public Set<String> hkeys(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.hkeys(key);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public String vset(String key, Object value) {
    return set(key, JSON.toJSONString(value));
}

@Override
public String vset(String key, Object value, int time) {
    return set(key, JSON.toJSONString(value), time);
}

/**
 * 获取单个值
 *
 * @param key
 * @return
 */
@Override
public <T> T vget(Class<T> cls, String key) {
    try {
        return JSON.parseObject(get(key), cls);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public long vhset(String key, String field, Object value) {
    return hset(key, field, JSON.toJSONString(value));
}

@Override
public <T> T vhget(Class<T> cls, String key, String field) {
    return JSON.parseObject(hget(key, field), cls);
}

@Override
public long setnx(String key, String value) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.setnx(key, value);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return -1;
    }
}

@Override
public <T> List<T> vgetArray(Class cls, String key) {
    try {
        return JSON.parseArray(get(key), cls);
    } catch (Exception e) {
        log.error("redis执行命令失败", e);
        return null;
    }
}

@Override
public String srandmember(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.srandmember(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public List<String> srandmember(String key, int count) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.srandmember(key, count);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String type(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.type(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public String typeByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.type(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public List<String> lrange(String key, long start, long end) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lrange(key, start, end);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public List<byte[]> lrangeByte(byte[] key, long start, long end) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.lrange(key, start, end);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Set<String> smembers(String key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.smembers(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Set<byte[]> smembersByte(byte[] key) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.smembers(key);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

@Override
public Boolean sismember(String key, String member) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.sismember(key, member);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return false;
    }
}

@Override
public Long srem(String key, String... member) {
    try (Jedis jedis = jedisPool.getResource()) {
        return jedis.srem(key, member);
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

/**
 * 目前顾问不是很多,使用这种方式,数据量大此方法有性能问题!!!!
 * @param keyPrifex
 * @return
 */
@Override
public Set<String> keys(String keyPrifex) {
    try (Jedis jedis = jedisPool.getResource()) {
        Set<String> list = new LinkedHashSet<>();
        Set<String> keys = jedis.keys(keyPrifex.concat(KEY_SUFFIX));
        if(null!=keys&&keys.size()>0){
            list.addAll(keys);
        }
        return list;
    } catch (Exception ex) {
        log.error("redis执行命令失败", ex);
        return null;
    }
}

}

注意:大家要提前再项目中配置redis的基础连接。。。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
JedisCluster是Redis的分布式解决方案,它不支持直接的分布式锁机制。但是,我们可以利用Redis的原子性操作和Lua脚本来实现分布式锁。 以下是一个简单的JedisCluster加锁的示例代码: ``` public class RedisLock { private static final String LOCK_SUCCESS = "OK"; private static final String SET_IF_NOT_EXIST = "NX"; private static final String SET_WITH_EXPIRE_TIME = "PX"; private static final Long RELEASE_SUCCESS = 1L; /** * 尝试获取分布式锁 * * @param jedisCluster Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @param expireTime 超期时间 * @return 是否获取成功 */ public static boolean tryGetDistributedLock(JedisCluster jedisCluster, String lockKey, String requestId, int expireTime) { String result = jedisCluster.set(lockKey, requestId, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, expireTime); if (LOCK_SUCCESS.equals(result)) { return true; } return false; } /** * 释放分布式锁 * * @param jedisCluster Redis客户端 * @param lockKey 锁 * @param requestId 请求标识 * @return 是否释放成功 */ public static boolean releaseDistributedLock(JedisCluster jedisCluster, String lockKey, String requestId) { 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(lockKey), Collections.singletonList(requestId)); if (RELEASE_SUCCESS.equals(result)) { return true; } return false; } } ``` 这个类包含了两个方法:tryGetDistributedLock和releaseDistributedLock。 在tryGetDistributedLock方法中,我们使用JedisCluster的set方法来设置锁,其中SET_IF_NOT_EXIST表示只有在锁不存在时才能设置成功,SET_WITH_EXPIRE_TIME表示锁的超时时间。如果返回的结果是LOCK_SUCCESS,表示获取锁成功。 在releaseDistributedLock方法中,我们使用Lua脚本来释放锁。Lua脚本中首先判断锁是否属于当前请求,如果是,则删除锁,否则返回0。如果返回的结果是RELEASE_SUCCESS,表示释放锁成功。 需要注意的是,在使用JedisCluster时,我们需要确保所有节点的时间都是同步的,否则会出现锁失效的问题。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值