gpt4 book ai didi

java - 如何在使用 Angular 和 SpringBoot 构建的 Web 应用程序中使浏览器缓存失效

转载 作者:行者123 更新时间:2023-12-02 06:20:42 27 4
gpt4 key购买 nike

我正在开发一个 Web 应用程序,使用 springboot 作为后端服务 Rest API,并使用 Angular7 作为前端。

我的应用程序需要花费很多时间来加载,因为它必须在服务器上执行大量处理,因此我决定通过存储缓存数据来提高性能,以便首先加载页面并最终在处理时更新数据结束。

这可行,但我遇到了问题:当我在 Angular 中更新数据时,这些数据通常保存在我的数据库中,但如果我更新页面,我看不到更改,因为浏览器继续访问旧的缓存数据,而不是修改新数据。

有没有办法让浏览器缓存中的某些数据在修改时失效?

Springboot:

我的其余 Controller 端点与此类似:

  @GetMapping(value = "/users")
public Iterable<User> getAllUsers(HttpServletResponse response) {
response.setHeader("Cache-Control", "max-age=3600");
return this.userService.findAll());
}

我的服务:

  @Cacheable("users")
public Iterable<User> findAll() {
return this.userRepository.findAll();
}

我的 Angular 服务:

  getUsers(): Observable<User[]> {
return this.http.get<User[]>(`<ip>' + '/users');
}

最佳答案

我可以看到您正在服务器端进行缓存,您可以通过使用您想要逐出的 key 调用带有 @CacheEvict 注释的方法来逐出缓存:

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

或者您可以使用 CacheManager 以编程方式执行此操作:

@Autowired
CacheManager cacheManager;

public void evictSingleCacheValue(String cacheName, String cacheKey) {
cacheManager.getCache(cacheName).evict(cacheKey);
}

关于java - 如何在使用 Angular 和 SpringBoot 构建的 Web 应用程序中使浏览器缓存失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com