当前位置:   article > 正文

redis工具类设计之回调函数的使用_rediscallback

rediscallback

前言

我们平时在项目中手写共同工具类的时候, 对于好多异常的处理很是头疼, 对于各种连接, 各种io的开启关闭控制都是一个头大的事情, 放在具体使用阶段进行处理又怕别人使用的时候忘记关闭. 这个时候回调函数的作用就体现出来了.

代码

回调函数需要一个接口, 当然写到类中也是没有问题的:

public class RedisTemplate {
    private Jedis jedis;

    public RedisTemplate(Jedis jedis) {
        this.jedis = jedis;
    }

    public <T> T execute(RedisCallback<T> callback) throws Exception {
        try {
            return callback.handle(jedis);
        }
        catch (Exception e) {
            throw e;
        }
        finally {
            returnResource(jedis);
        }
    }

    private void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    public interface RedisCallback<T> {
        public T handle(Jedis jedis);
    }
}
  • 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

如下是一个简化版的redis工具类, 对连接使用后进行了关闭:
这里没有加入bitmap的代码, 读者根据需要可自行加入. 另外添加了一些用于页面操作redis的方法, 能更灵活的控制redis中的数据, 不用访问客户端, 也会更直观的观察到库中数据, 如果库中数据量很大, 建议对redis进行分库, 本工具类对管理人员开放了getPrex+*的模糊查询方法, 会对大数据量的redis库产生一定压力. 慎用

public class RedisUtil {

    private static JedisPool jedisPool = null;

    private static JedisPool getJedisPool() {
        if (jedisPool == null) {
            PropUtils PROPS = new PropUtils(PropUtils.class.getResource("/config/redis.properties"));
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(Integer.parseInt(PROPS.getValue("redis.maxTotal"))); // 可用连接实例的最大数目,如果赋值为-1,表示不限制.
            config.setMaxIdle(Integer.parseInt(PROPS.getValue("redis.maxIdle"))); // 控制一个Pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8
            config.setMaxWaitMillis(Integer.parseInt(PROPS.getValue("redis.maxWaitMillis"))); // 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时/如果超过等待时间,则直接抛出异常
            config.setTestOnBorrow(Boolean.parseBoolean(PROPS.getValue("redis.testOnBorrow"))); // 在borrow一个jedis实例时,是否提前进行validate操作,如果为true,则得到的jedis实例均是可用的
            jedisPool = new JedisPool(config, PROPS.getValue("redis.host"), Integer.parseInt(PROPS.getValue("redis.port")));
        }
        return jedisPool;
    }

    public static Jedis getResource() {
        Jedis jedis = getJedisPool().getResource();
        jedis.select(10);
        return jedis;
    }

    public static void returnResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /*
     * 清除数据库所有数据
     */
    public static String flushAll() throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.flushAll();
            }
        });
    }

    /*
     * 清除当前id数据库所有数据
     */
    public static String flushDB() throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.flushDB();
            }
        });
    }

    /*
     * 通过key查找存储类型
     */
    public static String getType(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.type(key);
            }
        });
    }

    /*
     * key是否存在
     */
    public static Boolean hasKey(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Boolean>() {
            @Override
            public Boolean handle(Jedis jedis) {
                return jedis.exists(key);
            }
        });
    }

    /*
     * 多个key存在的个数
     */
    public static Long hasKeys(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.exists(keys);
            }
        });
    }

    /*
     * 删除key
     */
    public static Long del(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.del(keys);
            }
        });
    }

    /*
     * 模糊查询所有符合条件的key
     */
    public static Set<String> keys(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.keys(key);
            }
        });
    }

    /*
     * key追加字符串
     */
    public static Long keyAppend(final String key, final String keySuffix) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.append(key, keySuffix);
            }
        });
    }

    /*
     * key的长度
     */
    public static Long strlen(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.strlen(key);
            }
        });
    }

    /*
     * 设置key的有效时间 秒
     */
    public static Long expire(final String key, final int time) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.expire(key, time);
            }
        });
    }

    /*
     * 查询key的剩余有效时间 永久返回-1 超时返回-2
     */
    public static Long ttl(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.ttl(key);
            }
        });
    }

    /*
     * key变为永久有效
     */
    public static Long makePersist(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.persist(key);
            }
        });
    }

    /*
     * 设置key在指定时间失效
     */
    public static Long expireAt(final String key, final long time) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.expireAt(key, time);
            }
        });
    }

    /*
     * 插入key-string
     */
    public static String set(final String key, final String value) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.set(key, value);
            }
        });
    }

    /*
     * 通过key查找String
     */
    public static String get(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.get(key);
            }
        });
    }

    /*
     * 通过多个key和value设置多个key-value对
     */
    public static String mset(final String... keyValues) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.mset(keyValues);
            }
        });
    }

    /*
     * 通过多个key查询多个string类型的value
     */
    public static List<String> mget(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<String>>() {
            @Override
            public List<String> handle(Jedis jedis) {
                return jedis.mget(keys);
            }
        });
    }

    /*
     * key的value加1
     */
    public static Long incr(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.incr(key);
            }
        });
    }

    /*
     * key的value减1
     */
    public static Long decr(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.decr(key);
            }
        });
    }

    /*
     * key的value加num
     */
    public static Long incrBy(final String key, final Long num) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.incrBy(key, num);
            }
        });
    }

    /*
     * key的value减num
     */
    public static Long decrBy(final String key, final Long num) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.decrBy(key, num);
            }
        });
    }

    /*
     * key的value加num和-num
     */
    public static Double incrByFloat(final String key, final double num) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Double>() {
            @Override
            public Double handle(Jedis jedis) {
                return jedis.incrByFloat(key, num);
            }
        });
    }

    /*
     * 插入key-map 一条map数据
     */
    public static Long hset(final String key, final String mapKey, final String mapValue) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.hset(key, mapKey, mapValue);
            }
        });
    }

    /*
     * 通过key和mapKey查找mapValue
     */
    public static String hget(final String key, final String mapKey) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.hget(key, mapKey);
            }
        });
    }

    /*
     * 插入key-map多条map数据
     */
    public static String hmset(final String key, final Map value) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.hmset(key, value);
            }
        });
    }

    /*
     * 通过key和多条mapKey查找多条mapValue
     */
    public static List<String> hmget(final String key, final String... mapKey) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<String>>() {
            @Override
            public List<String> handle(Jedis jedis) {
                return jedis.hmget(key, mapKey);
            }
        });
    }

    /*
     * 删除key中多条mapKey的数据
     */
    public static Long hdel(final String key, final String... mapKeys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.hdel(key, mapKeys);
            }
        });
    }

    /*
     * 查询key中mapKey是否存在
     */
    public static Boolean hexists(final String key, final String mapKey) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Boolean>() {
            @Override
            public Boolean handle(Jedis jedis) {
                return jedis.hexists(key, mapKey);
            }
        });
    }

    /*
     * 通过key查找map所有key
     */
    public static Set<String> hkeys(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.hkeys(key);
            }
        });
    }

    /*
     * 通过key查找map所有value
     */
    public static List<String> hvals(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<String>>() {
            @Override
            public List<String> handle(Jedis jedis) {
                return jedis.hvals(key);
            }
        });
    }

    /*
     * 通过key查找map中的个数
     */
    public static Long hlen(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.hlen(key);
            }
        });
    }

    /*
     * 通过key查找map所有数据
     */
    public static Map<String, String> hgetAll(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Map<String, String>>() {
            @Override
            public Map<String, String> handle(Jedis jedis) {
                return jedis.hgetAll(key);
            }
        });
    }

    /*
     * 为key和mapKey的value增加数值 value必须是数字 key不存在将创建初始值为0
     */
    public static Long hincrBy(final String key, final String mapKey, final Long increaseByNum) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.hincrBy(key, mapKey, increaseByNum);
            }
        });
    }

    /*
     * 插入list
     */
    public static Long rPush(final String key, final String value) throws Exception{
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            public Long handle(Jedis jedis) {
                return jedis.rpush(key, value);
            }
        });
    }

    /*
     * 插入list
     */
    public static Long lPush(final String key, final String value) throws Exception{
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            public Long handle(Jedis jedis) {
                return jedis.lpush(key, value);
            }
        });
    }

    /*
     * 通过key查找list
     * 其中 0 表示第一个元素 -1 表示最后一个元素 -2 表示倒数第二个元素
     */
    public static List<String> lrange(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<String>>() {
            @Override
            public List<String> handle(Jedis jedis) {
                return jedis.lrange(key, start, end);
            }
        });
    }

    /*
     * 取出list最后一个值,并删除此值
     */
    public static String rpop(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            public String handle(Jedis jedis) {
                return jedis.rpop(key);
            }
        });
    }

    /*
     * 取出list第一个值,并删除此值
     */
    public static String lpop(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            public String handle(Jedis jedis) {
                return jedis.lpop(key);
            }
        });
    }

    /*
     * 操作会被阻塞 最多等待时间 time 如果列表为空返回一个nil
     * 否则返回一个含有两个元素的列表 第一个元素是被弹出元素所属的 key 第二个元素是被弹出元素的值
     */
    public static List<String> brpop( final int time, final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<String>>() {
            public List<String> handle(Jedis jedis) {
                return jedis.brpop(time, keys);
            }
        });
    }

    /*
     * list按index截取区间 多的删掉
     */
    public static String ltrim(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.ltrim(key, start, end);
            }
        });
    }

    /*
     * list删除全部等值value的元素 0为全部 负值为从尾部开始
     */
    public static Long lrem(final String key, final long count, final String value) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.lrem(key, count, value);
            }
        });
    }

    /*
     * 插入key-set
     */
    public static Long sadd(final String key, final String... members) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.sadd(key, members);
            }
        });
    }

    /*
     * 通过key查找set
     */
    public static Set<String> smembers(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.smembers(key);
            }
        });
    }

    /*
     * set是否包含member
     */
    public static Boolean sismember(final String key, final String member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Boolean>() {
            @Override
            public Boolean handle(Jedis jedis) {
                return jedis.sismember(key, member);
            }
        });
    }

    /*
     * set删除members
     */
    public static Long srem(final String key, final String... members) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.srem(key, members);
            }
        });
    }

    /*
     * 随机删除一个元素
     */
    public static String spop(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                return jedis.spop(key);
            }
        });
    }

    /*
     * 返回元素个数
     */
    public static Long scard(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.scard(key);
            }
        });
    }

    /*
     * 显示第一个set中不存在于之后所有set中的元素
     */
    public static Set<String> sdiff(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.sdiff(keys);
            }
        });
    }

    /*
     * 返回所有set的交集
     */
    public static Set<String> sinter(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.sinter(keys);
            }
        });
    }

    /*
     * 返回所有set的并集
     */
    public static Set<String> sunion(final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.sunion(keys);
            }
        });
    }

    /*
     * 将给定集合之间的交集存储在指定的集合中 如果指定的集合已经存在 则将其覆盖
     */
    public static Long sinterstore(final String key, final String... keys) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.sinterstore(key, keys);
            }
        });
    }

    /*
     * 插入key-(分值 value)
     */
    public static Long zadd(final String key, final double score, final String member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.zadd(key, score, member);
            }
        });
    }

    /*
     * key中member的分数增加score
     */
    public static Double zincrby(final String key, final double score, final String member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Double>() {
            @Override
            public Double handle(Jedis jedis) {
                return jedis.zincrby(key, score, member);
            }
        });
    }

    /*
     * 通过key value 查找 分值
     */
    public static Double zscore(final String key, final String member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Double>() {
            @Override
            public Double handle(Jedis jedis) {
                return jedis.zscore(key, member);
            }
        });
    }

    /*
     * 通过key value 查找 下标
     */
    public static Long zrank(final String key, final String member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.zrank(key, member);
            }
        });
    }

    /*
     * 删除元素
     */
    public static Long zrem(final String key, final String... member) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.zrem(key, member);
            }
        });
    }

    /*
     * 通过分数查找个数
     */
    public static Long zcount(final String key, final double score1, final double score2) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.zcount(key, score1, score2);
            }
        });
    }

    /*
     * 返回元素个数
     */
    public static Long zcard(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                return jedis.zcard(key);
            }
        });
    }

    /*
     * 对有序集合基于下标进行排序 从小到大
     */
    public static Set<String> zrange(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.zrange(key, start, end);
            }
        });
    }

    /*
     * 对有序集合基于下标进行排序 从大到小
     */
    public static Set<String> zrevrange(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.zrevrange(key, start, end);
            }
        });
    }

    /*
     * 对有序集合基于评分进行排序 从小到大
     */
    public static Set<String> zrangeByScore(final String key, final double min, final double max) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.zrangeByScore(key, min, max);
            }
        });
    }

    /*
     * 对有序集合基于评分进行排序 从大到小
     */
    public static Set<String> zrevrangeByScore(final String key, final double min, final double max) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.zrevrangeByScore(key, min, max);
            }
        });
    }

    /*
     * 对有序集合基于元素进行排序 从小到大
     */
    public static Set<String> zrangeByLex(final String key, final String min, final String max) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<String>>() {
            @Override
            public Set<String> handle(Jedis jedis) {
                return jedis.zrangeByLex(key, min, max);
            }
        });
    }

    /*
     * 对有序集合基于下标进行排序 从小到大 返回元祖和分数
     */
    public static Set<Tuple> zrangeWithScores(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Set<Tuple>>() {
            @Override
            public Set<Tuple> handle(Jedis jedis) {
                return jedis.zrangeWithScores(key, start, end);
            }
        });
    }


    /*
     * 对有序集合基于下标进行排序 从小到大 返回元素和分数
     */
    public static List<Map<String, Double>> zrangeByIndexAll(final String key, final long start, final long end) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<Map<String, Double>>>() {
            @Override
            public List<Map<String, Double>> handle(Jedis jedis) {
                Set<Tuple> set = jedis.zrangeWithScores(key, start, end);
                List<Map<String, Double>> list = new ArrayList<Map<String, Double>>();
                for (Tuple tuple : set) {
                    Map<String, Double> map = new HashMap<String, Double>();
                    map.put(tuple.getElement(), tuple.getScore());
                    list.add(map);
                }
                return list;
            }
        });
    }


    /*
     * 对有序集合基于分数进行排序 从大到小 返回元素和分数
     */
    public static List<Map<String, Double>> zrevrangeByScoreAll(final String key, final double max, final double min) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<Map<String, Double>>>() {
            @Override
            public List<Map<String, Double>> handle(Jedis jedis) {
                Set<Tuple> set = jedis.zrevrangeByScoreWithScores(key, max, min);
                List<Map<String, Double>> list = new ArrayList<Map<String, Double>>();
                for (Tuple tuple : set) {
                    Map<String, Double> map = new HashMap<String, Double>();
                    map.put(tuple.getElement(), tuple.getScore());
                    list.add(map);
                }
                return list;
            }
        });
    }

    /*
     * 通过前缀删除所有key
     */
    public static Long deleteByPrex(final String prex) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<Long>() {
            @Override
            public Long handle(Jedis jedis) {
                Set<String> set = jedis.keys(prex+"*");
                if (set.size()==0) {
                    return 0L;
                }
                return jedis.del(set.toArray(new String[0]));
            }
        });
    }

    /*
     * 通过key模糊搜索所有key-value
     */
    public static List getListLike(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<Map<String, String>>>() {
            @Override
            public List<Map<String, String>> handle(Jedis jedis) {
                List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                Set<String> set = jedis.keys(key);
                if (set.size()==0) {
                    return list;
                }
                String[] setArray = set.toArray(new String[0]);
                List<String> setList= jedis.mget(setArray);
                for (int i=0;i<setArray.length;i++) {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("SELECTKEY", setArray[i]);
                    map.put("CONCATVALUE", setList.get(i));
                    list.add(map);
                }
                return list;
            }
        });
    }

    /*
     * 通过key模糊搜索所有不包含key的key-value
     */
    public static List getListUnLike(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List<Map<String, String>>>() {
            @Override
            public List<Map<String, String>> handle(Jedis jedis) {
                Set<String> setAll = jedis.keys("*");
                Set<String> setUn = jedis.keys(key);
                setAll.removeAll(setUn);
                List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                if (setAll.size()==0) {
                    return list;
                }
                String[] setArray = setAll.toArray(new String[0]);
                List<String> setList= jedis.mget(setArray);
                for (int i=0;i<setArray.length;i++) {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("SELECTKEY", setArray[i]);
                    map.put("CONCATVALUE", setList.get(i));
                    list.add(map);
                }
                return list;
            }
        });
    }

    /*
     * 通过key查找value转换成list返回
     */
    public static List getList(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<List>() {
            @Override
            public List handle(Jedis jedis) {
                List list = new ArrayList();
                list = new Gson().fromJson(jedis.get(key), list.getClass());
                return list;
            }
        });
    }

    /*
     * 通过key查找type分别查找转换成string返回
     */
    public static String getByAllType(final String key) throws Exception {
        RedisTemplate redisTemplate = new RedisTemplate(getResource());
        return redisTemplate.execute(new RedisTemplate.RedisCallback<String>() {
            @Override
            public String handle(Jedis jedis) {
                String type = jedis.type(key);
                String value = "";
                if (type.equals("string")) {
                    value = jedis.get(key);
                } else if (type.equals("hash")) {
                    value = JSON.toJSONString(jedis.hgetAll(key));
                } else if (type.equals("list")) {
                    value = String.valueOf(jedis.lrange(key, 0, -1));
                } else if (type.equals("set")) {
                    value = String.valueOf(jedis.smembers(key));
                } else if (type.equals("zset")) {
                    Set<Tuple> set = jedis.zrangeWithScores(key, 0, -1);
                    value = "[";
                    for (Tuple tuple : set) {
                        value += "(" + tuple.getElement() + "," + String.valueOf(tuple.getScore()) + "),";
                    }
                    value = set.size()>0?value.substring(0,value.length()-1):value;
                    value += "]";
                } else if (type.equals("none")) {
                    value = "key is not exist";
                } else {
                    value = "value is unknown type";
                }
                return value;
            }
        });
    }
}

  • 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
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991
  • 992
  • 993
  • 994
  • 995
  • 996
  • 997
  • 998
  • 999
  • 1000
  • 1001
  • 1002
  • 1003
  • 1004
  • 1005
  • 1006
  • 1007
  • 1008
  • 1009
  • 1010
  • 1011
  • 1012
  • 1013
  • 1014
  • 1015
  • 1016
  • 1017
  • 1018
  • 1019
  • 1020
  • 1021
  • 1022
  • 1023
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/318406
推荐阅读
相关标签
  

闽ICP备14008679号