redis学习四)之Redis与SpringBoot整合
Spring Boot整合Redis非常简单,只需要按如下步骤整合即可:
1.在pom.xml文件中引入redis相关依赖
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 | <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.2.1.RELEASE</version> <relativePath/> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.X集成redis所需common-pool2--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.3.RELEASE</version> </plugin> </plugins> </build> |
2.application.properties配置redis配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # 应用名称<br>spring.application.name=springboot-redis<br># 应用服务 WEB 访问端口<br>server.port=8080<br>#Redis服务器地址 spring.redis.host=192.168.50.128<br># Redis服务器连接密码(默认为空)<br>spring.redis.password=123456 #Redis服务器连接端口 spring.redis.port=6379 #Redis数据库索引(默认为0) spring.redis.database= 0 #连接超时时间(毫秒) spring.redis.timeout=1800000 #连接池最大连接数(使用负值表示没有限制) spring.redis.lettuce.pool.max-active=20 #最大阻塞等待时间(负数表示没限制) spring.redis.lettuce.pool.max-wait=-1 #连接池中的最大空闲连接 spring.redis.lettuce.pool.max-idle=5 #连接池中的最小空闲连接 spring.redis.lettuce.pool.min-idle=0 |
3.添加Redis配置类
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 | import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; @EnableCaching @Configuration public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer();<br><br> //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)<br> //用这个进行序列化可以在redis可视化工具中查看数据,默认的会有乱码 Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object. class ); ObjectMapper om = new ObjectMapper();<br> // 指定要序列化的域,field,get和set,以及修饰符范围,ANY是都有包括private和public om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);<br> // 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会跑出异常 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om);<br> // 配置连接工厂 template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object. class ); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题),过期时间600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } } |
4.测试一下:RedisTestController中添加测试方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @RestController @RequestMapping( "/redisTest" ) public class RedisTestController { @Autowired private RedisTemplate redisTemplate; @GetMapping public String testRedis() { //设置值到redis redisTemplate.opsForValue(). set ( "name" , "lucy" ); //从redis获取值 String name = (String)redisTemplate.opsForValue(). get ( "name" ); return name; } } |
第二种:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @RestController @RequestMapping( "/redisTest" ) public class RedisTestController { @Autowired private RedisTemplate redisTemplate;<br> //访问和传值方式:http://127.0.0.1:8080/redis/addName?key=a&value=123456 //问号后面就是传值 @GetMapping( "addName" ) public String addNmae(@RequestParam String key,@RequestParam String value){ //设置一个值 redisTemplate.opsForValue(). set (key,value); //获取一个值 return redisTemplate.opsForValue(). get (key).toString(); } //访问和传值方式:http://127.0.0.1:8080/redis/addName?key=a&value=123456 //问号后面就是传值 @GetMapping( "deleteName" ) public String deleteNmae(@RequestParam String key){ //删除一个值,true成功,false失败 boolean isSuccess=redisTemplate.delete(key); //这里测试的话可以获取一个值,看是否删除成功 Object obj=redisTemplate.opsForValue(). get (key); return obj== null ? "删除成功" : "删除失败" ; } } |
详细说明(部分操作)
Spring 封装了 RedisTemplate 来操作 Redis,它支持所有的 Redis 原生的 API。在 RedisTemplate 中定义了对5种数据结构的操作方法。
- opsForValue():操作字符串。
- opsForList():操作列表。
- opsForHash():操作哈希。
- opsForSet():操作集合。
- opsForZSet():操作有序集合。
下面通过实例来理解和应用这些方法。这里需要特别注意的是,运行上述方法后要对数据进行清空操作,否则多次运行会导致数据重复操作。
(1)使用Maven添加依赖文件
在pom.xml配置信息文件中,添加Redis依赖:
我的SpringBoot版本:
1 2 3 4 5 6 | <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> |
添加Redis依赖:
1 2 3 4 5 6 | <!-- Redis启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.3.3.RELEASE</version> </dependency> |
(2)Redis的配置
在 application.yml 配置文件中配置Redis信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #Spring配置 spring: #缓存管理器 cache: type: redis #Redis配置 redis: database: 0 #Redis数据库索引(默认为0) host: 127.0.0.1 #Redis服务器地址 port: 6379 #Redis服务器连接端口 password: #Redis服务器连接密码(默认为空) jedis: pool: max-active: 8 #连接池最大连接数(使用负值表示没有限制) max-wait: -1s #连接池最大阻塞等待时间(使用负值表示没有限制) max-idle: 8 #连接池中的最大空闲连接 min-idle: 0 #连接池中的最小空闲连接 lettuce: shutdown-timeout: 100ms #关闭超时时间,默认值100ms |
(3)Redis配置类(config层)
创建com.pjb.config包中,并创建RedisConfig类(Redis配置类),并继承CachingConfigurerSupport类。
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 | package com.pjb.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import java.lang.reflect.Method; /** * Redis配置类 * @author pan_junbiao **/ @Configuration public class RedisConfig extends CachingConfigurerSupport { /** * 缓存对象集合中,缓存是以key-value形式保存的, * 当不指定缓存的key时,SpringBoot会使用keyGenerator生成Key。 */ @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params ) { StringBuilder sb = new StringBuilder(); //类名+方法名 sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params ) { sb.append(obj.toString()); } return sb.toString(); } }; } /** * 缓存管理器 */ @SuppressWarnings( "rawtypes" ) @Bean public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory); //设置缓存过期时间 return cacheManager; } /** * 实例化RedisTemplate对象 */ @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object. class ); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } } |
1、字符串(String)
字符串(String)是 Redis 最基本的数据类型。String 的一个“Key”对应一个“Value”,即 Key-Value 键值对。String 是二进制安全的,可以存储任何数据(比如图片或序列化的对象)。值最大能存储512MB的数据。一般用于一些复杂的计数功能的缓存。RedisTemplate 提供以下操作 String 的方法。
void set(K key, V value)和V get(Object key)
具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Redis操作字符串(String) * @author pan_junbiao **/ @SpringBootTest public class StringTest { @Autowired private RedisTemplate redisTemplate; @Test public void string1() { redisTemplate.opsForValue(). set ( "userName" , "pan_junbiao的博客" ); redisTemplate.opsForValue(). set ( "blogUrl" , "https://blog.csdn.net/pan_junbiao" ); redisTemplate.opsForValue(). set ( "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println( "用户名称:" + redisTemplate.opsForValue(). get ( "userName" )); System. out .println( "博客地址:" + redisTemplate.opsForValue(). get ( "blogUrl" )); System. out .println( "博客信息:" + redisTemplate.opsForValue(). get ( "blogRemark" )); } } |
incr、decr
incr、decr,将 key 中储存的数字值增一或减一
incr为每次增加多少,decr为每次减少多少
1 2 3 4 5 6 7 8 9 10 11 12 | @GetMapping( "incr" ) public String incr(@RequestParam String key) { //每次加1 return redisTemplate.boundValueOps(key).increment(1).toString(); } @GetMapping( "decr" ) public String decr(@RequestParam String key) { //每次减1 return redisTemplate.boundValueOps(key).decrement(1).toString(); } |
void set(K key, V value, long timeout, TimeUnit unit)
以下代码设置3s失效。3s之内查询有结果,3s之后查询返回为null。具体用法见以下代码:
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 | @Autowired private RedisTemplate redisTemplate; @Test public void string2() { //设置的是3s失效,3s之内查询有结果,3s之后返回null redisTemplate.opsForValue(). set ( "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ,3, TimeUnit.SECONDS); try { Object s1 = redisTemplate.opsForValue(). get ( "blogRemark" ); System. out .println( "博客信息:" + s1); Thread.currentThread().sleep(2000); Object s2 = redisTemplate.opsForValue(). get ( "blogRemark" ); System. out .println( "博客信息:" + s2); Thread.currentThread().sleep(5000); Object s3 = redisTemplate.opsForValue(). get ( "blogRemark" ); System. out .println( "博客信息:" + s3); } catch (InterruptedException ie) { ie.printStackTrace(); } } |
Setex、Setnx
Setex、Setnx,设置过期时间
Redis Setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。
SETNX 是 SET if Not eXists的简写,不存在才设置,否则就不做任何操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* *设置过期时间,如果key存在则覆盖旧值 * */ @GetMapping( "setex" ) public void setex(@RequestParam String key,@RequestParam String value){ redisTemplate.opsForValue(). set (key,value,20, TimeUnit.SECONDS); } /* *设置过期时间,SETNX 是 SET if Not eXists的简写,不存在才设置,否则就不做任何操作 * */ @GetMapping( "setnx" ) public void setnx(@RequestParam String key,@RequestParam String value){ redisTemplate.opsForValue().setIfAbsent(key,value,20, TimeUnit.SECONDS); } |
ttl
ttl,获取过期时间
1 2 3 4 5 6 | /* * 获取过期时间 * */ public Long ttl(@RequestParam String key){ return redisTemplate.getExpire( "k" ); } |
V getAndSet(K key, V value)
设置键的字符串,并返回其旧值。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Autowired private RedisTemplate redisTemplate; @Test public void string3() { //设置键的字符串并返回其旧值 redisTemplate.opsForValue(). set ( "blogRemark" , "pan_junbiao的博客" ); Object oldVaule = redisTemplate.opsForValue().getAndSet( "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); Object newVaule = redisTemplate.opsForValue(). get ( "blogRemark" ); System. out .println( "旧值:" + oldVaule); System. out .println( "新值:" + newVaule); } |
Integer append(K key, V value)
如果key已经存在,并且是一个字符串,则该命令将该值追加到字符串的末尾。如果key不存在,则它将被创建并设置为空字符串,因此 append 在这种特殊情况下类似于 set。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Autowired private RedisTemplate redisTemplate; @Test public void string4() { //设置value的序列化规则,否则会报错 redisTemplate.setValueSerializer( new StringRedisSerializer()); redisTemplate.opsForValue().append( "blogRemark" , "您好,欢迎访问 " ); System. out .println(redisTemplate.opsForValue(). get ( "blogRemark" )); redisTemplate.opsForValue().append( "blogRemark" , "pan_junbiao的博客" ); System. out .println(redisTemplate.opsForValue(). get ( "blogRemark" )); } |
注意:这里一定要注意反序列化配置,否则会报错。
Long size(K key)
返回key所对应的value值的长度,见以下代码:
1 2 3 4 5 6 7 8 9 10 | @Autowired private RedisTemplate redisTemplate; @Test public void string5() { redisTemplate.opsForValue(). set ( "userName" , "pan_junbiao的博客" ); System. out .println( "Value值:" + redisTemplate.opsForValue(). get ( "userName" )); System. out .println( "Value值的长度:" + redisTemplate.opsForValue().size( "userName" )); } |
2、列表(List)
Redis列表是简单的字符串列表,按照插入顺序排序。可以添加一个元素到列表的头部(左边)或尾部(右边)。
使用list数据结果,可以做简单的消息队列的功能。还可以利用 Irange 命令,做基于Reids的分页功能,性能极佳。
Long leftPushAll(K key, V... values);Long rightPushAll(K key, V... values)
leftPushAll方法:表示把一个数组插入列表中。
rightPushAll方法:表示向列表的最右边批量添加元素。具体用法见以下代码:
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 | /** * Redis操作列表(List) * @author pan_junbiao **/ @SpringBootTest public class ListTest { @Autowired private RedisTemplate redisTemplate; @Test public void list1() { String[] user1 = new String[]{ "1" , "pan_junbiao的博客" , "您好,欢迎访问 pan_junbiao的博客" }; String[] user2 = new String[]{ "2" , "pan_junbiao的博客" , "https://blog.csdn.net/pan_junbiao" }; String[] user3 = new String[]{ "3" , "pan_junbiao的博客" , "您好,欢迎访问 pan_junbiao的博客" }; redisTemplate.opsForList().rightPushAll( "user1" ,user1); redisTemplate.opsForList().rightPushAll( "user2" ,user2); redisTemplate.opsForList().rightPushAll( "user3" ,user3); System. out .println(redisTemplate.opsForList().range( "user1" ,0,-1)); System. out .println(redisTemplate.opsForList().range( "user2" ,0,-1)); System. out .println(redisTemplate.opsForList().range( "user3" ,0,-1)); } } |
Long leftPush(K key, V value);Long rightPush(K key, V value)
leftPush方法:将所有指定的值插入在键的列表的头部。如果键不存在,则在执行推送操作之前将其创建为空列表(从左边插入)。
rightPush方法:将所有指定的值插入在键的列表的尾部。如果键不存在,则在执行推送操作之前将其创建为空列表(从右边插入)。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Autowired private RedisTemplate redisTemplate; @Test public void list2() { redisTemplate.opsForList().rightPush( "userInfo" ,1); redisTemplate.opsForList().rightPush( "userInfo" , "pan_junbiao的博客" ); redisTemplate.opsForList().rightPush( "userInfo" , "https://blog.csdn.net/pan_junbiao" ); redisTemplate.opsForList().rightPush( "userInfo" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println( "用户编号:" + redisTemplate.opsForList().index( "userInfo" ,0)); System. out .println( "用户名称:" + redisTemplate.opsForList().index( "userInfo" ,1)); System. out .println( "博客地址:" + redisTemplate.opsForList().index( "userInfo" ,2)); System. out .println( "博客信息:" + redisTemplate.opsForList().index( "userInfo" ,3)); } |
Long size(K key)
返回存储在键中的列表的长度。如果键不存在,则将其解释为空列表,并返回0。如果key存在的值不是列表,则返回错误。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 | @Autowired private RedisTemplate redisTemplate; @Test public void list3() { String[] user = new String[]{ "1" , "pan_junbiao的博客" , "您好,欢迎访问 pan_junbiao的博客" }; redisTemplate.opsForList().leftPushAll( "user" ,user); System. out .println( "列表的长度:" + redisTemplate.opsForList().size( "user" )); } |
void set(K key, long index, V value)
在列表中 index 的位置设置 value。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Autowired private RedisTemplate redisTemplate; @Test public void list4() { String[] user = new String[]{ "1" , "pan_junbiao的博客" , "https://blog.csdn.net/pan_junbiao" }; redisTemplate.opsForList().rightPushAll( "user" ,user); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); redisTemplate.opsForList(). set ( "user" ,2, "您好,欢迎访问 pan_junbiao的博客" ); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); } |
V index(K key, long index)
根据下标获取列表中的值(下标从0开始)。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Autowired private RedisTemplate redisTemplate; @Test public void list2() { redisTemplate.opsForList().rightPush( "userInfo" ,1); redisTemplate.opsForList().rightPush( "userInfo" , "pan_junbiao的博客" ); redisTemplate.opsForList().rightPush( "userInfo" , "https://blog.csdn.net/pan_junbiao" ); redisTemplate.opsForList().rightPush( "userInfo" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println( "用户编号:" + redisTemplate.opsForList().index( "userInfo" ,0)); System. out .println( "用户名称:" + redisTemplate.opsForList().index( "userInfo" ,1)); System. out .println( "博客地址:" + redisTemplate.opsForList().index( "userInfo" ,2)); System. out .println( "博客信息:" + redisTemplate.opsForList().index( "userInfo" ,3)); } |
Long remove(K key, long count, Object value)
从存储在键中的列表,删除给定“count”值的元素的第1个计数事件。其中,参数count的含义如下:
- count=0:删除等于value的所有元素。
- count>0:删除等于从头到尾移动的值的元素。
- count<0:删除等于从尾到头移动的值的元素。
以下代码用于删除列表中第一次出现的值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Autowired private RedisTemplate redisTemplate; @Test public void list5() { String[] user = new String[]{ "1" , "pan_junbiao的博客" , "您好,欢迎访问 pan_junbiao的博客" }; redisTemplate.opsForList().rightPushAll( "user" ,user); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); //将删除列表中第一次出现的pan_junbiao的博客 redisTemplate.opsForList().remove( "user" ,1, "pan_junbiao的博客" ); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); } |
V leftPop(K key);V rightPop(K key)
leftPop方法:弹出最左边的元素,弹出之后该值在列表中将不复存在。
rightPop方法:弹出最右边的元素,弹出之后该值在列表中将不复存在。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Autowired private RedisTemplate redisTemplate; @Test public void list6() { String[] user = new String[]{ "1" , "pan_junbiao的博客" , "您好,欢迎访问 pan_junbiao的博客" }; redisTemplate.opsForList().rightPushAll( "user" ,user); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); //弹出最右边的元素,弹出之后该值在列表中将不复存在 System. out .println(redisTemplate.opsForList().rightPop( "user" )); System. out .println(redisTemplate.opsForList().range( "user" ,0,-1)); } |
rpush
1 2 3 4 5 6 7 8 9 | /* * 从右边插的示例 * 从左边插用leftPush * */ @GetMapping( "rpush" ) public void rpush(){ //从右边插 Long rightPush = redisTemplate.opsForList().rightPush( "k" , "v" ); } |
rpop
1 2 3 4 5 6 7 8 | /* * 从右边获取示例 * 从左边用lpop * */ @GetMapping( "rpop" ) public void rpop(){ Object value = redisTemplate.opsForList().rightPop( "k" ); } |
llen
1 2 3 4 5 6 7 | /* * 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 * */ @GetMapping( "llen" ) public void llen(){ Long value = redisTemplate.opsForList().size( "k" ); } |
lrange
1 2 3 4 5 6 7 8 | /* * Lrange 返回列表中指定区间内的元素 * 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 * */ @GetMapping( "lrange" ) public void lrange(){ List list = redisTemplate.opsForList().range( "list" , 0, -1); } |
3、哈希(Hash)
Redis 中的 hash(哈希)是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。value 中存放的是结构化的对象。利用这样数据结果,可以方便地操作其中的某个字段。比如在“单点登录”时,可以用这种数据结构存储用户信息。以 CookieId 作为 key,设置30分钟为缓存过期时间,能很好地模拟出类似 Session 的效果。
void putAll(H key, Map<? extends HK, ? extends HV> m);Map<HK, HV> entries(H key)
putAll方法:用 m 中提供的多个散列字段设置到 key 对应的散列表中。
entries方法:根据密钥获取整个散列存储。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Redis操作哈希(Hash) * @author pan_junbiao **/ @SpringBootTest public class HashTest { @Autowired private RedisTemplate redisTemplate; @Test public void hash1() { Map<String,Object> userMap = new HashMap<>(); userMap.put( "userName" , "pan_junbiao的博客" ); userMap.put( "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); redisTemplate.opsForHash().putAll( "userHash" ,userMap); System. out .println(redisTemplate.opsForHash().entries( "userHash" )); } } |
void put(H key, HK hashKey, HV value);HV get(H key, Object hashKey)
put方法:设置 hashKey 的值。
get方法:从键中的散列获取给定 hashKey 的值。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Autowired private RedisTemplate redisTemplate; @Test public void hash2() { redisTemplate.opsForHash().put( "userHash" , "userName" , "pan_junbiao的博客" ); redisTemplate.opsForHash().put( "userHash" , "blogUrl" , "https://blog.csdn.net/pan_junbiao" ); redisTemplate.opsForHash().put( "userHash" , "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println( "用户名称:" + redisTemplate.opsForHash(). get ( "userHash" , "userName" )); System. out .println( "博客地址:" + redisTemplate.opsForHash(). get ( "userHash" , "blogUrl" )); System. out .println( "博客信息:" + redisTemplate.opsForHash(). get ( "userHash" , "blogRemark" )); } |
List<HV> values(H key);Set<HK> keys(H key)
values方法:根据密钥获取整个散列存储的值。
keys方法:根据密钥获取整个散列存储的键。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 | @Autowired private RedisTemplate redisTemplate; @Test public void hash3() { redisTemplate.opsForHash().put( "userHash" , "userName" , "pan_junbiao的博客" ); redisTemplate.opsForHash().put( "userHash" , "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println( "散列存储的值:" + redisTemplate.opsForHash().values( "userHash" )); System. out .println( "散列存储的键:" + redisTemplate.opsForHash().keys( "userHash" )); } |
Boolean hasKey(H key, Object hashKey);Long size(H key)
hasKey方法:确定 hashKey 是否存在。
size方法:获取 key 所对应的散列表的大小个数。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @Autowired private RedisTemplate redisTemplate; @Test public void hash4() { redisTemplate.opsForHash().put( "userHash" , "userName" , "pan_junbiao的博客" ); redisTemplate.opsForHash().put( "userHash" , "blogUrl" , "https://blog.csdn.net/pan_junbiao" ); redisTemplate.opsForHash().put( "userHash" , "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println(redisTemplate.opsForHash().hasKey( "userHash" , "userName" )); System. out .println(redisTemplate.opsForHash().hasKey( "userHash" , "age" )); System. out .println(redisTemplate.opsForHash().size( "userHash" )); } |
Long delete(H key, Object... hashKeys)
删除给定的 hashKeys。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 | @Autowired private RedisTemplate redisTemplate; @Test public void hash5() { redisTemplate.opsForHash().put( "userHash" , "userName" , "pan_junbiao的博客" ); redisTemplate.opsForHash().put( "userHash" , "blogRemark" , "您好,欢迎访问 pan_junbiao的博客" ); System. out .println(redisTemplate.opsForHash().delete( "userHash" , "blogRemark" )); System. out .println(redisTemplate.opsForHash().entries( "userHash" )); } |
hset
1 2 3 4 5 6 7 8 | hset /* * 命令用于为哈希表中的字段赋值 * */ @GetMapping( "hset" ) public void hset(){ redisTemplate.opsForHash().put( "myhash" , "name" , "张三" ); } |
hget
1 2 3 4 5 6 7 8 9 10 | /* * 获取集合中的值 * */ @GetMapping( "hget" ) public void hget(){ //根据key和字段名获取value Object o = redisTemplate.opsForHash(). get ( "myhash" , "name" ); //获取所有value List list = redisTemplate.opsForHash().values( "myhash" ); } |
hdel
1 2 3 4 5 6 7 | /* * 删除集合中的值 * */ @GetMapping( "hdel" ) public void hdel(){ Long delete = redisTemplate.opsForHash().delete( "myhash" , "name" ); } |
hexists
1 2 3 4 5 6 7 | /* * 判断集合中的值是否存在 * */ @GetMapping( "hexists" ) public void hexists(){ Boolean isexists = redisTemplate.opsForHash().hasKey( "myhash" , "name" ); } |
4、集合(Set)
set 是存放不重复值的集合。利用 set 可以做全局去重复的功能。还可以进行交集、并集、差集等操作,也可用来实现计算共同喜好、全部的喜好、自己独有的喜好等功能。
Redis 的 set 是 string 类型的无序集合,通过散列表实现。
Long add(K key, V... values);Set<V> members(K key)
add方法:在无序集合中添加元素,返回添加个数;如果存在重复的则不进行添加。
members方法:返回集合中的所有成员。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Redis操作集合(Set) * @author pan_junbiao **/ @SpringBootTest public class SetTest { @Autowired private RedisTemplate redisTemplate; @Test public void set1() { String[] citys = new String[]{ "北京" , "上海" , "广州" , "深圳" }; System. out .println(redisTemplate.opsForSet().add( "citySet" ,citys)); System. out .println(redisTemplate.opsForSet().add( "citySet" , "香港" , "澳门" , "台湾" )); //返回集合中的所有元素 System. out .println(redisTemplate.opsForSet().members( "citySet" )); } } |
Long remove(K key, Object... values)
移除集合中一个或多个成员。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 | @Autowired private RedisTemplate redisTemplate; @Test public void set2() { String[] citys = new String[]{ "北京" , "上海" , "广州" , "深圳" }; System. out .println(redisTemplate.opsForSet().add( "citySet" ,citys)); System. out .println(redisTemplate.opsForSet().remove( "citySet" ,citys)); } |
V pop(K key)
移除并返回集合中的一个随机元素。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 | @Autowired private RedisTemplate redisTemplate; @Test public void set3() { String[] citys = new String[]{ "北京" , "上海" , "广州" , "深圳" }; System. out .println(redisTemplate.opsForSet().add( "citySet" ,citys)); System. out .println(redisTemplate.opsForSet().pop( "citySet" )); System. out .println(redisTemplate.opsForSet().members( "citySet" )); } |
Boolean move(K key, V value, K destKey)
将 member 元素移动。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | @Autowired private RedisTemplate redisTemplate; @Test public void set4() { String[] citys = new String[]{ "北京" , "上海" , "广州" , "深圳" }; System. out .println(redisTemplate.opsForSet().add( "citySet" ,citys)); System. out .println(redisTemplate.opsForSet().move( "citySet" , "深圳" , "citySet2" )); System. out .println(redisTemplate.opsForSet().members( "citySet" )); System. out .println(redisTemplate.opsForSet().members( "citySet2" )); } |
Cursor<V> scan(K key, ScanOptions options)
用于遍历 Set。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Autowired private RedisTemplate redisTemplate; @Test public void set5() { String[] citys = new String[]{ "北京" , "上海" , "广州" , "深圳" }; System. out .println(redisTemplate.opsForSet().add( "citySet" ,citys)); Cursor<Object> cursor = redisTemplate.opsForSet().scan( "citySet" , ScanOptions.NONE); while (cursor.hasNext()) { System. out .println(cursor.next()); } } |
交集、并集、差集
- Set<V> intersect(K key1, K key2)方法、Long intersectAndStore(K key1, K key2, K destKey)方法:交集。
- Set<V> union(K key1, K key2)方法、Long unionAndStore(K key1, K key2, K destKey)方法:并集。
- Set<V> difference(K key1, K key2)方法、Long differenceAndStore(K key1, K key2, K destKey)方法:差集。
具体用法见以下代码:
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 | @Autowired private RedisTemplate redisTemplate; @Test public void set6() { String[] city1 = new String[]{ "北京" , "上海" , "广州" , "深圳" , "昆明" }; String[] city2 = new String[]{ "北京" , "深圳" , "昆明" , "成都" }; System. out .println(redisTemplate.opsForSet().add( "citySet1" , city1)); System. out .println(redisTemplate.opsForSet().add( "citySet2" , city2)); //返回集合中的所有元素 System. out .println( "城市集合1:" + redisTemplate.opsForSet().members( "citySet1" )); System. out .println( "城市集合2:" + redisTemplate.opsForSet().members( "citySet2" )); //求交集、并集、差集(方式一) System. out .println( "求交集、并集、差集(方式一):" ); System. out .println( "交集:" + redisTemplate.opsForSet().intersect( "citySet1" , "citySet2" )); System. out .println( "并集:" + redisTemplate.opsForSet().union( "citySet1" , "citySet2" )); System. out .println( "差集:" + redisTemplate.opsForSet().difference( "citySet1" , "citySet2" )); //求交集、并集、差集(方式二) redisTemplate.opsForSet().intersectAndStore( "citySet1" , "citySet2" , "intersectCity" ); redisTemplate.opsForSet().unionAndStore( "citySet1" , "citySet2" , "unionCity" ); redisTemplate.opsForSet().differenceAndStore( "citySet1" , "citySet2" , "differenceCity" ); System. out .println( "求交集、并集、差集(方式二):" ); System. out .println( "交集:" + redisTemplate.opsForSet().members( "intersectCity" )); System. out .println( "并集:" + redisTemplate.opsForSet().members( "unionCity" )); System. out .println( "差集:" + redisTemplate.opsForSet().members( "differenceCity" )); } |
sadd
1 2 3 4 5 6 7 8 9 10 | /* * 向集合添加一个或多个成员 * */ @GetMapping( "sadd" ) public void sadd(){ //可以添加一个 Long add = redisTemplate.opsForSet().add( "k" , "v" ); //可以添加多个 Long add1 = redisTemplate.opsForSet().add( "k" , "v1" , "v2" ); } |
srem
1 2 3 4 5 6 7 8 | /* * 移除集合中的一个或多个成员元素 * */ @GetMapping( "srem" ) public void srem(){ Long remove = redisTemplate.opsForSet().remove( "k" , "v" ); Long remove1 = redisTemplate.opsForSet().remove( "k" , "v1" , "v2" ); } |
spop
1 2 3 4 5 6 7 | /* * 移除并返回集合中的一个随机元素 * */ @GetMapping( "spop" ) public void spop(){ Object value = redisTemplate.opsForSet().pop( "k" ); } |
scard
1 2 3 4 5 6 7 | /* * 获取集合的成员数 * */ @GetMapping( "scard" ) public void scard(){ Long size = redisTemplate.opsForSet().size( "k" ); } |
sinter
1 2 3 4 5 6 7 | /* * 返回给定所有集合的交集 * */ @GetMapping( "sinter" ) public void sinter(){ Set intersect = redisTemplate.opsForSet().intersect( "k" , "k2" ); } |
sunion
1 2 3 4 5 6 7 | /* * 返回所有给定集合的并集 * */ @GetMapping( "sunion" ) public void sunion(){ Set union = redisTemplate.opsForSet().union( "k" , "k2" ); } |
sdiff
1 2 3 4 5 6 7 | /* * 返回第一个集合与其他集合之间的差异。 * */ @GetMapping( "sdiff" ) public void sdiff(){ Set difference = redisTemplate.opsForSet().difference( "k" , "k2" ); } |
5、有序集合(Sorted Set)
zset(Sorted Set 有序集合)也是 string 类型元素的集合,且不允许重复的成员。每个元素都会关联一个 double 类型的分数。可以通过分数将该集合中的成员从小到大进行排序。
zset 的成员是唯一的,但权重参数分数(score)却可以重复。集合中的元素能够按 score 进行排列。它可以用来做排行榜应用、取TOP/N、延时任务、范围查找等。
Long add(K key, Set<ZSetOperations.TypedTuple<V>> tuples)
增加一个有序集合。具体用法见以下代码:
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 | import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; 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; /** * Redis操作有序集合(Sorted Set) * @author pan_junbiao **/ @SpringBootTest public class SortedSetTest { @Autowired private RedisTemplate redisTemplate; @Test public void Zset1() { ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>( "pan_junbiao的博客_01" ,9.6); ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>( "pan_junbiao的博客_02" ,1.5); ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>( "pan_junbiao的博客_03" ,7.4); Set<ZSetOperations.TypedTuple<String>> typles = new HashSet<ZSetOperations.TypedTuple<String>>(); typles.add(objectTypedTuple1); typles.add(objectTypedTuple2); typles.add(objectTypedTuple3); System. out .println(redisTemplate.opsForZSet().add( "typles" ,typles)); System. out .println(redisTemplate.opsForZSet().range( "typles" ,0,-1)); } } |
Boolean add(K key, V value, double score)
新增一个有序集合,存在的话为false,不存在的话为true。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset2() { System. out .println(redisTemplate.opsForZSet().add( "zset2" , "pan_junbiao的博客_01" , 9.6)); System. out .println(redisTemplate.opsForZSet().add( "zset2" , "pan_junbiao的博客_01" , 9.6)); } |
Long remove(K key, Object... values)
从有序集合中移除一个或者多个元素。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset3() { System. out .println(redisTemplate.opsForZSet().add( "zset3" , "pan_junbiao的博客_01" , 1.0)); System. out .println(redisTemplate.opsForZSet().add( "zset3" , "pan_junbiao的博客_02" , 1.0)); System. out .println(redisTemplate.opsForZSet().range( "zset3" , 0, -1)); System. out .println(redisTemplate.opsForZSet().remove( "zset3" , "pan_junbiao的博客_02" )); System. out .println(redisTemplate.opsForZSet().range( "zset3" , 0, -1)); } |
Long rank(K key, Object value)
返回有序集中指定成员的排名,其中有序集成员按分数值递增(从小到大)顺序排列。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset4() { System. out .println(redisTemplate.opsForZSet().add( "zset4" , "pan_junbiao的博客_01" ,9.6)); System. out .println(redisTemplate.opsForZSet().add( "zset4" , "pan_junbiao的博客_02" ,1.5)); System. out .println(redisTemplate.opsForZSet().add( "zset4" , "pan_junbiao的博客_03" ,7.4)); System. out .println(redisTemplate.opsForZSet().range( "zset4" , 0, -1)); System. out .println(redisTemplate.opsForZSet().rank( "zset4" , "pan_junbiao的博客_02" )); } |
注意:结果中的0表示第一(最小)。
Set<V> range(K key, long start, long end);Set<V> rangeByScore(K key, double score1, double score2)
range方法:通过索引区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列。
rangeByScore方法:通过分数区间返回有序集合成指定区间内的成员,其中有序集成员按分数值递增(从小到大)顺序排列。
具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset5() { ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>( "pan_junbiao的博客_01" ,9.6); ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>( "pan_junbiao的博客_02" ,1.5); ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>( "pan_junbiao的博客_03" ,7.4); Set<ZSetOperations.TypedTuple<String>> typles = new HashSet<ZSetOperations.TypedTuple<String>>(); typles.add(objectTypedTuple1); typles.add(objectTypedTuple2); typles.add(objectTypedTuple3); System. out .println(redisTemplate.opsForZSet().add( "zset5" ,typles)); System. out .println(redisTemplate.opsForZSet().range( "zset5" ,0,-1)); System. out .println(redisTemplate.opsForZSet().rangeByScore( "zset5" , 0, 8)); } |
Long count(K key, double score1, double score2);Long size(K key)
count方法:通过分数返回有序集合指定区间内的成员个数。
size方法:获取有序集合的成员数。
具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset6() { ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>( "pan_junbiao的博客_01" ,9.6); ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>( "pan_junbiao的博客_02" ,1.5); ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>( "pan_junbiao的博客_03" ,7.4); Set<ZSetOperations.TypedTuple<String>> typles = new HashSet<ZSetOperations.TypedTuple<String>>(); typles.add(objectTypedTuple1); typles.add(objectTypedTuple2); typles.add(objectTypedTuple3); redisTemplate.opsForZSet().add( "zset6" , typles); System. out .println( "分数在0至8区间内的成员个数:" + redisTemplate.opsForZSet().count( "zset6" , 0, 8)); System. out .println( "有序集合的成员数:" + redisTemplate.opsForZSet().size( "zset6" )); } |
Double score(K key, Object o)
获取指定成员的score值。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 | @Test public void Zset7() { redisTemplate.opsForZSet().add( "zset7" , "pan_junbiao的博客_01" , 9.6); redisTemplate.opsForZSet().add( "zset7" , "pan_junbiao的博客_02" , 1.5); redisTemplate.opsForZSet().add( "zset7" , "pan_junbiao的博客_03" , 7.4); System. out .println( "pan_junbiao的博客_01的分数:" + redisTemplate.opsForZSet().score( "zset7" , "pan_junbiao的博客_01" )); System. out .println( "pan_junbiao的博客_02的分数:" + redisTemplate.opsForZSet().score( "zset7" , "pan_junbiao的博客_02" )); System. out .println( "pan_junbiao的博客_03的分数:" + redisTemplate.opsForZSet().score( "zset7" , "pan_junbiao的博客_03" )); } |
Long removeRange(K key, long start, long end)
移除指定索引位置的成员,有序集合成员按照分数值递增(从小到大)顺序排列。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset8() { ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>( "pan_junbiao的博客_01" ,9.6); ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>( "pan_junbiao的博客_02" ,1.5); ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>( "pan_junbiao的博客_03" ,7.4); Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<ZSetOperations.TypedTuple<String>>(); tuples.add(objectTypedTuple1); tuples.add(objectTypedTuple2); tuples.add(objectTypedTuple3); System. out .println(redisTemplate.opsForZSet().add( "zset8" , tuples)); System. out .println(redisTemplate.opsForZSet().range( "zset8" , 0, -1)); System. out .println(redisTemplate.opsForZSet().removeRange( "zset8" , 1, 5)); System. out .println(redisTemplate.opsForZSet().range( "zset8" , 0, -1)); } |
Cursor<ZSetOperations.TypedTuple<V>> scan(K key, ScanOptions options)
遍历 zset。具体用法见以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Autowired private RedisTemplate redisTemplate; @Test public void Zset9() { ZSetOperations.TypedTuple<String> objectTypedTuple1 = new DefaultTypedTuple<>( "pan_junbiao的博客_01" ,9.6); ZSetOperations.TypedTuple<String> objectTypedTuple2 = new DefaultTypedTuple<>( "pan_junbiao的博客_02" ,1.5); ZSetOperations.TypedTuple<String> objectTypedTuple3 = new DefaultTypedTuple<>( "pan_junbiao的博客_03" ,7.4); Set<ZSetOperations.TypedTuple<String>> tuples = new HashSet<ZSetOperations.TypedTuple<String>>(); tuples.add(objectTypedTuple1); tuples.add(objectTypedTuple2); tuples.add(objectTypedTuple3); System. out .println(redisTemplate.opsForZSet().add( "zset9" , tuples)); Cursor<ZSetOperations.TypedTuple<Object>> cursor = redisTemplate.opsForZSet().scan( "zset9" , ScanOptions.NONE); while (cursor.hasNext()) { ZSetOperations.TypedTuple<Object> item = cursor.next(); System. out .println(item.getValue() + " 的分数值:" + item.getScore()); } } |
zadd
1 2 3 4 5 6 7 | /* * 向有序集合添加一个或多个成员,或者更新已存在成员的分数 * */ @GetMapping( "zadd" ) public void zadd(){ Boolean add = redisTemplate.opsForZSet().add( "k" , "aa" , 12); } |
zrevrange
1 2 3 4 5 6 7 | /* * 返回有序集中指定区间内的成员,通过索引,分数从高到低 * */ @GetMapping( "zrevrange" ) public void zrevrange(){ Set value = redisTemplate.opsForZSet().reverseRange( "k" , 0, -1); } |
zrevrangebyscore
1 2 3 4 5 6 7 | /* * 返回有序集中指定分数区间内的成员,分数从高到低排序 * */ @GetMapping( "zrevrangebyscore" ) public void zrevrangebyscore(){ Set range = redisTemplate.opsForZSet().reverseRangeByScore( "k" , 1, 100); } |
zincrby
1 2 3 4 5 6 7 | /* * 有序集合中对指定成员的分数加上增量 increment * */ @GetMapping( "zincrby" ) public void zincrby(){ Double aDouble = redisTemplate.opsForZSet().incrementScore( "k" , "aa" , 19); } |
zcard
1 2 3 4 5 6 7 | /* * 获取有序集合的成员数 * */ @GetMapping( "zcard" ) public void zcard(){ Long value = redisTemplate.opsForZSet().zCard( "k" ); } |
zrank
1 2 3 4 5 6 7 | /* * 返回有序集合中指定成员的索引 * */ @GetMapping( "zrank" ) public void zrank(){ Long value = redisTemplate.opsForZSet().rank( "k" , "aa" ); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!