springboot using Redis (a use SpringCache)

Part springboot use SpringCache say, why use redis, and I read on the Internet, SpringCache itself is an abstract realization of system cache, the cache and no specific ability, to use SpringCache also need to meet the specific cache implementations carry out.

redis is one of the above mentioned specific cache.

Use springboot redis operation has three kinds of programs

1.Spring Cache
2. Spring Data Redis
3. Jedis

This article talk about the first method: Spring Cache

Spring3.1 introduced in the beginning of an exciting Cache, in Spring Boot can be very convenient to use Redis as to achieve Cache, and then implement caching data.

1.pom.xml

 <!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- Cache -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

2.application.properties

Simple configuration information Redis and give a name cache

#redis
spring.redis.port=6379
spring.redis.host=localhost

3. open the cache, and let the entity classes implement the sequence of interfaces

Add Program Entry above category @EnableCaching to open the cache

@Data
public class Category implements Serializable {
    private int id;
    private String name;
}

After completing these configurations, Spring Boot will automatically help us configure a background RedisCacheManager of Bean, the RedisCacheManager indirectly to achieve the Cache interface in Spring, with this Bean, we can directly use the cache annotations and interfaces Spring in the , while the cache data will automatically be stored on the Redis . In the single 's Redis, this Bean system will automatically provide , if Redis cluster , the Bean needs developers to offer .

Then you can use annotations to achieve a spring cache

See my previous article (annotated parameter learning)
https://blog.csdn.net/m0_45025658/article/details/104217082

4. See Effect

redis has named a cache of categoryCache
Here Insert Picture Description

5 with source code (including database)

//download.csdn.net/download/m0_45025658/12148451

This is the first method, the next article describes ** Spring Date Redis **

Published 33 original articles · won praise 1 · views 2050

Guess you like

Origin blog.csdn.net/m0_45025658/article/details/104242269