【Redis】7. SpringBoot 整合 lettuce

本文介绍了如何在SpringBoot项目中整合Lettuce客户端来使用Redis作为缓存,包括新建Maven项目、引入相关依赖、配置redis缓存类及application.yml文件,以及演示了对String类型和Hash的操作。
摘要由CSDN通过智能技术生成

新建 Maven 项目

在这里插入图片描述

导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.fjx</groupId>
    <artifactId>boot-lettuce</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

     <dependencies>

         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>

         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-devtools</artifactId>
             <scope>runtime</scope>
             <optional>true</optional>
         </dependency>

         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
             <exclusions>
                 <exclusion>
                     <groupId>org.junit.vintage</groupId>
                     <artifactId>junit-vintage-engine</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>


         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>

         <!-- 默认是 lettuce 客户端 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
       <!--redis 依赖 Commons-pools 必须添加此依赖  -->
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-pool2</artifactId>
         </dependency>
     </dependencies>


    
Redis Hash 结构是一种数据结构,它允许你将键值对存储为哈希表的形式,其中每个键都是唯一的,并且可以关联任意类型的数据(包括字符串、列表、集合或有序集合)。在 Spring Boot 中整合 Redis,你可以使用 Spring Data Redis 库,它简化了与 Redis 的交互。 Spring Boot 整合 Redis 的步骤如下: 1. 添加依赖:在你的 `pom.xml` 或 `build.gradle` 文件中添加 Spring Data Redis 和相关 Redis客户端库的依赖,如 lettuce 或 Jedis。 ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Gradle (Lettuce) --> implementation 'org.springframework.boot:spring-boot-starter-data-redis:2.5.x' implementation 'io.lettuce:lettuce-core:6.0.x' ``` 2. 配置 Redis:在 `application.properties` 或 `application.yml` 中配置 Redis 的连接信息,如主机名、端口和密码(如果有)。 ```yaml spring.redis.host=your-redis-host spring.redis.port=your-redis-port spring.redis.password=your-password ``` 3. 使用 `HashOperations`:Spring Data Redis 提供了 `HashOperations` 接口,你可以通过注入 `RedisTemplate` 或 `HashOperations` 对象来操作 Redis 的 Hash 结构。 ```java @Autowired private RedisTemplate<String, Object> redisTemplate; // 使用方法 HashOperations<String, String, Object> hashOps = redisTemplate.opsForHash("your-hash-key"); hashOps.put("field1", "value1"); hashOps.get("field1"); // 获取 value1 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值