SpringBoot中Redis的常规使用

在 Spring Boot 中使用 Redis 可以借助 Spring Data Redis 来简化操作,以下为你详细介绍在 Spring Boot 项目里对 Redis 进行基本使用的步骤:

1. 创建 Spring Boot 项目

你可以使用 Spring Initializr(https://start.spring.io/ )创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Web:用于构建 Web 应用。
  • Spring Data Redis:用于与 Redis 进行交互。

或者在 pom.xml 文件中手动添加依赖:

<dependencies>
    <!-- Spring Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Data Redis -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

2. 配置 Redis 连接信息

application.propertiesapplication.yml 文件中配置 Redis 的连接信息,以 application.yml 为例:

spring:
  redis:
    host: localhost  # Redis 服务器地址
    port: 6379       # Redis 服务器端口
    password:        # Redis 服务器密码,如果没有则留空

3. 创建 Redis 操作服务类

创建一个服务类,用于封装对 Redis 的基本操作,例如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置键值对
     * @param key 键
     * @param value 值
     */
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 设置键值对并指定过期时间
     * @param key 键
     * @param value 值
     * @param timeout 过期时间
     * @param unit 时间单位
     */
    public void set(String key, Object value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    /**
     * 获取键对应的值
     * @param key 键
     * @return 值
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 删除指定的键
     * @param key 键
     * @return 是否删除成功
     */
    public boolean delete(String key) {
        return Boolean.TRUE.equals(redisTemplate.delete(key));
    }
}

4. 在控制器中使用 Redis 服务

创建一个控制器类,调用 RedisService 中的方法进行 Redis 操作,示例如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    /**
     * 设置键值对
     * @param key 键
     * @param value 值
     * @return 操作结果
     */
    @PostMapping("/set")
    public String set(@RequestParam String key, @RequestParam String value) {
        redisService.set(key, value);
        return "Set successfully";
    }

    /**
     * 获取键对应的值
     * @param key 键
     * @return 值
     */
    @GetMapping("/get")
    public Object get(@RequestParam String key) {
        return redisService.get(key);
    }

    /**
     * 删除指定的键
     * @param key 键
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public String delete(@RequestParam String key) {
        boolean result = redisService.delete(key);
        return result ? "Delete successfully" : "Delete failed";
    }
}

5. 运行项目并测试

启动 Spring Boot 项目后,可以使用工具(如 Postman)进行测试:

  • 设置键值对:发送 POST 请求到 http://localhost:8080/redis/set?key=testKey&value=testValue
  • 获取键对应的值:发送 GET 请求到 http://localhost:8080/redis/get?key=testKey
  • 删除指定的键:发送 DELETE 请求到 http://localhost:8080/redis/delete?key=testKey

通过以上步骤,你就可以在 Spring Boot 项目中实现对 Redis 的基本使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值