说明

配置文件参考:https://blog.csdn.net/qq_38428623/article/details/123217001?utm_source=app&app_version=5.1.1&code=app_1562916241&uLinkId=usr1mkqgl919blen

使用
package com.demo.redis.zset;

import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.ScoredEntry;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * RedisScoreSortedSet
 *
 * @author 王思勤
 */
@Component
public class RedisScoreSortedSet {

    @Resource
    private RedissonClient redissonClient;

    /**
     * 获取 字符串 的 RScoredSortedSet
     *
     * @param key 名称
     * @return 返回 值
     */
    public RScoredSortedSet<String> getScoredSortedSet(String key) {
        RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(key, StringCodec.INSTANCE);
        Assert.notNull(scoredSortedSet, "scoredSortedSet is null");
        return scoredSortedSet;
    }

    /**
     * 新增 set 数据
     *
     * @param key   名称
     * @param score 分数
     * @param value 值
     * @return 返回 是否新增成功
     */
    public boolean set(String key, Double score, String value) {
        return this.getScoredSortedSet(key).add(score, value);
    }

    /**
     * 新增 set 数据
     *
     * @param key      名称
     * @param valueMap 值
     * @return 返回 是否新增成功
     */
    public int set(String key, Map<String, Double> valueMap) {
        return this.getScoredSortedSet(key).addAll(valueMap);
    }

    /**
     * 读取 set 的数据
     *
     * @param key        名称
     * @param startIndex 开始索引
     * @param endIndex   结束索引
     * @return 返回 是否新增成功
     */
    public Collection<ScoredEntry<String>> entryRange(String key, int startIndex, int endIndex) {
        return this.getScoredSortedSet(key).entryRange(startIndex, endIndex);
    }

    /**
     * 验证 是否包含
     *
     * @param key   名称
     * @param value 值
     * @return 返回 是否 包含
     */
    public boolean contains(String key, String value) {
        return this.getScoredSortedSet(key).contains(value);
    }

    /**
     * 验证 是否包含
     *
     * @param key    名称
     * @param values 值
     * @return 返回 是否 包含
     */
    public boolean containsAll(String key, List<String> values) {
        return this.getScoredSortedSet(key).containsAll(values);
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐