springboot项目中使用redis做缓存

springboot项目中已经集成redis和mongodb,运行项目后mongodb里确实存储了一些信息。集成redis的主要目的是想使用redis做缓存,提高数据访问效率以及解决并发操作(类似于秒杀系统)。dao层,service层和controller层均已加入相关注解。redis没有起到缓存的作用,可能是配置文件写错了。
配置文件代码如下:

server:
  port: 8888
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/rainng_course?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
  redis:
    host: localhost
    port: 6379
    database: 0
  cache:
    type: redis
  session:
    store-type: redis
    timeout: 86400
  jackson:
    time-zone: Asia/Shanghai
  data:
    mongodb:
      url: mongodb://localhost:27017
mybatis-plus:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.rainng.studentsystem.model
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

启动类代码如下:

package com.rainng.studentsystem;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.rainng.studentsystem.dao.mapper")
//开启缓存功能
@EnableCaching
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

运行截图:

img


redis之前没有数据,上图是运行系统之后生成的数据。
redis里的数据有点看不懂,貌似是实现了缓存功能。希望广大网友能解答一下,感谢各位!

望采纳:
你可以看看这篇文章,说不定可以知道自己之前错那,更好的帮到你
https://blog.csdn.net/qq_47387991/article/details/127234145

麻烦贴一下Redis set key value的代码片段。从截图来看,key是spring:session:expirations:<timestamp>,value是你组装的数据,看起来是中文没有序列化为String形式就存储到Redis里,导致出现乱码