springboot整合redis五种数据结构API

简介: springboot整合redis五种数据结构API

image.png

三、案例: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的客户端启动,链接本地

#redis
spring.redis.database=15
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=200
spring.redis.jedis.pool.max-wait= -1
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout = 10000

User实体

package com.example.demo.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public 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
@Test
public void deleteKey() {
   
    //删除key
    redisTemplate.delete("strKey1");
}

四、总结:

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

image.png

重要信息

image.png
image.png

目录
相关文章
|
1月前
|
机器学习/深度学习 运维 监控
当系统开始“自愈”:聊聊大数据与AIOps的真正魔力
当系统开始“自愈”:聊聊大数据与AIOps的真正魔力
160 10
|
1月前
|
SQL 数据采集 人工智能
评估工程正成为下一轮 Agent 演进的重点
面向 RL 和在数据层(SQL 或 SPL 环境)中直接调用大模型的自动化评估实践。
975 220
|
1月前
|
监控 安全 Linux
Linux如何部署服务并设置为开机自启
系统ctl命令用于管理Linux服务,包括启动、停止、重启和重载配置等操作。journalctl命令可查看特定服务日志。编写服务文件时需定义[Unit]、[Service]和[Install]部分,通过systemctl管理新服务并设置开机自启。
217 14
|
1月前
|
设计模式 缓存 安全
无锁编程与原子操作:构建极致性能的高并发队列
本文深入探讨无锁编程与原子操作在高并发队列中的应用,通过CAS、环形缓冲、版本化引用等技术,实现高性能、低延迟的线程安全队列,显著提升系统吞吐量,适用于日志、网络通信等高并发场景。
122 10
|
1月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 深度实战:Nacos + Sentinel + Gateway 整合指南
本指南深入整合Spring Cloud Alibaba核心组件:Nacos实现服务注册与配置管理,Sentinel提供流量控制与熔断降级,Gateway构建统一API网关。涵盖环境搭建、动态配置、服务调用与监控,助你打造高可用微服务架构。(238字)
623 10
|
21天前
|
JSON 数据可视化 Java
Spring Boot中使用Swagger3.0.0版本构建RESTful APIs
Spring Boot中使用Swagger3.0.0版本构建RESTful APIs
207 6
|
1月前
|
人工智能 自动驾驶 Java
AI时代,拒当“代码缝合师”:用“组合思维”重掌“原材料”的控制权
本文探讨AI时代为何仍需深究技术原理,揭示“拿来主义”背后是大脑“直觉”的省电本能。提出“组合思维”:通过主动思考拆解技术组合逻辑,将“元剧本”喂养直觉,实现从“代码缝合师”到“技术谱曲家”的思维跃迁,真正掌控技术本质。
107 6
|
1月前
|
人工智能 供应链 物联网
制造业RPA案例:覆盖生产、供应链、财务等场景,附真实落地数据
在制造业数字化转型中,RPA以“数字员工”助力降本增效。本文结合政策趋势、权威数据与实在智能10余个真实案例,深入解析RPA在生产、供应链、财务等环节的应用成效,揭示AI+RPA融合升级路径,为制造企业落地自动化提供可复制的实践参考。