Spring Boot 接入 Redis

Spring Boot 接入 Redis

简介

Redis 是一种访问速度非常快的内存数据结构存储,用作数据库、缓存、消息代理和流引擎。提供 strings、hashes、lists、sets 等数据结构。可以解决会话缓存、消息队列、分布式锁、定期将数据集存储到硬盘等功能。

如果需要通过 Redis 设计实现更详细的功能,可查阅文章底部链接,特别是接口文档,它涵盖了所有可实现功能。

实现功能

  1. 创建Spring Boot 项目
  2. Windows 环境下启动 Redis 服务;
  3. 实现会话缓存功能;
  4. 测试会话缓存功能;

一、创建 Spring Boot 项目并启动 Redis

1.使用任意方式(idea、eclipse、https://start.spring.io/等)搭建项目后,添加 Redis 以及 FastJSON 依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.48</version>
</dependency>

二、Windows 环境下启动 Redis 服务

1.下载 Redis Windows 客户端(https://github.com/tporadowski/redis/releases),进入根目录执行命令启动 Redis:

redis-server redis.windows.conf

三、实现会话缓存功能

1.创建并注入连接 Redis 会话工厂配置文件 RedisConfig.java :

package com.ikgade.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Configuration
public class RedisConfig {

    @Bean
    LettuceConnectionFactory redisConnectionFactory() {
        // 连接 redis 配置
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setPassword(""); // 未设置密码也要配置为空 否则抛出 NO AUTH 异常
        return new LettuceConnectionFactory(configuration);
    }
}

2.创建并实现会话缓存控制器 SessionController.java :

package com.ikgade.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/session")
public class SessionController {

    @Resource
    RedisTemplate<String, String> redisTemplate;

    @PostMapping("/set")
    public JSONObject setMessage(@RequestBody JSONObject jsonObject){
        redisTemplate.opsForValue().set(jsonObject.getString("key"), jsonObject.getString("val"));
        return jsonObject;
    }

    @GetMapping("/get")
    public JSONObject getMessage(@RequestBody JSONObject jsonObject){
        JSONObject res = new JSONObject();
        res.put("val", redisTemplate.opsForValue().get(jsonObject.getString("key")));
        return res;
    }
}

四、测试会话缓存功能

1.存储会话缓存:
在这里插入图片描述
2.获取会话缓存:
在这里插入图片描述

参考资料

参考手册链接:
https://docs.spring.io/spring-data/redis/docs/current/reference/html/
https://docs.spring.io/spring-data/redis/docs/2.6.10/reference/html/
案例仓库:
https://github.com/spring-projects/spring-data-examples/tree/main/redis
接口文档:
https://docs.spring.io/spring-data/redis/docs

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值