当前位置: 移动技术网 > IT编程>开发语言>Java > (入门SpringBoot)SpringBoot结合redis(四)

(入门SpringBoot)SpringBoot结合redis(四)

2019年05月27日  | 移动技术网IT编程  | 我要评论

   springboot整合redis:

  1.引入jar

  <!--  引入redis依赖 -->
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-data-redis</artifactid>
    <!-- 排除redis默认客户端lettuce -->
    <exclusions>
        <exclusion>
            <groupid>io.lettuce</groupid>
            <artifactid>lettuce-core</artifactid>
        </exclusion>
    </exclusions>
</dependency>
<!-- 引入redis客户端驱动jedis -->
<dependency>
    <groupid>redis.clients</groupid>
    <artifactid>jedis</artifactid>
</dependency>

 2.设定资源文件:

#redis的配置:
#redis数据库索引(默认是0)
spring.redis.database=1
# redis服务器地址:
spring.redis.host=localhost
# redis服务器连接端口
spring.redis.port=6379
# redis服务器连接密码(默认为空)
spring.redis.password=123456
# redis连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=2000ms
# 连接池最大空闲数量:
spring.redis.jedis.pool.max-idle=10
# 连接池最小空闲时间:
spring.redis.jedis.pool.min-idle=5
# 连接超时时间(毫秒)
spring.redis.timeout=10000ms

     3.在controller使用:

@autowired
private redisutil redisutil;

  附带一个redisutil

package com.account.demo.config;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.stereotype.component;
import org.springframework.data.redis.connection.datatype;
import org.springframework.data.redis.core.cursor;
import org.springframework.data.redis.core.scanoptions;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.data.redis.core.zsetoperations.typedtuple;

import java.util.collection;
import java.util.date;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import java.util.set;
import java.util.concurrent.timeunit;
/**
 * redis工具类
 *
 * @author wangfan
 * @date 2018-02-24 下午03:09:50
 * @version 1.1 (github文档: https://github.com/whvcse/redisutil )
 */
@component
public class redisutil {
    @autowired
    private stringredistemplate redistemplate;

    /** -------------------key相关操作--------------------- */

    /**
     * 删除key
     *
     * @param key
     */
    public void delete(string key) {
        redistemplate.delete(key);
    }

    /**
     * 批量删除key
     *
     * @param keys
     */
    public void delete(collection<string> keys) {
        redistemplate.delete(keys);
    }

    /**
     * 序列化key
     *
     * @param key
     * @return
     */
    public byte[] dump(string key) {
        return redistemplate.dump(key);
    }

    /**
     * 是否存在key
     *
     * @param key
     * @return
     */
    public boolean haskey(string key) {
        return redistemplate.haskey(key);
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param timeout
     * @param unit
     * @return
     */
    public boolean expire(string key, long timeout, timeunit unit) {
        return redistemplate.expire(key, timeout, unit);
    }

    /**
     * 设置过期时间
     *
     * @param key
     * @param date
     * @return
     */
    public boolean expireat(string key, date date) {
        return redistemplate.expireat(key, date);
    }

    /**
     * 查找匹配的key
     *
     * @param pattern
     * @return
     */
    public set<string> keys(string pattern) {
        return redistemplate.keys(pattern);
    }

    /**
     * 将当前数据库的 key 移动到给定的数据库 db 当中
     *
     * @param key
     * @param dbindex
     * @return
     */
    public boolean move(string key, int dbindex) {
        return redistemplate.move(key, dbindex);
    }

    /**
     * 移除 key 的过期时间,key 将持久保持
     *
     * @param key
     * @return
     */
    public boolean persist(string key) {
        return redistemplate.persist(key);
    }

    /**
     * 返回 key 的剩余的过期时间
     *
     * @param key
     * @param unit
     * @return
     */
    public long getexpire(string key, timeunit unit) {
        return redistemplate.getexpire(key, unit);
    }

    /**
     * 返回 key 的剩余的过期时间
     *
     * @param key
     * @return
     */
    public long getexpire(string key) {
        return redistemplate.getexpire(key);
    }

    /**
     * 从当前数据库中随机返回一个 key
     *
     * @return
     */
    public string randomkey() {
        return redistemplate.randomkey();
    }

    /**
     * 修改 key 的名称
     *
     * @param oldkey
     * @param newkey
     */
    public void rename(string oldkey, string newkey) {
        redistemplate.rename(oldkey, newkey);
    }

    /**
     * 仅当 newkey 不存在时,将 oldkey 改名为 newkey
     *
     * @param oldkey
     * @param newkey
     * @return
     */
    public boolean renameifabsent(string oldkey, string newkey) {
        return redistemplate.renameifabsent(oldkey, newkey);
    }

    /**
     * 返回 key 所储存的值的类型
     *
     * @param key
     * @return
     */
    public datatype type(string key) {
        return redistemplate.type(key);
    }

    /** -------------------string相关操作--------------------- */

    /**
     * 设置指定 key 的值
     *
     * @param key
     * @param value
     */
    public void set(string key, string value) {
        redistemplate.opsforvalue().set(key, value);
    }

    /**
     * 获取指定 key 的值
     *
     * @param key
     * @return
     */
    public string get(string key) {
        return redistemplate.opsforvalue().get(key);
    }

    /**
     * 返回 key 中字符串值的子字符
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public string getrange(string key, long start, long end) {
        return redistemplate.opsforvalue().get(key, start, end);
    }

    /**
     * 将给定 key 的值设为 value ,并返回 key 的旧值(old value)
     *
     * @param key
     * @param value
     * @return
     */
    public string getandset(string key, string value) {
        return redistemplate.opsforvalue().getandset(key, value);
    }

    /**
     * 对 key 所储存的字符串值,获取指定偏移量上的位(bit)
     *
     * @param key
     * @param offset
     * @return
     */
    public boolean getbit(string key, long offset) {
        return redistemplate.opsforvalue().getbit(key, offset);
    }

    /**
     * 批量获取
     *
     * @param keys
     * @return
     */
    public list<string> multiget(collection<string> keys) {
        return redistemplate.opsforvalue().multiget(keys);
    }

    /**
     * 设置ascii码, 字符串'a'的ascii码是97, 转为二进制是'01100001', 此方法是将二进制第offset位值变为value
     *
     * @param key
     * @param postion 位置
     * @param value   值,true为1, false为0
     * @return
     */
    public boolean setbit(string key, long offset, boolean value) {
        return redistemplate.opsforvalue().setbit(key, offset, value);
    }

    /**
     * 将值 value 关联到 key ,并将 key 的过期时间设为 timeout
     *
     * @param key
     * @param value
     * @param timeout 过期时间
     * @param unit    时间单位, 天:timeunit.days 小时:timeunit.hours 分钟:timeunit.minutes
     *                秒:timeunit.seconds 毫秒:timeunit.milliseconds
     */
    public void setex(string key, string value, long timeout, timeunit unit) {
        redistemplate.opsforvalue().set(key, value, timeout, unit);
    }

    /**
     * 只有在 key 不存在时设置 key 的值
     *
     * @param key
     * @param value
     * @return 之前已经存在返回false, 不存在返回true
     */
    public boolean setifabsent(string key, string value) {
        return redistemplate.opsforvalue().setifabsent(key, value);
    }

    /**
     * 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
     *
     * @param key
     * @param value
     * @param offset 从指定位置开始覆写
     */
    public void setrange(string key, string value, long offset) {
        redistemplate.opsforvalue().set(key, value, offset);
    }

    /**
     * 获取字符串的长度
     *
     * @param key
     * @return
     */
    public long size(string key) {
        return redistemplate.opsforvalue().size(key);
    }

    /**
     * 批量添加
     *
     * @param maps
     */
    public void multiset(map<string, string> maps) {
        redistemplate.opsforvalue().multiset(maps);
    }

    /**
     * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在
     *
     * @param maps
     * @return 之前已经存在返回false, 不存在返回true
     */
    public boolean multisetifabsent(map<string, string> maps) {
        return redistemplate.opsforvalue().multisetifabsent(maps);
    }

    /**
     * 增加(自增长), 负数则为自减
     *
     * @param key
     * @param value
     * @return
     */
    public long incrby(string key, long increment) {
        return redistemplate.opsforvalue().increment(key, increment);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public double incrbyfloat(string key, double increment) {
        return redistemplate.opsforvalue().increment(key, increment);
    }

    /**
     * 追加到末尾
     *
     * @param key
     * @param value
     * @return
     */
    public integer append(string key, string value) {
        return redistemplate.opsforvalue().append(key, value);
    }

    /** -------------------hash相关操作------------------------- */

    /**
     * 获取存储在哈希表中指定字段的值
     *
     * @param key
     * @param field
     * @return
     */
    public object hget(string key, string field) {
        return redistemplate.opsforhash().get(key, field);
    }

    /**
     * 获取所有给定字段的值
     *
     * @param key
     * @return
     */
    public map<object, object> hgetall(string key) {
        return redistemplate.opsforhash().entries(key);
    }

    /**
     * 获取所有给定字段的值
     *
     * @param key
     * @param fields
     * @return
     */
    public list<object> hmultiget(string key, collection<object> fields) {
        return redistemplate.opsforhash().multiget(key, fields);
    }

    public void hput(string key, string hashkey, string value) {
        redistemplate.opsforhash().put(key, hashkey, value);
    }

    public void hputall(string key, map<string, string> maps) {
        redistemplate.opsforhash().putall(key, maps);
    }

    /**
     * 仅当hashkey不存在时才设置
     *
     * @param key
     * @param hashkey
     * @param value
     * @return
     */
    public boolean hputifabsent(string key, string hashkey, string value) {
        return redistemplate.opsforhash().putifabsent(key, hashkey, value);
    }

    /**
     * 删除一个或多个哈希表字段
     *
     * @param key
     * @param fields
     * @return
     */
    public long hdelete(string key, object... fields) {
        return redistemplate.opsforhash().delete(key, fields);
    }

    /**
     * 查看哈希表 key 中,指定的字段是否存在
     *
     * @param key
     * @param field
     * @return
     */
    public boolean hexists(string key, string field) {
        return redistemplate.opsforhash().haskey(key, field);
    }

    /**
     * 为哈希表 key 中的指定字段的整数值加上增量 increment
     *
     * @param key
     * @param field
     * @param increment
     * @return
     */
    public long hincrby(string key, object field, long increment) {
        return redistemplate.opsforhash().increment(key, field, increment);
    }

    /**
     * 为哈希表 key 中的指定字段的整数值加上增量 increment
     *
     * @param key
     * @param field
     * @param delta
     * @return
     */
    public double hincrbyfloat(string key, object field, double delta) {
        return redistemplate.opsforhash().increment(key, field, delta);
    }

    /**
     * 获取所有哈希表中的字段
     *
     * @param key
     * @return
     */
    public set<object> hkeys(string key) {
        return redistemplate.opsforhash().keys(key);
    }

    /**
     * 获取哈希表中字段的数量
     *
     * @param key
     * @return
     */
    public long hsize(string key) {
        return redistemplate.opsforhash().size(key);
    }

    /**
     * 获取哈希表中所有值
     *
     * @param key
     * @return
     */
    public list<object> hvalues(string key) {
        return redistemplate.opsforhash().values(key);
    }

    /**
     * 迭代哈希表中的键值对
     *
     * @param key
     * @param options
     * @return
     */
    public cursor<entry<object, object>> hscan(string key, scanoptions options) {
        return redistemplate.opsforhash().scan(key, options);
    }

    /** ------------------------list相关操作---------------------------- */

    /**
     * 通过索引获取列表中的元素
     *
     * @param key
     * @param index
     * @return
     */
    public string lindex(string key, long index) {
        return redistemplate.opsforlist().index(key, index);
    }

    /**
     * 获取列表指定范围内的元素
     *
     * @param key
     * @param start 开始位置, 0是开始位置
     * @param end   结束位置, -1返回所有
     * @return
     */
    public list<string> lrange(string key, long start, long end) {
        return redistemplate.opsforlist().range(key, start, end);
    }

    /**
     * 存储在list头部
     *
     * @param key
     * @param value
     * @return
     */
    public long lleftpush(string key, string value) {
        return redistemplate.opsforlist().leftpush(key, value);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public long lleftpushall(string key, string... value) {
        return redistemplate.opsforlist().leftpushall(key, value);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public long lleftpushall(string key, collection<string> value) {
        return redistemplate.opsforlist().leftpushall(key, value);
    }

    /**
     * 当list存在的时候才加入
     *
     * @param key
     * @param value
     * @return
     */
    public long lleftpushifpresent(string key, string value) {
        return redistemplate.opsforlist().leftpushifpresent(key, value);
    }

    /**
     * 如果pivot存在,再pivot前面添加
     *
     * @param key
     * @param pivot
     * @param value
     * @return
     */
    public long lleftpush(string key, string pivot, string value) {
        return redistemplate.opsforlist().leftpush(key, pivot, value);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public long lrightpush(string key, string value) {
        return redistemplate.opsforlist().rightpush(key, value);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public long lrightpushall(string key, string... value) {
        return redistemplate.opsforlist().rightpushall(key, value);
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public long lrightpushall(string key, collection<string> value) {
        return redistemplate.opsforlist().rightpushall(key, value);
    }

    /**
     * 为已存在的列表添加值
     *
     * @param key
     * @param value
     * @return
     */
    public long lrightpushifpresent(string key, string value) {
        return redistemplate.opsforlist().rightpushifpresent(key, value);
    }

    /**
     * 在pivot元素的右边添加值
     *
     * @param key
     * @param pivot
     * @param value
     * @return
     */
    public long lrightpush(string key, string pivot, string value) {
        return redistemplate.opsforlist().rightpush(key, pivot, value);
    }

    /**
     * 通过索引设置列表元素的值
     *
     * @param key
     * @param index 位置
     * @param value
     */
    public void lset(string key, long index, string value) {
        redistemplate.opsforlist().set(key, index, value);
    }

    /**
     * 移出并获取列表的第一个元素
     *
     * @param key
     * @return 删除的元素
     */
    public string lleftpop(string key) {
        return redistemplate.opsforlist().leftpop(key);
    }

    /**
     * 移出并获取列表的第一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
     *
     * @param key
     * @param timeout 等待时间
     * @param unit    时间单位
     * @return
     */
    public string lbleftpop(string key, long timeout, timeunit unit) {
        return redistemplate.opsforlist().leftpop(key, timeout, unit);
    }

    /**
     * 移除并获取列表最后一个元素
     *
     * @param key
     * @return 删除的元素
     */
    public string lrightpop(string key) {
        return redistemplate.opsforlist().rightpop(key);
    }

    /**
     * 移出并获取列表的最后一个元素, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
     *
     * @param key
     * @param timeout 等待时间
     * @param unit    时间单位
     * @return
     */
    public string lbrightpop(string key, long timeout, timeunit unit) {
        return redistemplate.opsforlist().rightpop(key, timeout, unit);
    }

    /**
     * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回
     *
     * @param sourcekey
     * @param destinationkey
     * @return
     */
    public string lrightpopandleftpush(string sourcekey, string destinationkey) {
        return redistemplate.opsforlist().rightpopandleftpush(sourcekey,
                destinationkey);
    }

    /**
     * 从列表中弹出一个值,将弹出的元素插入到另外一个列表中并返回它; 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
     *
     * @param sourcekey
     * @param destinationkey
     * @param timeout
     * @param unit
     * @return
     */
    public string lbrightpopandleftpush(string sourcekey, string destinationkey,
                                        long timeout, timeunit unit) {
        return redistemplate.opsforlist().rightpopandleftpush(sourcekey,
                destinationkey, timeout, unit);
    }

    /**
     * 删除集合中值等于value得元素
     *
     * @param key
     * @param index index=0, 删除所有值等于value的元素; index>0, 从头部开始删除第一个值等于value的元素;
     *              index<0, 从尾部开始删除第一个值等于value的元素;
     * @param value
     * @return
     */
    public long lremove(string key, long index, string value) {
        return redistemplate.opsforlist().remove(key, index, value);
    }

    /**
     * 裁剪list
     *
     * @param key
     * @param start
     * @param end
     */
    public void ltrim(string key, long start, long end) {
        redistemplate.opsforlist().trim(key, start, end);
    }

    /**
     * 获取列表长度
     *
     * @param key
     * @return
     */
    public long llen(string key) {
        return redistemplate.opsforlist().size(key);
    }

    /** --------------------set相关操作-------------------------- */

    /**
     * set添加元素
     *
     * @param key
     * @param values
     * @return
     */
    public long sadd(string key, string... values) {
        return redistemplate.opsforset().add(key, values);
    }

    /**
     * set移除元素
     *
     * @param key
     * @param values
     * @return
     */
    public long sremove(string key, object... values) {
        return redistemplate.opsforset().remove(key, values);
    }

    /**
     * 移除并返回集合的一个随机元素
     *
     * @param key
     * @return
     */
    public string spop(string key) {
        return redistemplate.opsforset().pop(key);
    }

    /**
     * 将元素value从一个集合移到另一个集合
     *
     * @param key
     * @param value
     * @param destkey
     * @return
     */
    public boolean smove(string key, string value, string destkey) {
        return redistemplate.opsforset().move(key, value, destkey);
    }

    /**
     * 获取集合的大小
     *
     * @param key
     * @return
     */
    public long ssize(string key) {
        return redistemplate.opsforset().size(key);
    }

    /**
     * 判断集合是否包含value
     *
     * @param key
     * @param value
     * @return
     */
    public boolean sismember(string key, object value) {
        return redistemplate.opsforset().ismember(key, value);
    }

    /**
     * 获取两个集合的交集
     *
     * @param key
     * @param otherkey
     * @return
     */
    public set<string> sintersect(string key, string otherkey) {
        return redistemplate.opsforset().intersect(key, otherkey);
    }

    /**
     * 获取key集合与多个集合的交集
     *
     * @param key
     * @param otherkeys
     * @return
     */
    public set<string> sintersect(string key, collection<string> otherkeys) {
        return redistemplate.opsforset().intersect(key, otherkeys);
    }

    /**
     * key集合与otherkey集合的交集存储到destkey集合中
     *
     * @param key
     * @param otherkey
     * @param destkey
     * @return
     */
    public long sintersectandstore(string key, string otherkey, string destkey) {
        return redistemplate.opsforset().intersectandstore(key, otherkey,
                destkey);
    }

    /**
     * key集合与多个集合的交集存储到destkey集合中
     *
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public long sintersectandstore(string key, collection<string> otherkeys,
                                   string destkey) {
        return redistemplate.opsforset().intersectandstore(key, otherkeys,
                destkey);
    }

    /**
     * 获取两个集合的并集
     *
     * @param key
     * @param otherkeys
     * @return
     */
    public set<string> sunion(string key, string otherkeys) {
        return redistemplate.opsforset().union(key, otherkeys);
    }

    /**
     * 获取key集合与多个集合的并集
     *
     * @param key
     * @param otherkeys
     * @return
     */
    public set<string> sunion(string key, collection<string> otherkeys) {
        return redistemplate.opsforset().union(key, otherkeys);
    }

    /**
     * key集合与otherkey集合的并集存储到destkey中
     *
     * @param key
     * @param otherkey
     * @param destkey
     * @return
     */
    public long sunionandstore(string key, string otherkey, string destkey) {
        return redistemplate.opsforset().unionandstore(key, otherkey, destkey);
    }

    /**
     * key集合与多个集合的并集存储到destkey中
     *
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public long sunionandstore(string key, collection<string> otherkeys,
                               string destkey) {
        return redistemplate.opsforset().unionandstore(key, otherkeys, destkey);
    }

    /**
     * 获取两个集合的差集
     *
     * @param key
     * @param otherkey
     * @return
     */
    public set<string> sdifference(string key, string otherkey) {
        return redistemplate.opsforset().difference(key, otherkey);
    }

    /**
     * 获取key集合与多个集合的差集
     *
     * @param key
     * @param otherkeys
     * @return
     */
    public set<string> sdifference(string key, collection<string> otherkeys) {
        return redistemplate.opsforset().difference(key, otherkeys);
    }

    /**
     * key集合与otherkey集合的差集存储到destkey中
     *
     * @param key
     * @param otherkey
     * @param destkey
     * @return
     */
    public long sdifference(string key, string otherkey, string destkey) {
        return redistemplate.opsforset().differenceandstore(key, otherkey,
                destkey);
    }

    /**
     * key集合与多个集合的差集存储到destkey中
     *
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public long sdifference(string key, collection<string> otherkeys,
                            string destkey) {
        return redistemplate.opsforset().differenceandstore(key, otherkeys,
                destkey);
    }

    /**
     * 获取集合所有元素
     *
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public set<string> setmembers(string key) {
        return redistemplate.opsforset().members(key);
    }

    /**
     * 随机获取集合中的一个元素
     *
     * @param key
     * @return
     */
    public string srandommember(string key) {
        return redistemplate.opsforset().randommember(key);
    }

    /**
     * 随机获取集合中count个元素
     *
     * @param key
     * @param count
     * @return
     */
    public list<string> srandommembers(string key, long count) {
        return redistemplate.opsforset().randommembers(key, count);
    }

    /**
     * 随机获取集合中count个元素并且去除重复的
     *
     * @param key
     * @param count
     * @return
     */
    public set<string> sdistinctrandommembers(string key, long count) {
        return redistemplate.opsforset().distinctrandommembers(key, count);
    }

    /**
     * @param key
     * @param options
     * @return
     */
    public cursor<string> sscan(string key, scanoptions options) {
        return redistemplate.opsforset().scan(key, options);
    }

    /**------------------zset相关操作--------------------------------*/

    /**
     * 添加元素,有序集合是按照元素的score值由小到大排列
     *
     * @param key
     * @param value
     * @param score
     * @return
     */
    public boolean zadd(string key, string value, double score) {
        return redistemplate.opsforzset().add(key, value, score);
    }

    /**
     * @param key
     * @param values
     * @return
     */
    public long zadd(string key, set<typedtuple<string>> values) {
        return redistemplate.opsforzset().add(key, values);
    }

    /**
     * @param key
     * @param values
     * @return
     */
    public long zremove(string key, object... values) {
        return redistemplate.opsforzset().remove(key, values);
    }

    /**
     * 增加元素的score值,并返回增加后的值
     *
     * @param key
     * @param value
     * @param delta
     * @return
     */
    public double zincrementscore(string key, string value, double delta) {
        return redistemplate.opsforzset().incrementscore(key, value, delta);
    }

    /**
     * 返回元素在集合的排名,有序集合是按照元素的score值由小到大排列
     *
     * @param key
     * @param value
     * @return 0表示第一位
     */
    public long zrank(string key, object value) {
        return redistemplate.opsforzset().rank(key, value);
    }

    /**
     * 返回元素在集合的排名,按元素的score值由大到小排列
     *
     * @param key
     * @param value
     * @return
     */
    public long zreverserank(string key, object value) {
        return redistemplate.opsforzset().reverserank(key, value);
    }

    /**
     * 获取集合的元素, 从小到大排序
     *
     * @param key
     * @param start 开始位置
     * @param end   结束位置, -1查询所有
     * @return
     */
    public set<string> zrange(string key, long start, long end) {
        return redistemplate.opsforzset().range(key, start, end);
    }

    /**
     * 获取集合元素, 并且把score值也获取
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public set<typedtuple<string>> zrangewithscores(string key, long start,
                                                    long end) {
        return redistemplate.opsforzset().rangewithscores(key, start, end);
    }

    /**
     * 根据score值查询集合元素
     *
     * @param key
     * @param min 最小值
     * @param max 最大值
     * @return
     */
    public set<string> zrangebyscore(string key, double min, double max) {
        return redistemplate.opsforzset().rangebyscore(key, min, max);
    }

    /**
     * 根据score值查询集合元素, 从小到大排序
     *
     * @param key
     * @param min 最小值
     * @param max 最大值
     * @return
     */
    public set<typedtuple<string>> zrangebyscorewithscores(string key,
                                                           double min, double max) {
        return redistemplate.opsforzset().rangebyscorewithscores(key, min, max);
    }

    /**
     * @param key
     * @param min
     * @param max
     * @param start
     * @param end
     * @return
     */
    public set<typedtuple<string>> zrangebyscorewithscores(string key,
                                                           double min, double max, long start, long end) {
        return redistemplate.opsforzset().rangebyscorewithscores(key, min, max,
                start, end);
    }

    /**
     * 获取集合的元素, 从大到小排序
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public set<string> zreverserange(string key, long start, long end) {
        return redistemplate.opsforzset().reverserange(key, start, end);
    }

    /**
     * 获取集合的元素, 从大到小排序, 并返回score值
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public set<typedtuple<string>> zreverserangewithscores(string key,
                                                           long start, long end) {
        return redistemplate.opsforzset().reverserangewithscores(key, start,
                end);
    }

    /**
     * 根据score值查询集合元素, 从大到小排序
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public set<string> zreverserangebyscore(string key, double min,
                                            double max) {
        return redistemplate.opsforzset().reverserangebyscore(key, min, max);
    }

    /**
     * 根据score值查询集合元素, 从大到小排序
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public set<typedtuple<string>> zreverserangebyscorewithscores(
            string key, double min, double max) {
        return redistemplate.opsforzset().reverserangebyscorewithscores(key,
                min, max);
    }

    /**
     * @param key
     * @param min
     * @param max
     * @param start
     * @param end
     * @return
     */
    public set<string> zreverserangebyscore(string key, double min,
                                            double max, long start, long end) {
        return redistemplate.opsforzset().reverserangebyscore(key, min, max,
                start, end);
    }

    /**
     * 根据score值获取集合元素数量
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public long zcount(string key, double min, double max) {
        return redistemplate.opsforzset().count(key, min, max);
    }

    /**
     * 获取集合大小
     *
     * @param key
     * @return
     */
    public long zsize(string key) {
        return redistemplate.opsforzset().size(key);
    }

    /**
     * 获取集合大小
     *
     * @param key
     * @return
     */
    public long zzcard(string key) {
        return redistemplate.opsforzset().zcard(key);
    }

    /**
     * 获取集合中value元素的score值
     *
     * @param key
     * @param value
     * @return
     */
    public double zscore(string key, object value) {
        return redistemplate.opsforzset().score(key, value);
    }

    /**
     * 移除指定索引位置的成员
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public long zremoverange(string key, long start, long end) {
        return redistemplate.opsforzset().removerange(key, start, end);
    }

    /**
     * 根据指定的score值的范围来移除成员
     *
     * @param key
     * @param min
     * @param max
     * @return
     */
    public long zremoverangebyscore(string key, double min, double max) {
        return redistemplate.opsforzset().removerangebyscore(key, min, max);
    }

    /**
     * 获取key和otherkey的并集并存储在destkey中
     *
     * @param key
     * @param otherkey
     * @param destkey
     * @return
     */
    public long zunionandstore(string key, string otherkey, string destkey) {
        return redistemplate.opsforzset().unionandstore(key, otherkey, destkey);
    }

    /**
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public long zunionandstore(string key, collection<string> otherkeys,
                               string destkey) {
        return redistemplate.opsforzset()
                .unionandstore(key, otherkeys, destkey);
    }

    /**
     * 交集
     *
     * @param key
     * @param otherkey
     * @param destkey
     * @return
     */
    public long zintersectandstore(string key, string otherkey,
                                   string destkey) {
        return redistemplate.opsforzset().intersectandstore(key, otherkey,
                destkey);
    }

    /**
     * 交集
     *
     * @param key
     * @param otherkeys
     * @param destkey
     * @return
     */
    public long zintersectandstore(string key, collection<string> otherkeys,
                                   string destkey) {
        return redistemplate.opsforzset().intersectandstore(key, otherkeys,
                destkey);
    }

    /**
     * @param key
     * @param options
     * @return
     */
    public cursor<typedtuple<string>> zscan(string key, scanoptions options) {
        return redistemplate.opsforzset().scan(key, options);
    }



}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网