写点什么

springboot 整合 redis 五种数据结构 API

作者:刘大猫
  • 2025-11-12
    黑龙江
  • 本文字数:5047 字

    阅读完需:约 17 分钟

三、案例:springboot 整合 redis 五种数据结构 API

  1. string(字符串)类型

  2. hash(哈希)类型

  3. list(列表)类型

  4. set(无序集合)类型

  5. zset(有序集合)类型


pom 依赖


<!--redis --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId>    <version>2.3.4.RELEASE</version></dependency><!--redis锁--><dependency>    <groupId>org.redisson</groupId>    <artifactId>redisson</artifactId>    <version>3.13.6</version></dependency><dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.10.1</version></dependency>
复制代码


环境变量配置,redis 采用 windows 的客户端启动,链接本地


#redisspring.redis.database=15spring.redis.host=localhostspring.redis.port=6379spring.redis.password=spring.redis.pool.max-active=200spring.redis.jedis.pool.max-wait= -1spring.redis.jedis.pool.max-idle=10spring.redis.jedis.pool.min-idle=0spring.redis.timeout = 10000
复制代码


User 实体


package com.example.demo.bean;
import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;
@Data@AllArgsConstructor@NoArgsConstructorpublic class User { //姓名 private String name; //密码 private String password;}
复制代码

1、string(字符串)类型

使用场景:key-value 缓存、计数操作对象:redisTemplate.opsForValue()添加数据:set(Object k, Object v);获取数据:get(Object k);获取数据长度:size(Object k);拼接内容:append(Object k, String s);数值加一:increment(Object k);数值减一:decrement(Object k);


package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;import com.example.demo.DemoApplication;import com.example.demo.bean.User;import org.junit.Test;import org.junit.runner.RunWith;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;import java.util.List;
@SpringBootTest(classes = DemoApplication.class)@RunWith(SpringRunner.class)public class RedisControllerTest { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
//string类型添加 @Test public void stringAdd() { // 添加redis 字符类型数据 strKey1 redisTemplate.opsForValue().set("strKey1","一段话。。。");
// 添加redis 字符类型数据 strKey2 JSONObject json = new JSONObject(); json.put("dog","狗"); json.put("cat","猫"); redisTemplate.opsForValue().set("strKey2",json.toJSONString()); } //string类型查询 @Test public void stringQuery() { // 通过 strKey1 获取并打印值 System.err.println(redisTemplate.opsForValue().get("strKey1")); // 通过 strKey2 获取并打印值 System.err.println(redisTemplate.opsForValue().get("strKey2")); }
复制代码

2、hash(哈希)类型

使用场景:缓存对象(string 类型也可以实现-值存 json 对象字符串)操作对象:redisTemplate.opsForHash()添加数据:put(Object h, Object hk, Object hv);获取 map 对象某值:get(Object h, Object o);获取 map 对象:entries(Object h);


package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;import com.example.demo.DemoApplication;import com.example.demo.bean.User;import org.junit.Test;import org.junit.runner.RunWith;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;import java.util.List;
@SpringBootTest(classes = DemoApplication.class)@RunWith(SpringRunner.class)public class RedisControllerTest { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
//hash类型添加 @Test public void hashAdd() { // 添加数据 redisTemplate.opsForHash().put("hash1","key1","value1"); redisTemplate.opsForHash().put("hash1","key2","value2"); } //hash类型查询 @Test public void hashQuery() { // 通过 h1 获取值 System.err.println(redisTemplate.opsForHash().get("hash1","key1")); System.err.println(redisTemplate.opsForHash().entries("hash1")); }
复制代码

3、list(列表)类型

使用场景:队列、栈(左进右出:队列,左进左出:栈)操作对象:redisTemplate.opsForList()从列表左侧添加数据:leftPush(Object k, Object v);从列表左侧取数据:leftPop(Object k);从列表右侧取数据:rightPop(Object k);


package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;import com.example.demo.DemoApplication;import com.example.demo.bean.User;import org.junit.Test;import org.junit.runner.RunWith;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;import java.util.List;
@SpringBootTest(classes = DemoApplication.class)@RunWith(SpringRunner.class)public class RedisControllerTest { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
//list类型添加 @Test public void listAdd() { List list = new ArrayList<>(); User user1 = new User("老赵", "123"); User user2 = new User("老曹", "456"); list.add(user1); list.add(user2); // 直接添加list redisTemplate.opsForList().leftPush("listKey",list);
//循环添加元素 redisTemplate.opsForList().leftPush("list1","v1"); redisTemplate.opsForList().leftPush("list1","v2"); redisTemplate.opsForList().leftPush("list1","v3"); } //list类型查询 @Test public void listQuery() { System.err.println(redisTemplate.opsForList().leftPop("listKey")); // 通过 list1 从队列左侧取出并删除数据 System.err.println(redisTemplate.opsForList().leftPop("list1")); // 通过 list1 从队列右侧取出并删除数据 System.err.println(redisTemplate.opsForList().rightPop("list1")); }
复制代码

4、set(无序集合)类型

使用场景:无序且不重复的集合,求交、差、并集操作对象:redisTemplate.opsForSet()获取两个集合的交集:intersect(Object k, Object k1);获取两个集合的差集:difference(Object k,Object k1);获取两个集合的并集:union(Object k,Object k1);


package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;import com.example.demo.DemoApplication;import com.example.demo.bean.User;import org.junit.Test;import org.junit.runner.RunWith;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;import java.util.List;
@SpringBootTest(classes = DemoApplication.class)@RunWith(SpringRunner.class)public class RedisControllerTest { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
//set(无序集合)类型添加 @Test public void setAdd() { User user1 = new User("老赵", "123"); User user2 = new User("老曹", "456"); // 添加数据 redisTemplate.opsForSet().add("set1","v1","v2","v3"); redisTemplate.opsForSet().add("set2","v1"); redisTemplate.opsForSet().add("set3",user1, user2); } //set(无序集合)类型查询 @Test public void setQuery() { // 求交集 System.err.println(redisTemplate.opsForSet().intersect("set1","set2")); // 求差集 System.err.println(redisTemplate.opsForSet().difference("set1","set2")); // 求并集 System.err.println(redisTemplate.opsForSet().union("set1","set2")); System.err.println(redisTemplate.opsForSet().members("set3")); }
复制代码

5、zset(有序集合)类型

使用场景:根据权重获取集合操作对象:redisTemplate.opsForZSet()添加数据:add(Object k, Object v, Object v1);根据权重范围获取集合:rangeByScore(Object k,Object v,Object v1);


package com.example.demo.controller;
import com.alibaba.fastjson.JSONObject;import com.example.demo.DemoApplication;import com.example.demo.bean.User;import org.junit.Test;import org.junit.runner.RunWith;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;import java.util.List;
@SpringBootTest(classes = DemoApplication.class)@RunWith(SpringRunner.class)public class RedisControllerTest { @Autowired private RedisTemplate redisTemplate; @Autowired private StringRedisTemplate stringRedisTemplate;
//zset(有序集合)类型添加 @Test public void zsetAdd() { // 添加数据 redisTemplate.opsForZSet().add("zset1","A",1); redisTemplate.opsForZSet().add("zset1","B",3); redisTemplate.opsForZSet().add("zset1","C",2); redisTemplate.opsForZSet().add("zset1","D",5); } //zset(有序集合)类型查询 @Test public void zsetQuery() { System.err.println(redisTemplate.opsForZSet().rangeByScore("zset1",1,4)); }
复制代码

6、删除 key

//删除key@Testpublic void deleteKey() {    //删除key    redisTemplate.delete("strKey1");}
复制代码

四、总结:

当你的 redis 数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,那么你就使用 StringRedisTemplate 即可,但是如果你的数据是复杂的对象类型,而取出的时候又不想做任何的数据转换,直接从 Redis 里面取出一个对象,那么使用 RedisTemplate 是更好的选择。

用户头像

刘大猫

关注

还未添加个人签名 2022-08-23 加入

还未添加个人简介

评论

发布
暂无评论
springboot整合redis五种数据结构API_人工智能_刘大猫_InfoQ写作社区