【问题标题】:Spring cache EhCache | Data are not updated and deleted from cacheSpring缓存EhCache |数据不会从缓存中更新和删除
【发布时间】:2019-04-07 15:32:18
【问题描述】:

我开始学习 Spring Cache 抽象。 为此,我使用 Spring boot、Spring Data Jpa、EhCache 提供程序。

我的 ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ehcache>
<ehcache>

    <diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="100"
              eternal="false"
              timeToIdleSeconds="120"
              timeToLiveSeconds="120"
              overflowToDisk="true">
</defaultCache>

<cache name="teams"
       maxElementsInMemory="500"
       eternal="true"
       timeToIdleSeconds="0"
       timeToLiveSeconds="100"
       overflowToDisk="false">
</cache>

我的服务:

@CacheConfig(cacheNames = "teams")
@Service
public class TeamService {
    @Autowired
    private TeamRepository teamRepository;
    @Cacheable
    public Team findById(long id) {
        return teamRepository.findById(id).get();
    }
    @Cacheable
    public List<Team> findAll() {
        return teamRepository.findAll();
    }
    @CachePut
    public Team save(Team team) {
        return teamRepository.save(team);
    }
    @CacheEvict
    public void delete(long id) {
        teamRepository.deleteById(id);
    }
}

我的控制器:

@RestController
public class TeamController {
    @Autowired
    private TeamService teamService;
    @GetMapping("/teams")
    public List<Team> getAll() {
        return teamService.findAll();
    }
    @GetMapping("/team/{id}")
    public Team getById(@PathVariable long id) {
        return teamService.findById(id);
    }
    @DeleteMapping("/team/{id}")
    public void delete(@PathVariable long id) {
        teamService.delete(id);
    }
    @PostMapping("/team")
    public Team save(@RequestBody Team team) {
        return teamService.save(team);
    }
}

我正在向我的控制器执行请求...

当我执行控制器的 getAll() 方法时,数据被正确缓存,然后下次不执行对数据库的查询。然后我使用控制器的相应方法更新和删除数据库中的数据,这些服务方法分别标记为@CachePut 和@CacheEvict 并且必须刷新缓存。然后我再次执行上述 getAll() 方法并获得与第一次相同的响应,但我希望在执行删除和更新请求后刷新它。

我做错了什么或者我怎样才能得到想要的结果?

【问题讨论】:

  • 我也有同样的问题。你找到解决办法了吗?

标签: spring-boot caching ehcache spring-cache


【解决方案1】:

当您在方法上放置@Cachable 注释时,所有条目都将保留在缓存中,默认情况下添加一个名称,那么第一个 cachable 与第二个 cachable 不同,因此如果您想正常工作,您需要添加一个名称想要,例如:

@Cachable("teams") 

@Cachable("teams") 

@CachePut("teams")

@CacheEvict(value="teams", allEntries=true)

您可以在此链接中获取更多信息:https://www.baeldung.com/spring-cache-tutorial

也许最好的解决方案是:

@Cachable("team") 

@Cachable("teams") 

@Caching(put = { 
    @CachePut(value="team"), 
    @CachePut(value="teams") })

@Caching(evict = { 
    @CacheEvict(value="team", allEntries=true), 
    @CacheEvict(value="teams", allEntries=true) })

【讨论】:

  • 我在类级别上有 @CacheConfig(cacheNames = "teams") 注释,类似于您的方法。但是你直接声明我正在尝试的所有方法,它也不起作用)
  • 我更新了我的答案,请删除@CacheConfig 上的名称。你能试试吗?
  • 也许,对于这种方法,我需要在我的配置中定义一些“团队”缓存,因为我得到 java.lang.IllegalArgumentException: 找不到名为“团队”的缓存
  • 哦,对了,你需要创建一个缓存配置更多.. 只需复制并粘贴相同的 aans 更改名称...
猜你喜欢
  • 2016-09-20
  • 2012-12-12
  • 1970-01-01
  • 2016-12-31
  • 2023-04-09
  • 2023-03-15
  • 2021-04-04
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多