Cache Annotations in Spring Boot
Caching is a very important topic in Spring Boot. When we need to read some data frequently, in order to improve performance, these data can be cached to avoid reading from the database every time. In order to implement caching, Spring Boot provides some caching annotations, which can easily implement caching functions.
What are cache annotations?
Spring Boot provides four cache annotations, namely:
@Cacheable
@CachePut
@CacheEvict
@Caching
These annotations can be used to mark that a method needs to be cached, or that the cache needs to be updated or deleted.
The principle of caching annotations
In Spring Boot, caching is implemented through a cache manager. The cache manager is responsible for operations such as creating, reading, updating, and deleting caches. Spring Boot provides a variety of cache manager implementations, such as Ehcache, Redis, Caffeine, etc.
When a method is marked as a cache method, Spring Boot will first check whether there is a cache, and if so, read the data directly from the cache. If not present in the cache, the method is executed and the result is cached in the cache.
When a method is marked to update or delete the cache, Spring Boot will update or delete the cache according to the parameters in the annotation. For example, @CachePut
an annotation caches the result of a method, while @CacheEvict
an annotation deletes the cache.
How to use cache annotation?
In Spring Boot, caching can be enabled by adding caching annotations to methods. Here are four commonly used cache annotations.
@Cacheable
@Cacheable
Annotations can mark a method to be cached. In the annotation, you can specify the name of the cache and the key of the cache. For example:
@Cacheable(value = "users", key = "#id")
public User getUserById(Long id) {
// 从数据库中读取用户信息
}
In the above example, the name of the cache is users
and the key of the cache is the parameter of the method id
. When the method is executed, Spring Boot will first look up the cache. If there is corresponding data in the cache, it will read it directly from the cache, otherwise execute the method and cache the result in the cache.
@CachePut
@CachePut
An annotation can mark a method that needs to update the cache. In the annotation, you can specify the name of the cache and the key of the cache. For example:
@CachePut(value = "users", key = "#user.id")
public User updateUser(User user) {
// 更新数据库中的用户信息
}
In the above example, the name of the cache is users
and the key of the cache is the method return value user.id
. When the method is executed, Spring Boot will update the data in the cache.
@CacheEvict
@CacheEvict
An annotation can mark a method that needs to delete the cache. In the annotation, you can specify the name of the cache and the key of the cache. For example:
@CacheEvict(value = "users", key = "#id")
public void deleteUserById(Long id) {
// 删除数据库中的用户信息
}
In the above example, the name of the cache is users
and the key of the cache is the parameter of the method id
. When the method is executed, Spring Boot will delete the corresponding data in the cache.
@Caching
@Caching
Annotations can combine multiple cache annotations together. For example:
@Caching(
cacheable = @Cacheable(value = "users", key = "#id"),
put = @CachePut(value = "users", key = "#result.id"),
evict = @CacheEvict(value = "allUsers", allEntries = true)
)
public User getUserById(Long id) {
// 从数据库中读取用户信息
}
In the example above, @Caching
the annotation contains three cache annotations: @Cacheable
, @CachePut
and @CacheEvict
. When the method is executed, Spring Boot will first look up the cache, and if the corresponding data exists in the cache, it will be read directly from the cache; if it does not exist in the cache, the method will be executed and the result will be cached in the cache; at the same time, the cache will be users
updated , and delete allUsers
all data in the cache.
Configuration of cache annotations
In Spring Boot, cache properties can be configured through configuration files. Here is an example configuration file that uses Redis as the cache manager:
spring:
cache:
type: redis
redis:
host: localhost
port: 6379
password: password
time-to-live: 30000
In the above example, Redis is used as the cache manager, and the host address, port number, password and timeout of Redis are set. It can be configured according to the actual situation.
code example
Below is a code example using the cache annotation. In this example, we define a UserService
class with a getUserById()
method and a updateUser()
method. The caching annotation is added to the method, which can conveniently realize the caching function.
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Cacheable(value = "users", key = "#id")
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
@CachePut(value = "users", key = "#user.id")
public User updateUser(User user) {
userRepository.save(user);
return user;
}
}
In the above example, getUserById()
the method is marked as @Cacheable
annotation, the name of the cache is users
, and the key of the cache is the parameter of the method id
; updateUser()
the method is marked as @CachePut
annotation, the name of the cache is users
, and the key of the cache is the method return value user.id
. When the method is executed, Spring Boot will first look up the cache. If there is corresponding data in the cache, it will read it directly from the cache, otherwise execute the method and cache the result in the cache.
Summarize
Caching is very important in Spring Boot. By using cache annotations, you can easily implement the cache function and improve the performance of the program. In the code, we can enable the cache function by using @Cacheable
, @CachePut
, @CacheEvict
and @Caching
annotations, or configure the cache properties through the configuration file.