【发布时间】:2018-08-14 16:23:28
【问题描述】:
我有一个 spring rest api 应用程序,它使用 HATEOAS/PagingAndSortingRepository 来完成大部分繁重的工作。
我已经使用 guava 实现了缓存,但是我遇到了问题,当用户在 api 调用中途取消请求时,它会缓存不完整的 json 并重新提供 60 秒。
我正在尝试使用@Cacheable 注释的unless="" 参数。以前,我只是使用unless="#result == null",但它不能处理不完整或无效的 json。
这似乎也不起作用。所以现在我正在尝试使用com.google.gson.JsonParser 来解析结果并在适用时使其无效。
存储库
@RepositoryRestResource(path = "products", collectionResourceRel = "products")
public interface ProductEntityRepository extends PagingAndSortingRepository<ProductEntity, String> {
JsonParser parser = new JsonParser();
@Cacheable(value = CacheConfig.STORE_CACHE)
ProductEntity findByName(String name);
}
缓存配置
public final static String PRODUCTS_CACHE = "products";
@Bean
public Cache productsCache() {
return new GuavaCache(PRODUCTS_CACHE, CacheBuilder.newBuilder()
.expireAfterWrite(60, TimeUnit.SECONDS)
.build());
}
如何检测unless=""参数中的无效json?
【问题讨论】:
标签: spring spring-boot spring-data-jpa spring-data guava