Spring Boot中cache+redis的简单使用

一、需要准备的材料
1.安装redis,Linux需要再配置远程访问权限;
2.安装mysql或者其他关系型数据库;
3.eclipse或者idea并且安装jdk;
二、配置
1.配置mysql数据源、配置redis、配置cache、

#配置mysql数据源
spring.datasource.url=jdbc:mysql://localhost:3306/myblog?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#cache+redis的配置
#cache类型
spring.cache.type=redis
#redisIP地址
spring.redis.host=127.0.0.1
#redis端口
spring.redis.port=6379
#cache缓存的名字,可以有多个缓存模块(一般一个name代表一个实体类)
spring.cache.cache-names=category

2.代码中的service层加上注解
(1)在增删改的操作的service实现层的方法上加上注解@CacheEvict更新数据

@CacheEvict(cacheNames="category",allEntries=true)

(2)在查询的方法上加上注解

@Cacheable(cacheNames="category",key="#p0")

猜你喜欢

转载自blog.csdn.net/qq_41844287/article/details/98038618
今日推荐