logo 范 · 拾光录
网址收集 关于作者 Github Gitee
杂文随笔4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端编程3
前端基础知识16
UniApp15
Vue框架19
Java编程7
Java基础知识14
SpringBoot31
SpringMVC14
MyBatis8
SpringCloud15
RocketMQ1
MyCat1
数据库4
MySQL6
Redis7
MongoDB10
H21
Java技术栈4
知识整理分布式锁的实现方案MySQL事务MySQL的锁
软件工具箱14
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaPicGoRustFSVSCodeDockerObsidianObs录制
Linux知识11
树莓派安装及使用ArchLinux:常用软件ArchLinux:基础系统安装ArchLinux:深度优化ArchLInux:图形化界面安装ArchLinux:Nirifrp内网穿透Jar启动脚本Linux常用命令VirtualBox安装CentOSVirtualBox安装Ubuntu
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

依赖

<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

配置

# 数据
spring:
  # redis配置
  redis:
    # Redis数据库索引(默认为0)
    database: 1
    # Redis服务器地址
    host: 120.48.50.81
    # Redis服务器连接端口
    port: 6378
    # Redis服务器连接密码(默认为空)
    password: xxx
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池最大连接数
        max-active: 200
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
        # 连接池中的最大空闲连接
        max-idle: 10
        # 连接池中的最小空闲连接
        min-idle: 0

使用

@Resource
private RedisTemplate<String, String> redisTemplate;


// 获取
String redisData = redisTemplate.opsForValue().get(token);
// 放入
redisTemplate.opsForValue().set("system:adminUser:" + token, redisData);
redisTemplate.opsForValue().set(RedisKeyEnum.KEY_WEIXIN_ACCESS_TOKEN.value(), accessToken, 7000, TimeUnit.SECONDS);

命名

key管理

最好使用枚举对所有使用到的key进行统一个管理:

/**
 * redis的key枚举
 */
public enum RedisKeyEnum {

    /**
     * 后台管理员登录后信息
     */
    KEY_ADMIN_USER("system:admin:user:");

    private final String value;

    RedisKeyEnum(String value) {
        this.value = value;
    }

    public String value() {
        return value;
    }

}

加过期时间

// 秒
redisTemplate.opsForValue().set("", "", 60, TimeUnit.SECONDS);
// 天
redisTemplate.opsForValue().set(RedisKeyEnum.KEY_ADMIN_USER.value() + token, json, 30, TimeUnit.DAYS);

存储乱码问题

增加配置类:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

@Configuration
public class RedisConfig {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Bean
    public RedisTemplate<String, Object> redisTemplateInit() {
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

    /**
     * 保持Redis连接,防止连接断开
     */
    @Scheduled(cron = "0 * * * * ?") // 每一分钟一次
    public void keepAlive() {
        redisTemplate.execute((RedisCallback<Object>) connection -> {
            System.out.println("redis keep alive");
            connection.ping();
            return null;
        });
    }

}
依赖
配置
使用
命名
key管理
加过期时间
存储乱码问题