Springboot整合Redis

1 篇文章 0 订阅

介绍

springboot整合redis需要的是spring的另外一个项目 spring data ;
他使用的是RedisTemplate这个类来进行操作

导入依赖

<dependencies>
  	<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

配置文件

spring:
  redis:
    # redis服务端主机
    host: localhost
    # 端口
    port: 6379

RedisConfig

package com.example.springdataredisdemo.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Author Aeolian
 * @Date: 2021/4/9  14:26
 * @EMAIL: 
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        // 使用的fastjson的序列化,需要导入fastjson的包
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        // key 采用 String 的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash 的 key 也采用 String 的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value 序列化方式采用 fastjson
        template.setValueSerializer(genericFastJsonRedisSerializer);
        // hash 的 value 序列化方式采用 fastjson
        template.setHashValueSerializer(genericFastJsonRedisSerializer);
        template.afterPropertiesSet();

        return template;
    }
// 两种连接方式选一个 推荐使用LettuceConnectionFactory
    @Bean
    public RedisConnectionFactory redisConnectionFactory(){
        return new JedisConnectionFactory(new RedisStandaloneConfiguration("localhost",6379));
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory1(){
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("localhost",6379));
    }

}

测试

String

package com.example.springdataredisdemo;

import com.example.springdataredisdemo.pojo.User;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class StringDemo {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    ValueOperations<String, Object> valueOperations;




    /**
     * set/get操作
     */
    @Test
    void textSetAndGet() {
        valueOperations = redisTemplate.opsForValue();
        // set 值
        valueOperations.set("k1","v1");
        valueOperations.set("k2","v2");
        valueOperations.set("k3","v3");
        // get
        System.out.println(valueOperations.get("k1"));
        System.out.println(valueOperations.get("k2"));
        System.out.println(valueOperations.get("k3"));
        // 长度
        System.out.println(valueOperations.size("k1"));
    }

    /**
     * 自增和自减
     */
    @Test
    void testStringIncrAndDecr(){
        valueOperations = redisTemplate.opsForValue();
        // 自增长和自减
        valueOperations.set("k1",1);
        valueOperations.set("k2",2);
        // 默认增长1
        valueOperations.increment("k1");
        System.out.println(valueOperations.get("k1"));
        // 指定
        valueOperations.increment("k1",2);
        System.out.println(valueOperations.get("k1"));
        // 负数就是减与decrement同样的效果 反向思维即可
        valueOperations.increment("k1",-1);
        valueOperations.decrement("k2",2);
        System.out.println(valueOperations.get("k1"));
        System.out.println(valueOperations.get("k2"));
    }

    /**
     * 主要json的序列化问题,如果使用json的序列化作为String类型value的序列化,那么对于逗号的识别会有问题,
     * 只有转换为String的序列化才能解决问题.
     * append操作
     */
    @Test
    void testAppend(){
        valueOperations = redisTemplate.opsForValue();
        valueOperations.set("k1","hello,");
        valueOperations.append("k1","world");
        System.out.println(valueOperations.get("k1"));
    }

    /**
     * 字符串替换及范围获取
     */
    @Test
    void testRange(){
        valueOperations = redisTemplate.opsForValue();
        valueOperations.set("k1","helloworld");
        String s = "abc";
        // 范围获取  hell
        System.out.println(valueOperations.get("k1", 0, 3));
        // 替换 用 value 参数覆写给定 key 所储存的字符串值,从偏移量 offset 开始
        valueOperations.set("k1",s,3);
        System.out.println(valueOperations.get("k1"));
    }

    /**
     * 设置过期时间及只有在 key 不存在时设置 key 的值
     */
    @Test
    void testSetexAndSetnx() throws InterruptedException {
        valueOperations = redisTemplate.opsForValue();
        // 设置这个key 30秒后过期
        valueOperations.set("k1","v1",30,TimeUnit.SECONDS);
        Thread.sleep(3000);
        // 还有多少秒过期
        System.out.println(redisTemplate.getExpire("k1",TimeUnit.SECONDS));
        // 只有在 key 不存在时设置 key 的值
        if (valueOperations.setIfAbsent("k1","hello")) {
            System.out.println("不存在创建成功");
        }else {
            System.out.println("存在创建失败");
        }
        System.out.println("k1的值:"+valueOperations.get("k1"));
        if (valueOperations.setIfAbsent("k5","v2")){
            System.out.println("不存在创建成功");
        }else {
            System.out.println("存在创建失败");
        }
        System.out.println("k5的值:"+valueOperations.get("k5"));
    }

    /**
     * 批量添加 key-value
     * 批量获取 value
     */
    @Test
    void testMset(){
        valueOperations = redisTemplate.opsForValue();
        Map<String,Object> map = new HashMap<>();
        map.put("key1","v1");
        map.put("key2","v2");
        map.put("key3","v3");
        valueOperations.multiSet(map);
        List<String> list = new ArrayList<>();
        list.add("key1");
        list.add("key2");
        list.add("key3");
        for (Object o : valueOperations.multiGet(list)) {
            System.out.println(o);
        }
    }

    /**
     * 同时设置一个或多个 key-value 对,当且仅当所有给定 key 都不存在才会设置成功
     */
    @Test
    void testMultiSetIfAbsent(){
        valueOperations = redisTemplate.opsForValue();
        Map<String,Object> map = new HashMap<>();
        map.put("key4","v1");
        map.put("key5","v2");
        map.put("key6","v3");
        valueOperations.multiSet(map);
        List<String> list = new ArrayList<>();
        list.add("key4");
        list.add("key5");
        list.add("key6");
        for (Object o : valueOperations.multiGet(list)) {
            System.out.println(o);
        }
    }

    @Test
    void testPojo(){
        valueOperations = redisTemplate.opsForValue();

        User user = new User();
        user.setUsername("zhangsan");
        user.setPassword("123456");
        valueOperations.set("k1",user);
        System.out.println(valueOperations.get("k1"));
    }

}

Hash

package com.example.springdataredisdemo;

import com.example.springdataredisdemo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author Aeolian
 * @Date: 2021/4/11  22:30
 * @EMAIL: 
 */
@SpringBootTest
public class MapDemo {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    HashOperations<String, String, Object> hashOperations;

    /**
     * put/get
     */
    @Test
    void testGetAndSet(){
        hashOperations = redisTemplate.opsForHash();
        hashOperations.put("k1","field1","v1");
        hashOperations.put("k1","field2","v2");
        hashOperations.put("k1","field3","v3");
        Map<String, Object> k1 = hashOperations.entries("k1");
        for (Map.Entry<String, Object> objectObjectEntry : k1.entrySet()) {
            System.out.println(objectObjectEntry);
        }
        System.out.println("===========================");
        System.out.println("field1="+hashOperations.get("k1", "field1"));
        System.out.println("field2="+hashOperations.get("k1", "field2"));
        System.out.println("field3="+hashOperations.get("k1", "field3"));
        System.out.println("===========================");
        System.out.println("k1"+hashOperations.values("k1"));
        System.out.println("============================");
        Cursor<Map.Entry<String, Object>> scan = hashOperations.scan("k1", ScanOptions.scanOptions().build());
        while (scan.hasNext()){
            Map.Entry<String, Object> next = scan.next();
            System.out.println(next.getKey()+":"+next.getValue());
        }
        System.out.println("============================");
        Map<String,Object> map = new HashMap<>();
        map.put("filed1","v1");
        map.put("filed2","v2");
        map.put("filed3","v3");
        hashOperations.putAll("k2",map);
        Map<String, Object> k2 = hashOperations.entries("k2");
        for (Map.Entry<String, Object> objectObjectEntry : k1.entrySet()) {
            System.out.println("k2:"+objectObjectEntry);
        }
    }

    /**
     * 仅当 hashKey 不存在时才设置
     */
    @Test
    void testPutIfAbsent(){
        hashOperations = redisTemplate.opsForHash();
        if (hashOperations.putIfAbsent("k1","field1","v1")) {
            System.out.println("不存在设置成功");
        }else {
            System.out.println("存在设置失败");
        }
        Map<String, Object> k1 = hashOperations.entries("k1");
        for (Map.Entry<String, Object> objectObjectEntry : k1.entrySet()) {
            System.out.println(objectObjectEntry);
        }
        if (hashOperations.putIfAbsent("k3","field1","v1")) {
            System.out.println("不存在设置成功");
        }else {
            System.out.println("存在设置失败");
        }
        Map<String, Object> k3 = hashOperations.entries("k3");
        for (Map.Entry<String, Object> objectObjectEntry : k3.entrySet()) {
            System.out.println(objectObjectEntry);
        }
    }

    /**
     * 删除
     */
    @Test
    void testDel(){
        hashOperations = redisTemplate.opsForHash();
        // 判断是否有这个字段
        if (hashOperations.hasKey("k1","field1")){
            hashOperations.delete("k1","field1");
        }
        Map<String, Object> k1 = hashOperations.entries("k1");
        for (Map.Entry<String, Object> objectObjectEntry : k1.entrySet()) {
            System.out.println(objectObjectEntry);
        }
    }

    /**
     * 获取hash字段的数量和hash表的字段
     */
    @Test
    void testKeysAndValues(){
        hashOperations = redisTemplate.opsForHash();
        System.out.println("k1的字段数:"+hashOperations.size("k1"));
        // 获取到k1的所有字段
        Set<String> k1 = hashOperations.keys("k1");
        for (String s : k1) {
            System.out.println("k1:"+s+"="+hashOperations.get("k1", s));
        }

    }

    /**
     * 增量
     */
    @Test
    void testIncrAndDecr(){
        hashOperations = redisTemplate.opsForHash();
        hashOperations.put("k4","field1",1);
        // 增量
        hashOperations.increment("k4","field1",1);
        System.out.println("====输出增量后的值===");
        System.out.println(hashOperations.get("k4", "field1"));
        // 减
        hashOperations.increment("k4","field1",-2);
        System.out.println("====输出减量后的值===");
        System.out.println(hashOperations.get("k4", "field1"));
    }
}

List

package com.example.springdataredisdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author Aeolian
 * @Date: 2021/4/12  9:35
 * @EMAIL: 
 */
@SpringBootTest
public class ListDemo {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    ListOperations<String, Object> listOperations;

    /**
     * left 头插法
     */
    @Test
    void testLeft(){
        listOperations = redisTemplate.opsForList();
        // 头插 hello3 hello2 hello1
        listOperations.leftPush("k1","hello1");
        listOperations.leftPush("k1","hello2");
        listOperations.leftPush("k1","hello3");
        // 通过下标获取值
        System.out.println(listOperations.index("k1", 0));
        System.out.println(listOperations.index("k1", 1));
        System.out.println(listOperations.index("k1", 2));
        // 通过指定下标设置值,前提这个key已经存在且长度没有超出
        listOperations.set("k1",0,"v1");
        listOperations.set("k1",1,"v2");
        listOperations.set("k1",2,"v3");
        // 通过范围获取
        System.out.println("===通过范围获取===");
        System.out.println("k1:"+listOperations.range("k1",0,-1));
        // 多个值存储
        listOperations.leftPushAll("k2","v1","v2","v3","v4");
        List<String> list = new ArrayList<>();
        list.add("v1");
        list.add("v2");
        list.add("v3");
        list.add("v4");
        listOperations.leftPushAll("k3",list);
        System.out.println("k2:"+listOperations.range("k2",0,-1));
        System.out.println("k3:"+listOperations.range("k3",0,-1));
        // 如果k1的v1存在,就在v1前添加
        listOperations.leftPush("k1","v1","v0");
        System.out.println("===========");
        System.out.println("k1:"+listOperations.range("k1",0,-1));
        // pop 弹出顶层的元素
        System.out.println("=====POP======");
        System.out.println("k1的数量:"+listOperations.size("k1"));
        System.out.println(listOperations.leftPop("k1"));
        System.out.println(listOperations.leftPop("k1"));
        System.out.println(listOperations.leftPop("k1"));
        System.out.println("pop后k1的数量:"+listOperations.size("k1"));
    }

    /**
     * right 尾插法 头插和尾插很多都是通用的,只是位置问题。
     */
    @Test
    void testRight(){
        listOperations = redisTemplate.opsForList();
        // 尾插 v1 v2 v3
        listOperations.rightPush("k1","v1");
        listOperations.rightPush("k1","v2");
        listOperations.rightPush("k1","v3");
        System.out.println(listOperations.range("k1",0,-1));
        // pop
        listOperations.rightPop("k1");
        listOperations.leftPop("k1");
        System.out.println(listOperations.range("k1",0,-1));
        // 尾部弹出,头部插入
        listOperations.rightPush("k1","v1");
        listOperations.leftPush("k1","v3");
        listOperations.rightPush("k2","hello1");
        // 将k1的尾部弹出插入到k2的头部
        listOperations.rightPopAndLeftPush("k1","k2");
        System.out.println("===================");
        System.out.println(listOperations.range("k1",0,-1));
        System.out.println(listOperations.range("k2",0,-1));
    }

    /**
     * list内部操作
     */
    @Test
    void testList(){

        listOperations = redisTemplate.opsForList();
        // 尾插 v1 v2 v3
        listOperations.rightPush("k1","v1");
        listOperations.rightPush("k1","v1");
        listOperations.rightPush("k1","v2");
        listOperations.rightPush("k1","v2");
        listOperations.rightPush("k1","v3");
        listOperations.rightPush("k1","v3");
        // 长度
        System.out.println("k1的长度为:"+listOperations.size("k1"));
        // 删除指定的value元素
        // index = 0, 删除所有值等于value的元素;
        // index > 0, 从头部开始删除第一个值等于 value 的元素;
        // index < 0, 从尾部开始删除第一个值等于 value 的元素;
        listOperations.remove("k1",0,"v1");
        System.out.println("====删除后=====");
        System.out.println("k1:"+listOperations.range("k1",0,-1));
        // 裁剪
        listOperations.trim("k1",0,2);
        System.out.println("====裁剪后=====");
        System.out.println("k1:"+listOperations.range("k1",0,-1));
    }
}

Set

package com.example.springdataredisdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @Author Aeolian
 * @Date: 2021/4/12  11:34
 * @EMAIL: 
 */
@SpringBootTest
public class SetDemo {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    SetOperations<String, Object> setOperations;

    @Test
    void test(){
        setOperations = redisTemplate.opsForSet();
        // add 可以添加多个
        setOperations.add("k1","v1","v2","v3");
        setOperations.add("k2","v1","v2","v3");
//        setOperations.add("k3","hello1","hello2","hello3");
        // 移除 可以移除多个
        System.out.println("========移除k1的多个元素===========");
        setOperations.remove("k1","v1","v2");
        System.out.println(setOperations.members("k1"));
        // 移除并返回集合的一个随机元素
        Object k2 = setOperations.pop("k2");
        System.out.println("=========移除返回元素==========");
        System.out.println("随机移除k2的元素:"+k2);
        // 将元素 value 从一个集合移到另一个集合
        System.out.println("========元素 value 从一个集合移到另一个集合===========");
        setOperations.add("k1","v1","v2");
        setOperations.move("k1",k2,"k2");
        System.out.println("k1:"+setOperations.members("k1"));
        System.out.println("k2:"+setOperations.members("k2"));
        // 集合大小 这里k1已经将一个元素移至k2,所以长度为2
        System.out.println("========k1的长度===========");
        System.out.println(setOperations.size("k1"));
        // 判断是否含有这个value 包含返回 true, 不包含返回 false
        System.out.println("========k1是否包含===========");
        if (setOperations.isMember("k1",k2)){
            System.out.println("k1不存在:"+k2);
        }else {
            System.out.println("k1存在:"+k2);
        }
        // 获取两个集合的交集
        System.out.println("=========两个集合的元素交集==========");
        Set<Object> intersect = setOperations.intersect("k1", "k2");
        System.out.println("k1和k2的交集元素:"+intersect);
        // 还可以指定多个集合
        System.out.println("=========多个集合的元素交集==========");
        List<String> list = new ArrayList<>();
        list.add("k1");
        list.add("k2");
        Set<Object> intersect1 = setOperations.intersect(list);
        System.out.println("k1和k2的交集元素:"+intersect1);
        // key 集合与 otherKey 集合的交集存储到 destKey 中、 返回新集合的长度
        setOperations.intersectAndStore("k1", "k2", "k5");
        System.out.println("=========多个集合的元素交集存入新的集合==========");
        System.out.println("k5:"+setOperations.members("k5"));

        // 集合与集合间的并集
        Set<Object> union = setOperations.union("k1", "k2");
        System.out.println("=========多个集合的元素并集==========");
        System.out.println("多个集合元素的并集:"+union);
        // key 集合与 otherKey 集合的并集存储到 destKey 中、 返回新集合的长度
        setOperations.unionAndStore("k1", "k2", "k3");
        System.out.println("=========多个集合的元素并集存入新的集合==========");
        System.out.println("k3:"+setOperations.members("k3"));

        // 获取两个集合的差集
        setOperations.add("k1","hello");
        setOperations.add("k2","helloworld");
        System.out.println("=========两个集合的元素差集==========");
        Set<Object> intersect2 = setOperations.difference("k1", "k2");
        System.out.println("k1和k2的差集元素:"+intersect2);
        // 还可以指定多个集合
        System.out.println("=========多个集合的元素差集==========");
        List<String> list2 = new ArrayList<>();
        list2.add("k1");
        list2.add("k2");
        Set<Object> intersect3 = setOperations.difference(list2);
        System.out.println("k1和k2的差集元素:"+intersect3);
        // key 集合与 otherKey 集合的差集存储到 destKey 中、 返回新集合的长度
        setOperations.differenceAndStore("k1","k2","k4");
        System.out.println("=========多个集合的元素差集存入新的集合==========");
        System.out.println("k4:"+setOperations.members("k4"));
        // 随机获取集合中的一个元素
        System.out.println("随机获取k1的元素:"+setOperations.randomMember("k1"));
        // 随机获取集合中的多个个元素
        System.out.println("随机获取k1的多个元素:"+setOperations.randomMembers("k1",2));
    }
}

ZSet

package com.example.springdataredisdemo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @Author Aeolian
 * @Date: 2021/4/12  14:40
 * @EMAIL: 
 */
@SpringBootTest
public class ZSetDemo {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Test
    void test(){
        ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();;
        // 添加元素
        zSetOperations.add("k1","one",1);
        // 集合添加元素
        ZSetOperations.TypedTuple<Object> typedTuple = new DefaultTypedTuple<Object>("two",2D);
        ZSetOperations.TypedTuple<Object> typedTuple2 = new DefaultTypedTuple<Object>("three",3D);
        Set<ZSetOperations.TypedTuple<Object>> set = new HashSet<>();
        set.add(typedTuple);
        set.add(typedTuple2);
        zSetOperations.add("k1",set);
        // 按顺序输出元素
        System.out.println("=========按顺序输出元素============");
        System.out.println(zSetOperations.range("k1",0,-1));
        // 移除有序集合中的值
        System.out.println("=========移除有序集合中的值============");
        zSetOperations.remove("k1","tow","three");
        System.out.println(zSetOperations.range("k1",0,-1));
        // 移除指定索引位置的成员
        System.out.println("=========移除指定索引位置的成员============");
        zSetOperations.add("k1",set);
        zSetOperations.removeRange("k1",1,2);
        System.out.println(zSetOperations.range("k1",0,-1));
        // 根据指定的 score 值的范围来移除成员
        System.out.println("=========根据指定的 score 值的范围来移除成员============");
        zSetOperations.add("k1",set);
        zSetOperations.removeRangeByScore("k1",1,3);
        System.out.println(zSetOperations.range("k1",0,-1));
        // 增加元素的 score 值,并返回增加后的值
        System.out.println("=========增加元素的 score 值,并返回增加后的值============");
        zSetOperations.add("k1",set);
        zSetOperations.incrementScore("k1","one",4);
        System.out.println(zSetOperations.range("k1",0,-1));
        // 返回元素在集合的排名,有序集合是按照元素的 score 值由小到大排列
        System.out.println("=========返回元素在集合的排名,有序集合是按照元素的 score 值由小到大排列============");
        System.out.println("one的排行:"+zSetOperations.rank("k1", "one"));
        // 返回元素在集合的排名,按元素的 score 值由大到小排列
        System.out.println("=========返回元素在集合的排名,按元素的 score 值由大到小排列============");
        System.out.println("one的排行:"+zSetOperations.reverseRank("k1", "one"));
        // 获取集合元素, 并且把 score 值也获取
        System.out.println("=========获取集合元素, 并且把 score 值也获取============");
        Set<ZSetOperations.TypedTuple<Object>> set1 = zSetOperations.rangeWithScores("k1", 0, 2);
        for (ZSetOperations.TypedTuple<Object> objectTypedTuple : set1) {
            System.out.println(objectTypedTuple.getScore()+":"+objectTypedTuple.getValue());
        }
        // 根据 score 值查询集合元素  这里应该只会输出一个two 因为只有two在这个区间
        System.out.println("k1:"+zSetOperations.rangeByScore("k1", 0, 2));
        // 根据 score 值查询集合元素及其分数, 并按分数从小到大排序
        System.out.println("=========根据 score 值查询集合元素及其分数, 并按分数从小到大排序============");
        Set<ZSetOperations.TypedTuple<Object>> set2 = zSetOperations.rangeWithScores("k1", 0, 6);
        for (ZSetOperations.TypedTuple<Object> objectTypedTuple : set2) {
            System.out.println(objectTypedTuple.getScore()+":"+objectTypedTuple.getValue());
        }
        // 获取集合的元素, 从大到小排序
        System.out.println("k1:"+zSetOperations.reverseRange("k1",0,-1));
        // 说明:反序输出也可以使用类似上面的方法,只不过顺序是反的

        // 根据 score 值获取集合元素数量
        System.out.println("=========根据 score 值获取集合元素数量============");
        System.out.println("k1:"+zSetOperations.count("k1",0,3));
        // 获取集合大小 size的底层实现还是ZCard
        System.out.println("=========获取集合大小============");
        System.out.println("k1的大小:"+zSetOperations.size("k1"));
        System.out.println("k1的大小:"+zSetOperations.zCard("k1"));
        // 获取集合中 value 元素的 score 值
        System.out.println("=========获取集合中 value 元素的 score 值============");
        System.out.println("k1的one元素的score值:"+zSetOperations.score("k1","one"));

        // ZSet也有并集交集差集,调用方法和set是一致的
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值