Spring @Cachable更新数据 [英] Spring @Cachable updating data

查看:89
本文介绍了Spring @Cachable更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自以下示例: SpringSource

@Cacheable(value = "vets")
public Collection<Vet> findVets() throws DataAccessException {
    return vetRepository.findAll();
}

findVets()确切地工作吗?

这是第一次,它从 vetRepository 获取数据并将结果保存在缓存中。但是,如果在数据库中插入了新的兽医,会发生什么?缓存是否会更新(即席即用的行为)?如果没有,我们可以配置它来更新吗?

For the first time, it takes the data from vetRepository and saves the result in cache. But what happens if a new vet is inserted in the database - does the cache update (out of the box behavior) ? If not, can we configure it to update ?



编辑:

但是,如果从外部源(例如,使用同一数据库的应用程序)更新数据库,会发生什么?

But what happens if the DB is updated from an external source (e.g. an application which uses the same DB) ?

推荐答案

@CachePut("vets")    
public void save(Vet vet) {..}

您必须告诉缓存对象是陈旧的。如果在不使用服务方法的情况下更改数据,那么当然会有问题。但是,您可以使用以下命令清除整个缓存

You have to tell the cache that an object is stale. If data change without using your service methods then, of course, you would have a problem. You can, however, clear the whole cache with

@CacheEvict(value = "vets", allEntries = true)    
public void clearCache() {..}

但这取决于缓存提供程序。如果另一个应用程序在没有通知您的应用程序的情况下更新了数据库,但是它使用了相同的缓存,那么另一个应用程序也可能也会更新缓存。

It depends on the Caching Provider though. If another app updates the database without notifying your app, but it uses the same cache it, then the other app would probably update the cache too.

这篇关于Spring @Cachable更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆