如何在使用angular7和springboot构建的Web应用程序中使浏览器缓存无效

时间:2019-04-24 17:01:52

标签: java angular spring-boot

我正在使用springboot开发一个Web应用程序,该服务用于后端服务其余API,而Angular7用于前端。

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

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

是否可以通过修改某些方法来使浏览器缓存中的某些数据无效?

Springboot:

我的休息控制器端点类似于:

  @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();
  }

我的角度服务:

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

1 个答案:

答案 0 :(得分:0)

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

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

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

@Autowired
CacheManager cacheManager;

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