拾光记录
218个文档 导航 作者 Gitee
随笔记录4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端知识4
基础知识17
Vue框架19
UniApp14
微信小程序1
Java编程6
Java基础15
SpringBoot31
SpringMVC18
MyBatis9
SpringCloud15
中间件2
数据库4
MySQL13
Redis8
MongoDB10
其他数据库1
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv管理工具
Linux12
Linux常用命令Jar启动脚本VirtualBox安装CentOSVirtualBox安装Ubuntu树莓派安装及使用frp内网穿透ArchLinux:基础系统安装ArchLInux:图形化界面安装ArchLinux:常用软件ArchLinux:深度优化ArchLinux:NiriArchLinux:模块记录
软件工具12
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaRustFSPicGoVSCodeDocker
创意设计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管理加过期时间存储乱码问题