All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.redis.single.service.JedisTemplate Maven / Gradle / Ivy

There is a newer version: 2.0.1.RELEASE
Show newest version
package com.luues.redis.single.service;

import com.luues.exception.core.exception.RedisException;
import com.luues.redis.single.config.RedisConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.*;
import redis.clients.jedis.params.GeoRadiusParam;
import redis.clients.jedis.params.SetParams;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Redis封装工具类
 * 注意:所有方法传入的key,如有配置spirng.redis.perifx 则自动spirng.redis.perifx + key(如key为:开头,忽略spirng.redis.perifx配置)
 *
 * @author Mr-Wu
 * @version 1.0
 */
@Component
@Slf4j(topic = "c.l.r.s.s.j")
public class JedisTemplate {

    @Autowired
    private ShardedJedisPool shardedJedisPool;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private RedisConfig redisConfig;


    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public boolean init() {
        return null == shardedJedisPool ? false : true;
    }

    public Jedis getJedis() {
        if (null == jedisPool) {
            log.error("{}", "single-redis is null,需要使用请先配置!");
        }
        return jedisPool.getResource();
    }

    public ShardedJedis getShardedJedis() {
        if (null == shardedJedisPool) {
            log.error("{}", "single-redis is null,需要使用请先配置!");
        }
        return shardedJedisPool.getResource();
    }

    public Set keys(String pattern) {
        return keys_(getKey(pattern));
    }
    public Set keys(String pattern, boolean prefix) {
        if(!prefix)
            return keys_(pattern);
        return keys(pattern);
    }
    protected Set keys_(String pattern){
        Set stringSet = new HashSet();
        String cursor = ScanParams.SCAN_POINTER_START;
        ScanParams scanParams = new ScanParams();
        scanParams.match(pattern);
        scanParams.count(1000);
        while (true) {
            ScanResult sscanResult = scan(cursor, scanParams);
            cursor = sscanResult.getCursor();// 返回0 说明遍历完成
            stringSet.addAll(sscanResult.getResult());
            if ("0".equals(cursor)) {
                break;
            }
        }
        return stringSet;
    }
    protected ScanResult scan(String cursor, ScanParams scanParams) {
        Jedis jedis = getJedis();
        try {
            return jedis.scan(cursor, scanParams);
        } finally {
            jedis.close();
        }
    }
    public void delKeys(String pattern) {
        delKeys_(getKey(pattern));
    }
    public void delKeys(String pattern, boolean prefix) {
        if(!prefix){
            delKeys_(pattern);
            return;
        }
        delKeys(pattern);
    }
    protected void delKeys_(String pattern){
        Set strings = keys(pattern);
        if (strings.size() == 0) {
            return;
        }
        Iterator iterator = strings.iterator();
        while (iterator.hasNext()) {
            del(iterator.next());
        }
    }


    /**
     * 功能:设置key过期时间
     *
     * @param key     键
     * @param seconds 过期时间(单位:秒)
     * @return Boolean 是否设置过期时间异常
     * @throws Exception
     */
    public Long expire(String key, Integer seconds) {
        return expire_(getKey(key), seconds);
    }
    public Long expire(String key, Integer seconds, boolean prefix) {
        if(!prefix)
            return expire_(key, seconds);
        return expire(key, seconds);
    }
    protected Long expire_(String key, Integer seconds){
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.expire(key, seconds);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:设置key过期时间
     *
     * @param key      键
     * @param expire   过期时间值
     * @param timeUnit 时间类型
     */
    public Long expire(String key, int expire, TimeUnit timeUnit) {
        return expire_(getKey(key), expire, timeUnit);
    }
    public Long expire(String key, int expire, TimeUnit timeUnit, boolean prefix) {
        if(!prefix)
            return expire_(key, expire, timeUnit);
        return expire(key, expire, timeUnit);
    }
    @SneakyThrows
    protected Long expire_(String key, int expire, TimeUnit timeUnit){
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            switch (timeUnit) {
                case SECONDS:
                    return shardedJedis.expire(key, expire);
                case MINUTES:
                    return shardedJedis.expire(key, expire * 60);
                case HOURS:
                    return shardedJedis.expire(key, 60 * 60 * expire);
                case DAYS:
                    return shardedJedis.expire(key, 60 * 60 * 24 * expire);
                default:
                    throw new RedisException("timeUnit 暂只支持SECONDS,MINUTES,HOURS,DAYS");
            }
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置key过期时间
     *
     * @param key     键
     * @param seconds 过期时间(单位:秒)
     * @return Boolean 是否设置过期时间异常
     * @throws Exception
     */
    public Long expire(byte[] key, Integer seconds) {
        return expire_(getKey(new String(key)).getBytes(), seconds);
    }
    public Long expire(byte[] key, Integer seconds, boolean prefix) {
        if(!prefix)
            return expire_(key, seconds);
        return expire(key, seconds);
    }
    protected Long expire_(byte[] key, Integer seconds){
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.expire(key, seconds);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:设置key过期时间
     *
     * @param key      键
     * @param expire   过期时间值
     * @param timeUnit 时间类型
     */
    public Long expire(byte[] key, int expire, TimeUnit timeUnit) {
        return expire_(getKey(new String(key)).getBytes(), expire, timeUnit);
    }
    public Long expire(byte[] key, int expire, TimeUnit timeUnit, boolean prefix) {
        if(!prefix)
            return expire_(key, expire, timeUnit);
        return expire(key, expire, timeUnit);
    }
    @SneakyThrows
    protected Long expire_(byte[] key, int expire, TimeUnit timeUnit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            switch (timeUnit) {
                case SECONDS:
                    return shardedJedis.expire(key, expire);
                case MINUTES:
                    return shardedJedis.expire(key, expire * 60);
                case HOURS:
                    return shardedJedis.expire(key, 60 * 60 * expire);
                case DAYS:
                    return shardedJedis.expire(key, 60 * 60 * 24 * expire);
                default:
                    throw new RedisException("timeUnit 暂只支持SECONDS,MINUTES,HOURS,DAYS");
            }
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置String类型值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public void set(String key, String value) {
        set_(getKey(key), value);
    }
    public void set(String key, String value, boolean perfix) {
        if(!perfix){
            set_(key, value);
            return;
        }
        set(key, value);
    }
    protected void set_(String key, String value){
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.set(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置String类型值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public void set(byte[] key, byte[] value) {
        set_(getKey(new String(key)).getBytes(), value);
    }
    public void set(byte[] key, byte[] value, boolean perfix) {
        if(!perfix){
            set_(key, value);
            return;
        }
        set(key, value);
    }
    protected void set_(byte[] key, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.set(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:设置String类型值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public String set(String key, String value, SetParams setParams) {
        return set_(getKey(key), value, setParams);
    }
    public String set(String key, String value, SetParams setParams, boolean perfix) {
        if(!perfix)
            return set_(key, value, setParams);
        return set(key, value, setParams);
    }
    protected String set_(String key, String value, SetParams setParams) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.set(key, value, setParams);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置String类型值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public String set(byte[] key, byte[] value, SetParams setParams) {
        return set_(getKey(new String(key)).getBytes(), value, setParams);
    }
    public String set(byte[] key, byte[] value, SetParams setParams, boolean perfix) {
        if(!perfix)
            return set_(key, value, setParams);
        return set(key, value, setParams);
    }
    protected String set_(byte[] key, byte[] value, SetParams setParams) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.set(key, value, setParams);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置String类型值和过期时间
     *
     * @param key     键
     * @param value   值
     * @param seconds 时间(秒)
     * @return
     */
    public void setex(String key, String value, int seconds) {
        setex_(getKey(key), value, seconds);
    }
    public void setex(String key, String value, int seconds, boolean perfix) {
        if(!perfix){
            setex_(getKey(key), value, seconds);
            return;
        }
        setex(key, value, seconds);

    }
    protected void setex_(String key, String value, int seconds) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.setex(key, seconds, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:设置String类型值和过期时间
     *
     * @param key      键
     * @param value    值
     * @param expire   时间值
     * @param timeUnit 时间类型
     * @return
     */
    public void setex(String key, String value, int expire, TimeUnit timeUnit) {
        setex_(getKey(key), value, expire, timeUnit);
    }
    public void setex(String key, String value, int expire, TimeUnit timeUnit, boolean perfix) {
        if(!perfix){
            setex_(key, value, expire, timeUnit);
            return;
        }
        setex(key, value, expire, timeUnit);
    }
    @SneakyThrows
    protected void setex_(String key, String value, int expire, TimeUnit timeUnit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            switch (timeUnit) {
                case SECONDS:
                    shardedJedis.setex(key, expire, value);
                    break;
                case MINUTES:
                    shardedJedis.setex(key, expire * 60, value);
                    break;
                case HOURS:
                    shardedJedis.setex(key, 60 * 60 * expire, value);
                    break;
                case DAYS:
                    shardedJedis.setex(key, 60 * 60 * 24 * expire, value);
                    break;
                default:
                    throw new RedisException("timeUnit 暂只支持SECONDS,MINUTES,HOURS,DAYS");
            }
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:设置String类型值和过期时间
     *
     * @param key     键
     * @param value   值
     * @param seconds 时间(秒)
     * @return
     */
    public void setex(byte[] key, byte[] value, int seconds) {
        setex_(getKey(new String(key)).getBytes(), value, seconds);
    }
    public void setex(byte[] key, byte[] value, int seconds, boolean perfix) {
        if(!perfix){
            setex_(key, value, seconds);
            return;
        }
        setex(key, value, seconds);
    }
    protected void setex_(byte[] key, byte[] value, int seconds) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.setex(key, seconds, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:设置String类型值和过期时间
     *
     * @param key      键
     * @param value    值
     * @param expire   时间值
     * @param timeUnit 时间类型
     * @return
     */
    public void setex(byte[] key, byte[] value, int expire, TimeUnit timeUnit) {
        setex_(getKey(new String(key)).getBytes(), value, expire, timeUnit);
    }
    public void setex(byte[] key, byte[] value, int expire, TimeUnit timeUnit, boolean perfix) {
        if(!perfix){
            setex_(key, value, expire, timeUnit);
            return;
        }
        setex(key, value, expire, timeUnit);
    }
    @SneakyThrows
    protected void setex_(byte[] key, byte[] value, int expire, TimeUnit timeUnit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            switch (timeUnit) {
                case SECONDS:
                    shardedJedis.setex(key, expire, value);
                    break;
                case MINUTES:
                    shardedJedis.setex(key, expire * 60, value);
                    break;
                case HOURS:
                    shardedJedis.setex(key, 60 * 60 * expire, value);
                    break;
                case DAYS:
                    shardedJedis.setex(key, 60 * 60 * 24 * expire, value);
                    break;
                default:
                    throw new RedisException("timeUnit 暂只支持SECONDS,MINUTES,HOURS,DAYS");
            }
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:获取String类型值
     *
     * @param key 键
     * @return
     */
    public String get(String key) {
        return get_(getKey(key));
    }
    public String get(String key, boolean perfix) {
        if(!perfix)
            return get_(key);
        return get(key);
    }
    protected String get_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.get(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:获取String类型值
     *
     * @param key 键
     * @return
     */
    public byte[] get(byte[] key) {
        return get_(getKey(new String(key)).getBytes());
    }
    public byte[] get(byte[] key, boolean perfix) {
        if(!perfix)
            return get_(key);
        return get(key);
    }
    protected byte[] get_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.get(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:新增有序集合
     * 有序集合的成员是唯一的,但分数(score)却可以重复。
     * 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
     *
     * @param key 键
     * @return
     */
    public void zadd(String key, double score, String value) {
        zadd_(getKey(key), score, value);
    }
    public void zadd(String key, double score, String value, boolean perfix) {
        if(!perfix){
            zadd_(key, score, value);
            return;
        }
        zadd(key, score, value);
    }
    protected void zadd_(String key, double score, String value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.zadd(key, score, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:新增有序集合
     * 有序集合的成员是唯一的,但分数(score)却可以重复。
     * 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
     *
     * @param key 键
     * @return
     */
    public void zadd(byte[] key, double score, byte[] value) {
        zadd_(getKey(new String(key)).getBytes(), score, value);
    }
    public void zadd(byte[] key, double score, byte[] value, boolean perfix) {
        if(!perfix){
            zadd_(key, score, value);
            return;
        }
        zadd(key, score, value);
    }
    protected void zadd_(byte[] key, double score, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.zadd(key, score, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:返回有序集中指定区间内的成员,通过索引,分数从高到底
     *
     * @param key   键
     * @param start 开始
     * @param end   结束   查所有为-1  差第一条为0
     * @return
     */
    public Set zrevrange(String key, long start, long end) {
        return zrevrange_(getKey(key), start, end);
    }
    public Set zrevrange(String key, long start, long end, boolean perfix) {
        if(!perfix)
            return zrevrange_(key, start, end);
        return zrevrange(key, start, end);
    }
    protected Set zrevrange_(String key, long start, long end) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.zrevrange(key, start, end);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:返回有序集中指定区间内的成员,通过索引,分数从高到底
     *
     * @param key   键
     * @param start 开始
     * @param end   结束   查所有为-1  差第一条为0
     * @return
     */
    public Set zrevrange(byte[] key, long start, long end) {
        return zrevrange_(getKey(new String(key)).getBytes(), start, end);
    }
    public Set zrevrange(byte[] key, long start, long end, boolean perfix) {
        if(!perfix)
            return zrevrange_(key, start, end);
        return zrevrange(key, start, end);
    }
    protected Set zrevrange_(byte[] key, long start, long end) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.zrevrange(key, start, end);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:返回有序集中指定区间内的成员与分值,通过索引,分数从高到底
     *
     * @param key   键
     * @param start 开始
     * @param end   结束   查所有为-1  差第一条为0
     * @return
     */
    public Set zrevrangeWithScores(String key, long start, long end) {
        return zrevrangeWithScores(getKey(key), start, end);
    }
    public Set zrevrangeWithScores(String key, long start, long end, boolean perfix) {
        if(!perfix)
            return zrevrangeWithScores_(key, start, end);
        return zrevrangeWithScores(key, start, end);
    }
    protected Set zrevrangeWithScores_(String key, long start, long end) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.zrevrangeWithScores(key, start, end);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:返回有序集中指定区间内的成员与分值,通过索引,分数从高到底
     *
     * @param key   键
     * @param start 开始
     * @param end   结束   查所有为-1  差第一条为0
     * @return
     */
    public Set zrevrangeWithScores(byte[] key, long start, long end) {
        return zrevrangeWithScores_(getKey(new String(key)).getBytes(), start, end);
    }
    public Set zrevrangeWithScores(byte[] key, long start, long end, boolean perfix) {
        if(!perfix)
            return zrevrangeWithScores_(key, start, end);
        return zrevrangeWithScores(key, start, end);
    }
    protected Set zrevrangeWithScores_(byte[] key, long start, long end) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.zrevrangeWithScores(key, start, end);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 添加无序集合数据
     */
    public void sadd(String key, String... value) {
        sadd_(getKey(key), value);
    }
    public void sadd(boolean perfix, String key, String... value) {
        if(!perfix){
            sadd_(key, value);
            return;
        }
        sadd(key, value);
    }
    protected void sadd_(String key, String... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.sadd(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 添加无序集合数据
     */
    public void sadd(byte[] key, byte[]... value) {
        sadd_(getKey(new String(key)).getBytes(), value);
    }
    public void sadd(boolean perfix, byte[] key, byte[]... value) {
        if(!perfix){
            sadd_(key, value);
            return;
        }
        sadd(key, value);
    }
    protected void sadd_(byte[] key, byte[]... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.sadd(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 判断成员元素是否是集合的成员。
     */
    public Boolean sismember(String key, String value) {
        return sismember_(getKey(key), value);
    }
    public Boolean sismember(String key, String value, boolean perfix) {
        if(!perfix)
            return sismember_(key, value);
        return sismember(key, value);
    }
    protected Boolean sismember_(String key, String value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sismember(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 判断成员元素是否是集合的成员。
     */
    public Boolean sismember(byte[] key, byte[] value) {
        return sismember_(getKey(new String(key)).getBytes(), value);
    }
    public Boolean sismember(byte[] key, byte[] value, boolean perfix) {
        if(!perfix)
            return sismember_(key, value);
        return sismember(key, value);
    }
    protected Boolean sismember_(byte[] key, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sismember(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 移除集合中一个或多个成员
     */
    public Long srem(String key, String... value) {
        return srem_(getKey(key), value);
    }
    public Long srem(boolean perfix, String key, String... value) {
        if(!perfix)
            return srem_(key, value);
        return srem(key, value);
    }
    protected Long srem_(String key, String... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.srem(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 移除集合中一个或多个成员
     */
    public Long srem(byte[] key, byte[]... value) {
        return srem_(getKey(new String(key)).getBytes(), value);
    }
    public Long srem(boolean perfix, byte[] key, byte[]... value) {
        if(!perfix)
            return srem_(key, value);
        return srem(key, value);
    }
    protected Long srem_(byte[] key, byte[]... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.srem(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 获取集合成员数
     */
    public Long scard(String key) {
        return scard_(getKey(key));
    }
    public Long scard(String key, boolean perfix) {
        if(!perfix)
            return scard_(key);
        return scard(key);
    }
    protected Long scard_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.scard(key);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 获取集合成员数
     */
    public Long scard(byte[] key) {
        return scard_(getKey(new String(key)).getBytes());
    }
    public Long scard(byte[] key, boolean perfix) {
        if(!perfix)
            return scard_(key);
        return scard(key);
    }
    protected Long scard_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.scard(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:为队列头添加值
     *
     * @param key   队列名
     * @param value 值
     */
    public void rpush(String key, String... value) {
        rpush_(getKey(key), value);
    }
    public void rpush(boolean perfix, String key, String... value) {
        if(!perfix){
            rpush_(key, value);
            return;
        }
        rpush(key, value);
    }
    protected void rpush_(String key, String... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.rpush(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:为队列头添加值
     *
     * @param key   队列名
     * @param value 值
     */
    public void rpush(byte[] key, byte[]... value) {
        rpush_(getKey(new String(key)).getBytes(), value);
    }
    public void rpush(boolean perfix, byte[] key, byte[]... value) {
        if(!perfix){
            rpush_(key, value);
            return;
        }
        rpush(key, value);
    }
    protected void rpush_(byte[] key, byte[]... value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.rpush(key, value);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:从队列尾取出值
     *
     * @param key 队列名
     * @return
     */
    public String rpop(String key) {
        return rpop_(getKey(key));
    }
    public String rpop(String key, boolean perfix) {
        if(!perfix)
            return rpop_(key);
        return rpop(key);
    }
    protected String rpop_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.rpop(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:从队列尾取出值
     *
     * @param key 队列名
     * @return
     */
    public byte[] rpop(byte[] key) {
        return rpop_(getKey(new String(key)).getBytes());
    }
    public byte[] rpop(byte[] key, boolean perfix) {
        if(!perfix)
            return rpop_(key);
        return rpop(key);
    }
    protected byte[] rpop_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.rpop(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:判断SessionId是否存在缓存服务器中 参数:
     *
     * @param key sessionId
     * @return Boolean true:存在 false:不存在
     * @throws Exception
     */
    public Boolean exists(String key) {
        return exists_(getKey(key));
    }
    public Boolean exists(String key, boolean perfix) {
        if(!perfix)
            return exists_(key);
        return exists(key);
    }
    protected Boolean exists_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.exists(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:判断SessionId是否存在缓存服务器中 参数:
     *
     * @param key sessionId
     * @return Boolean true:存在 false:不存在
     * @throws Exception
     */
    public Boolean exists(byte[] key) {
        return exists_(getKey(new String(key)).getBytes());
    }
    public Boolean exists(byte[] key, boolean perfix) {
        if(!perfix)
            return exists_(key);
        return exists(key);
    }
    protected Boolean exists_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.exists(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:判断SessionId是否存在缓存服务器中,存在则删除
     *
     * @param key sessionId
     * @return Boolean true:删除 false:不存在
     * @throws Exception
     */
    public Long del(String key) {
        return del_(getKey(key));
    }
    public Long del(String key, boolean perfix) {
        if(!perfix)
            return del_(key);
        return del(key);
    }
    protected Long del_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            if (exists(key, false)) {
                return shardedJedis.del(key);
            }
            return (long) -1;
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:判断SessionId是否存在缓存服务器中,存在则删除
     *
     * @param key sessionId
     * @throws Exception
     */
    public Long del(byte[] key) {
        return del_(getKey(new String(key)).getBytes());
    }
    public Long del(byte[] key, boolean perfix) {
        if(!perfix)
            return del_(key);
        return del(key);
    }
    protected Long del_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            if (exists(key)) {
                return shardedJedis.del(key);
            }
            return (long) -1;
        } finally {
            shardedJedis.close();
        }
    }


    public Long incr(String key) {
        return incr_(getKey(key));
    }
    public Long incr(String key, boolean perfix) {
        if(!perfix)
            return incr_(key);
        return incr(key);
    }
    protected Long incr_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.incr(key);
        } finally {
            shardedJedis.close();
        }
    }


    public Long incr(byte[] key) {
        return incr_(getKey(new String(key)).getBytes());
    }
    public Long incr(byte[] key, boolean perfix) {
        if(!perfix)
            return incr_(key);
        return incr(key);
    }
    protected Long incr_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.incr(key);
        } finally {
            shardedJedis.close();
        }
    }

    public Long decr(String key) {
        return decr_(getKey(key));
    }
    public Long decr(String key, boolean perfix) {
        if(!perfix)
            return decr_(key);
        return decr(key);
    }
    protected Long decr_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.decr(key);
        } finally {
            shardedJedis.close();
        }
    }


    public Long decr(byte[] key) {
        return decr_(getKey(new String(key)).getBytes());
    }
    public Long decr(byte[] key, boolean perfix) {
        if(!perfix)
            return decr_(key);
        return decr(key);
    }
    protected Long decr_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.decr(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 功能:将数据存到队列中:
     *
     * @param key 队列名
     * @return
     */
    public void lpush(byte[] key, byte[] value) {
        lpush_(getKey(new String(key)).getBytes(), value);
    }
    public void lpush(byte[] key, byte[] value, boolean perfix) {
        if(!perfix){
            lpush_(key, value);
            return;
        }
        lpush(key, value);
    }
    protected void lpush_(byte[] key, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.lpush(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:将数据存到队列中:
     *
     * @param key 队列名
     */
    public void lpush(String key, String value) {
        lpush_(getKey(key), value);
    }
    public void lpush(String key, String value, boolean perfix) {
        if(!perfix){
            lpush_(key, value);
            return;
        }
        lpush(key, value);
    }
    protected void lpush_(String key, String value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            shardedJedis.lpush(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:获取队列中第一条:
     *
     * @param key 队列名
     * @return
     */
    public byte[] lpop(byte[] key) {
        return lpop(getKey(new String(key)).getBytes());
    }
    public byte[] lpop(byte[] key, boolean perfix) {
        if(!perfix)
            return lpop_(key);
        return lpop(key);
    }
    protected byte[] lpop_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.lpop(key);
        } finally {
            shardedJedis.close();
        }
    }

    /**
     * 功能:获取队列中第一条:
     *
     * @param key 队列名
     * @return
     */
    public String lpop(String key) {
        return lpop_(getKey(key));
    }
    public String lpop(String key, boolean perfix) {
        if(!perfix)
            return lpop_(key);
        return lpop(key);
    }
    protected String lpop_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.lpop(key);
        } finally {
            shardedJedis.close();
        }
    }


    public Long llen(byte[] key) {
        return llen_(getKey(new String(key)).getBytes());
    }
    public Long llen(byte[] key, boolean perfix) {
        if(!perfix)
            return llen_(key);
        return llen(key);
    }
    protected Long llen_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.llen(key);
        } finally {
            shardedJedis.close();
        }
    }

    public Long llen(String key) {
        return llen_(getKey(key));
    }
    public Long llen(String key, boolean perfix) {
        if(!perfix)
            return llen_(key);
        return llen(key);
    }
    protected Long llen_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.llen(key);
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 获取所有的Lists
     *
     * @param key
     * @return
     */
    public List lrange(String key) {
        return lrange_(getKey(key));
    }
    public List lrange(String key, boolean perfix) {
        if(!perfix)
            return lrange_(key);
        return lrange(key);
    }
    protected List lrange_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.lrange(key, 0, llen(key));
        } finally {
            shardedJedis.close();
        }
    }


    /**
     * 获取所有的Lists
     *
     * @param key
     * @return
     */
    public List lrange(byte[] key) {
        return lrange_(getKey(new String(key)).getBytes());
    }
    public List lrange(byte[] key, boolean perfix) {
        if(!perfix)
            return lrange_(key);
        return lrange(key);
    }
    protected List lrange_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.lrange(key, 0, llen(key));
        } finally {
            shardedJedis.close();
        }
    }


    public Long hset(String key, String field, String value) {
        return hset_(getKey(key), field, value);
    }
    public Long hset(String key, String field, String value, boolean perfix) {
        if(!perfix)
            return hset_(key, field, value);
        return hset(key, field, value);
    }
    protected Long hset_(String key, String field, String value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hset(key, field, value);
        } finally {
            shardedJedis.close();
        }
    }


    public Long hset(byte[] key, byte[] field, byte[] value) {
        return hset_(getKey(new String(key)).getBytes(), field, value);
    }
    public Long hset(byte[] key, byte[] field, byte[] value, boolean perfix) {
        if(!perfix)
            return hset_(key, field, value);
        return hset(key, field, value);
    }
    protected Long hset_(byte[] key, byte[] field, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hset(key, field, value);
        } finally {
            shardedJedis.close();
        }
    }


    public boolean hexists(String key, String field) {
        return hexists_(getKey(key), field);
    }
    public boolean hexists(String key, String field, boolean perfix) {
        if(!perfix)
            return hexists_(key, field);
        return hexists(key, field);
    }
    protected boolean hexists_(String key, String field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hexists(key, field);
        } finally {
            shardedJedis.close();
        }
    }


    public boolean hexists(byte[] key, byte[] field) {
        return hexists_(getKey(new String(key)).getBytes(), field);
    }
    public boolean hexists(byte[] key, byte[] field, boolean perfix) {
        if(!perfix)
            return hexists_(key, field);
        return hexists(key, field);
    }
    protected boolean hexists_(byte[] key, byte[] field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hexists(key, field);
        } finally {
            shardedJedis.close();
        }
    }


    public Map hgetAll(String key) {
        return hgetAll_(getKey(key));
    }
    public Map hgetAll(String key, boolean perfix) {
        if(!perfix)
            return hgetAll_(key);
        return hgetAll(key);
    }
    protected Map hgetAll_(String key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hgetAll(key);
        } finally {
            shardedJedis.close();
        }
    }


    public Map hgetAll(byte[] key) {
        return hgetAll_(getKey(new String(key)).getBytes());
    }
    public Map hgetAll(byte[] key, boolean perfix) {
        if(!perfix)
            return hgetAll_(key);
        return hgetAll(key);
    }
    protected Map hgetAll_(byte[] key) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hgetAll(key);
        } finally {
            shardedJedis.close();
        }
    }


    public String hget(String key, String field) {
        return hget_(getKey(key), field);
    }
    public String hget(String key, String field, boolean perfix) {
        if(!perfix)
            return hget_(key, field);
        return hget(key, field);
    }
    protected String hget_(String key, String field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hget(key, field);
        } finally {
            shardedJedis.close();
        }
    }


    public byte[] hget(byte[] key, byte[] field) {
        return hget_(getKey(new String(key)).getBytes(), field);
    }
    public byte[] hget(byte[] key, byte[] field, boolean perfix) {
        if(!perfix)
            return hget_(key, field);
        return hget(key, field);
    }
    protected byte[] hget_(byte[] key, byte[] field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hget(key, field);
        } finally {
            shardedJedis.close();
        }
    }

    public Long geoadd(String key, double longitude, double latitude, String member) {
        return geoadd_(getKey(key), longitude, latitude, member);
    }
    public Long geoadd(String key, double longitude, double latitude, String member, boolean perfix) {
        if(!perfix)
            return geoadd_(key, longitude, latitude, member);
        return geoadd(key, longitude, latitude, member);
    }
    public Long geoadd_(String key, double longitude, double latitude, String member) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geoadd(key, longitude, latitude, member);
        } finally {
            shardedJedis.close();
        }
    }

    public Long geoadd(String key, Map memberCoordinateMap) {
        return geoadd_(getKey(key), memberCoordinateMap);
    }
    public Long geoadd(String key, Map memberCoordinateMap, boolean perfix) {
        if(!perfix)
            return geoadd_(key, memberCoordinateMap);
        return geoadd(key, memberCoordinateMap);
    }
    public Long geoadd_(String key, Map memberCoordinateMap) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geoadd(key, memberCoordinateMap);
        } finally {
            shardedJedis.close();
        }
    }

    public Double geodist(String key, String member1, String member2) {
        return geodist_(getKey(key), member1, member2);
    }
    public Double geodist(String key, String member1, String member2, boolean perfix) {
        if(!perfix)
            return geodist_(key, member1, member2);
        return geodist(key, member1, member2);
    }
    public Double geodist_(String key, String member1, String member2) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geodist(key, member1, member2);
        } finally {
            shardedJedis.close();
        }
    }

    public Double geodist(String key, String member1, String member2, GeoUnit unit) {
        return geodist_(getKey(key), member1, member2, unit);
    }
    public Double geodist(String key, String member1, String member2, GeoUnit unit, boolean perfix) {
        if(!perfix)
            return geodist_(key, member1, member2, unit);
        return geodist(key, member1, member2, unit);
    }
    public Double geodist_(String key, String member1, String member2, GeoUnit unit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geodist(key, member1, member2, unit);
        } finally {
            shardedJedis.close();
        }
    }

    public List geohash(String key, String... members) {
        return geohash_(getKey(key), members);
    }
    public List geohash(String key, boolean perfix, String... members) {
        if(!perfix)
            return geohash_(key, members);
        return geohash(key, members);
    }
    public List geohash_(String key, String... members) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geohash(key, members);
        } finally {
            shardedJedis.close();
        }
    }

    public List geopos(String key, String... members) {
        return geopos_(getKey(key), members);
    }
    public List geopos(String key, boolean perfix, String... members) {
        if(!perfix)
            return geopos_(key, members);
        return geopos(key, members);
    }
    public List geopos_(String key, String... members) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.geopos(key, members);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadius(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        return georadius_(getKey(key), longitude, latitude, radius, unit);
    }
    public List georadius(String key, double longitude, double latitude, double radius, GeoUnit unit, boolean perfix) {
        if(!perfix)
            return georadius_(key, longitude, latitude, radius, unit);
        return georadius(key, longitude, latitude, radius, unit);
    }
    public List georadius_(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadius(key, longitude, latitude, radius, unit);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        return georadiusReadonly_(getKey(key), longitude, latitude, radius, unit);
    }
    public List georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit, boolean perfix) {
        if(!perfix)
            return georadiusReadonly_(key, longitude, latitude, radius, unit);
        return georadiusReadonly(key, longitude, latitude, radius, unit);
    }
    public List georadiusReadonly_(String key, double longitude, double latitude, double radius, GeoUnit unit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusReadonly(key, longitude, latitude, radius, unit);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadius(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        return georadius_(getKey(key), longitude, latitude, radius, unit, param);
    }
    public List georadius(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param, boolean perfix) {
        if(!perfix)
            return georadius_(key, longitude, latitude, radius, unit, param);
        return georadius(key, longitude, latitude, radius, unit, param);
    }
    public List georadius_(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadius(key, longitude, latitude, radius, unit, param);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        return georadiusReadonly_(getKey(key), longitude, latitude, radius, unit, param);
    }
    public List georadiusReadonly(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param, boolean perfix) {
        if(!perfix)
            return georadiusReadonly_(key, longitude, latitude, radius, unit, param);
        return georadiusReadonly(key, longitude, latitude, radius, unit, param);
    }
    public List georadiusReadonly_(String key, double longitude, double latitude, double radius, GeoUnit unit, GeoRadiusParam param) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusReadonly(key, longitude, latitude, radius, unit, param);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusByMember(String key, String member, double radius, GeoUnit unit) {
        return georadiusByMember_(getKey(key), member, radius, unit);
    }
    public List georadiusByMember(String key, String member, double radius, GeoUnit unit, boolean perfix) {
        if(!perfix)
            return georadiusByMember_(key, member, radius, unit);
        return georadiusByMember(key, member, radius, unit);
    }
    public List georadiusByMember_(String key, String member, double radius, GeoUnit unit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusByMember(key, member, radius, unit);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit) {
        return georadiusByMemberReadonly_(getKey(key), member, radius, unit);
    }
    public List georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit, boolean perfix) {
        if(!perfix)
            return georadiusByMemberReadonly_(key, member, radius, unit);
        return georadiusByMemberReadonly(key, member, radius, unit);
    }
    public List georadiusByMemberReadonly_(String key, String member, double radius, GeoUnit unit) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusByMemberReadonly(key, member, radius, unit);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusByMember(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        return georadiusByMember_(getKey(key), member, radius, unit, param);
    }
    public List georadiusByMember(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param, boolean perfix) {
        if(!perfix)
            return georadiusByMember_(key, member, radius, unit, param);
        return georadiusByMember(key, member, radius, unit, param);
    }
    public List georadiusByMember_(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusByMember(key, member, radius, unit, param);
        } finally {
            shardedJedis.close();
        }
    }

    public List georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        return georadiusByMemberReadonly_(getKey(key), member, radius, unit, param);
    }
    public List georadiusByMemberReadonly(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param, boolean perfix) {
        if(!perfix)
            return georadiusByMemberReadonly_(key, member, radius, unit, param);
        return georadiusByMemberReadonly(key, member, radius, unit, param);
    }
    public List georadiusByMemberReadonly_(String key, String member, double radius, GeoUnit unit, GeoRadiusParam param) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.georadiusByMemberReadonly(key, member, radius, unit, param);
        } finally {
            shardedJedis.close();
        }
    }

    public Long hdel(String key, String field) {
        return hdel_(getKey(key), field);
    }
    public Long hdel(String key, String field, boolean perfix) {
        if(!perfix)
            return hdel_(key, field);
        return hdel(key, field);
    }
    protected Long hdel_(String key, String field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hdel(key, field);
        } finally {
            shardedJedis.close();
        }
    }


    public Long hdel(byte[] key, byte[] field) {
        return hdel_(getKey(new String(key)).getBytes(), field);
    }
    public Long hdel(byte[] key, byte[] field, boolean perfix) {
        if(!perfix)
            return hdel_(key, field);
        return hdel(key, field);
    }
    protected Long hdel_(byte[] key, byte[] field) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.hdel(key, field);
        } finally {
            shardedJedis.close();
        }
    }

    public ScanResult sscan(String key, String cursor, ScanParams scanParams) {
        return sscan_(getKey(key), cursor, scanParams);
    }
    public ScanResult sscan(String key, String cursor, ScanParams scanParams, boolean perfix) {
        if(!perfix)
            return sscan_(key, cursor, scanParams);
        return sscan(key, cursor, scanParams);
    }
    protected ScanResult sscan_(String key, String cursor, ScanParams scanParams) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sscan(key, cursor, scanParams);
        } finally {
            shardedJedis.close();
        }
    }

    public ScanResult sscan(byte[] key, byte[] cursor, ScanParams scanParams) {
        return sscan_(getKey(new String(key)).getBytes(), cursor, scanParams);
    }
    public ScanResult sscan(byte[] key, byte[] cursor, ScanParams scanParams, boolean perfix) {
        if(!perfix)
            return sscan_(key, cursor, scanParams);
        return sscan(key, cursor, scanParams);
    }
    protected ScanResult sscan_(byte[] key, byte[] cursor, ScanParams scanParams) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sscan(key, cursor, scanParams);
        } finally {
            shardedJedis.close();
        }
    }

    public ScanResult sscan(String key, String cursor) {
        return sscan_(getKey(key), cursor);
    }
    public ScanResult sscan(String key, String cursor, boolean perfix) {
        if(!perfix)
            return sscan_(key, cursor);
        return sscan(key, cursor);
    }
    protected ScanResult sscan_(String key, String cursor) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sscan(key, cursor);
        } finally {
            shardedJedis.close();
        }
    }

    public ScanResult sscan(byte[] key, byte[] cursor) {
        return sscan_(getKey(new String(key)).getBytes(), cursor);
    }
    public ScanResult sscan(byte[] key, byte[] cursor, boolean perfix) {
        if(!perfix)
            return sscan_(key, cursor);
        return sscan(key, cursor);
    }
    protected ScanResult sscan_(byte[] key, byte[] cursor) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.sscan(key, cursor);
        } finally {
            shardedJedis.close();
        }
    }

    public Long setnx(byte[] key, byte[] value) {
        return setnx_(getKey(new String(key)).getBytes(), value);
    }
    public Long setnx(byte[] key, byte[] value, boolean perfix) {
        if(!perfix)
            return setnx_(key, value);
        return setnx(key, value);
    }
    protected Long setnx_(byte[] key, byte[] value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.setnx(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    public Long setnx(String key, String value) {
        return setnx_(getKey(key), value);
    }
    public Long setnx(String key, String value, boolean perfix) {
        if(!perfix)
            return setnx_(key, value);
        return setnx(key, value);
    }
    protected Long setnx_(String key, String value) {
        ShardedJedis shardedJedis = getShardedJedis();
        try {
            return shardedJedis.setnx(key, value);
        } finally {
            shardedJedis.close();
        }
    }

    public final String LOCK_SUCCESS = "OK";
    private final Long RELEASE_SUCCESS = 1L;

    /**
     * 尝试获取锁(租塞,要么直接成功,要么等待直到获取锁成功)
     *
     * @param lockKey    锁
     * @param requestId  请求标识,谁加锁谁就负责解锁,可用uuid之类
     * @param expireTime 超期时间(过期时间)
     * @return 是否获取成功
     */
    public boolean lock(String lockKey, String requestId, int expireTime) {
        while (null == set(lockKey, requestId, SetParams.setParams().nx().ex(expireTime))) {
        }
        return true;
    }

    /**
     * 尝试获取锁(租塞,不一定能成功。当别人正在持有锁的时候,加锁的线程需要继续尝试。
     * 这个“继续尝试”通常是“忙等待”,超过指定的obtainTime时间还未获取到锁,直接放弃失败)
     *
     * @param lockKey    锁
     * @param requestId  请求标识,谁加锁谁就负责解锁,可用uuid之类
     * @param obtainTime 获取锁时间
     * @param expireTime 超期时间(过期时间)
     * @return 是否获取成功
     */
    public boolean lock(String lockKey, String requestId, int obtainTime, int expireTime) {
        return lock_(lockKey, requestId, obtainTime, expireTime, true);
    }
    public boolean lock(String lockKey, String requestId, int obtainTime, int expireTime, boolean perfix) {
        return lock_(lockKey, requestId, obtainTime, expireTime, perfix);
    }
    protected boolean lock_(String lockKey, String requestId, int obtainTime, int expireTime, boolean perfix) {
        final long start = System.currentTimeMillis();
        final long end = start + obtainTime * 1000;
        boolean result = false; // 默认返回失败
        while (!(result = null != set(lockKey, requestId, SetParams.setParams().nx().ex(expireTime), perfix))) {
            if (System.currentTimeMillis() > end) {
                break;
            }
        }
        return result;
    }

    /**
     * 尝试获取锁(非租塞,要么成功,要么失败,不等待)
     *
     * @param lockKey    锁
     * @param requestId  请求标识,谁加锁谁就负责解锁,可用uuid之类
     * @param expireTime 超期时间(过期时间)
     * @return 是否获取成功
     */
    public boolean tryLock(String lockKey, String requestId, int expireTime) {
        return tryLock_(lockKey, requestId, expireTime, true);
    }
    public boolean tryLock(String lockKey, String requestId, int expireTime, boolean perfix) {
        return tryLock_(lockKey, requestId, expireTime, perfix);
    }
    protected boolean tryLock_(String lockKey, String requestId, int expireTime, boolean perfix) {
        String result = set(lockKey, requestId, SetParams.setParams().nx().ex(expireTime), perfix);
        if (null != result && LOCK_SUCCESS.equals(result)) {
            return true;
        }
        return false;
    }

    /**
     * 释放锁
     *
     * @param lockKey   锁
     * @param requestId 请求标识
     * @return 是否释放成功
     */
    public boolean unLock(String lockKey, String requestId) {
        return unLock_(lockKey, requestId, true);
    }
    public boolean unLock(String lockKey, String requestId, boolean perfix) {
        return unLock_(lockKey, requestId, perfix);
    }
    protected boolean unLock_(String lockKey, String requestId, boolean perfix) {
        if(perfix)
            lockKey = getKey(lockKey);
        Jedis jedis = getJedis();
        try {
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            Object result = jedis.eval(script, Collections.singletonList(lockKey), Collections.singletonList(requestId));
            if (RELEASE_SUCCESS.equals(result)) {
                return true;
            }
        } finally {
            jedis.close();
        }
        return false;
    }

    protected String getKey(String key) {
        if (null != redisConfig && null != redisConfig.getPrefix() && !key.startsWith(":")) {
            key = redisConfig.getPrefix().endsWith(":") ? redisConfig.getPrefix().concat(key) : redisConfig.getPrefix().concat(":").concat(key);
        }
        if (key.startsWith(":"))
            key = key.substring(1);
        return key;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy