Java Spring 分别缓存列表中的所有元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51237672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring cache all elements in list separately
提问by Federico Piazza
I'm trying to add caching to a CRUD app, I started doing something like this:
我正在尝试向 CRUD 应用程序添加缓存,我开始做这样的事情:
@Cacheable("users")
List<User> list() {
return userRepository.findAll()
}
@CachePut(value = "users", key = "#user.id")
void create(User user) {
userRepository.create(user)
}
@CachePut(value = "users", key = "#user.id")
void update(User user) {
userRepository.update(user)
}
@CacheEvict(value = "users", key = "#user.id")
void delete(User user) {
userRepository.delete(user)
}
The problem I have is that I would like that create/update/delete operations can update the elements already stored in the cache for the list()
operation (note that list()
is not pulling from database but an data engine), but I am not able to do it.
我遇到的问题是,我希望创建/更新/删除操作可以更新已存储在缓存中的元素以进行list()
操作(请注意,这list()
不是从数据库中提取,而是从数据引擎中提取),但我无法做到.
I would like to cache all elements returned by list()
individually so all other operations can update the cache by using #user.id
. Or perhaps, make all operations to update the list already stored in cache.
我想缓存list()
单独返回的所有元素,以便所有其他操作都可以使用#user.id
. 或者,进行所有操作以更新已存储在缓存中的列表。
I read that I could evict the whole cache when it is updated, but I want to avoid something like:
我读到我可以在更新时驱逐整个缓存,但我想避免类似的事情:
@CacheEvict(value = "users", allEntries=true)
void create(User user) {
userRepository.create(user)
}
Is there any way to create/update/remove values within a cached collection? Or to cache all values from a collection as individual keys?
有没有办法在缓存集合中创建/更新/删除值?或者将集合中的所有值缓存为单独的键?
采纳答案by Federico Piazza
I'll self answer my question since no one gave any and could help others.
我会自己回答我的问题,因为没有人给出任何并可以帮助他人。
The problem I had when dealing with this issue was a problem of misconception of Cache usage. My need posted on this question was related to how to update members of a cached list (method response). This problem cannot be solved with cache, because the cached value was the list itself and we cannot update a cached value partially.
我在处理这个问题时遇到的问题是对缓存使用的误解问题。我在这个问题上发布的需求与如何更新缓存列表的成员(方法响应)有关。这个问题不能用缓存解决,因为缓存的值是列表本身,我们不能部分更新缓存的值。
The way I wanted to tackle this problem is related to a "map" or a distributed map, but I wanted to use the @Cacheable
annotation. By using a distributed map would have achieved what I asked in my question without using @Cacheable
. So, the returned list could have been updated.
我想解决这个问题的方式与“地图”或分布式地图有关,但我想使用@Cacheable
注释。通过使用分布式地图,无需使用@Cacheable
. 因此,返回的列表可能已更新。
So, I had (wanted) to tackle this problem using @Cacheable
from other angle. Anytime the cache needed to update I refreshed it with this code.
所以,我(想要)@Cacheable
从其他角度解决这个问题。每当缓存需要更新时,我都会使用此代码对其进行刷新。
I used below code to fix my problem:
我使用下面的代码来解决我的问题:
@Cacheable("users")
List<User> list() {
return userRepository.findAll()
}
// Refresh cache any time the cache is modified
@CacheEvict(value = "users", allEntries = true")
void create(User user) {
userRepository.create(user)
}
@CacheEvict(value = "users", allEntries = true")
void update(User user) {
userRepository.update(user)
}
@CacheEvict(value = "users", allEntries = true")
void delete(User user) {
userRepository.delete(user)
}
In addition, I have enabled the logging output for spring cache to ensure/learn how the cache is working:
此外,我已启用 spring 缓存的日志输出以确保/了解缓存的工作方式:
# Log Spring Cache output
logging.level.org.springframework.cache=TRACE