Simple use of cache+redis in Spring Boot


1. Materials to be prepared 1. To install redis, Linux needs to configure remote access permissions;
2. Install mysql or other relational databases;
3. eclipse or idea and install jdk;
2. Configure
1. Configure mysql data source, configure redis , Configure 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. Add annotations to the service layer in the code
(1) Add annotations @CacheEvict to update the data on the method of the service implementation layer of the addition, deletion and modification operations

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

(2) Annotate the query method

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

Guess you like

Origin blog.csdn.net/qq_41844287/article/details/98038618