Spring Cache (basic knowledge + St. Regis takeaway items)

Spring Cache

basic introduction

Spring Cache is a framework that realizes the annotation-based caching function. It only needs to add an annotation to realize the caching function.
Spring Cache provides a layer of abstraction, and the underlying layer can switch between different cache implementations. Specifically, different caching technologies are unified through the CacheManager interface.
CacheManager is an abstract interface for various cache technologies provided by Spring.
For different caching technologies, different CacheManagers need to be implemented:
insert image description here

annotation

insert image description here

getting Started

Import pom dependencies

If it is using the basic, we only need to import this pom dependency. If you use redis cache, you need to add other dependencies.
insert image description here

On the startup class, add the annotation @EnableCaching

@CachePut adds cache

CachePut puts the return value of the method into the cache, and this annotation is generally written on the add method.
The parameter value indicates the name of the cache, and each cache can have multiple keys; the key of the key cache

@CachePut(value="userCache",key="#result.id"

result represents the return value object; while writing user represents the parameter, which has the same name as the parameter!
insert image description here
This way of writing uses ConcurrentMapCacheManager by default. The cache is based on memory. When the server is stopped and restarted, there will be no data in it.

@CacheEvict clears the cache

This annotation is generally written on the delete;update method to clear the cache
@CacheEvict clears the name of the specified cache
value cache, and each cache name can have multiple key key
cache keys
insert image description here
insert image description here

@Cacheable first judges in storage

This annotation is written on the get method.
@Cacheable first checks whether there is data in the cache, if there is data, returns the cached data directly; if not, calls the method and puts the return value in the cache.
insert image description here
As written above, no matter whether there is any data found out, it will be stored in the cache. The value of value may be null. The lower part will
judge whether the return value is empty or not.
insert image description here
The above one has an error: it is because the condition cannot use result
insert image description here

The bottom layer of the above cache is based on map, now we change the bottom layer to make it based on redis

redis implementation

insert image description here
Importing this dependency requires a little more implementation classes, but there is still no redis
to import this dependency. It
insert image description here
insert image description here
insert image description here
should be noted that when adding a cache, the returned result must implement the serialization interface , or an error will not be reported! ! !
Add cache ------- query method

 @Cacheable(value="setmealCache",key = "#setmeal.categoryId")

Delete all cached data ------------ Modify, delete and add methods

@CacheEvict(value = "setmealCache",allEntries = true)
@Cacheable(value="setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")

Guess you like

Origin blog.csdn.net/weixin_45757641/article/details/131682342