小编sum*_*nik的帖子

如何在Spring中访问缓存值

场景:我需要在另一种方法中访问作为一种方法的一部分创建的缓存的值。我该怎么做?

public class Invoice {
  private String invoiced;
  private BigDecimal amount;
  //Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)

方法 1:当客户想要从 UI 获取发票列表时调用

@Cacheable(value="invoices")
public List<Invoice> getAllInvoices(String customerId){
...
//Get all invoices from Database and return 
...
return invoices
}
Run Code Online (Sandbox Code Playgroud)

方法2:当客户点击UI上的下载时调用

public File downloadInvoice(String invoiceId) {
 //TODO: 
 //Validate if invoiceId is present in the cache. This is a validation step
 //How can I access the cache "invoices" here.
 ...
 //if InvoiceId is present in cache then download from db else throw Exception
 return file; …
Run Code Online (Sandbox Code Playgroud)

spring caching spring-mvc spring-boot spring-cache

4
推荐指数
1
解决办法
3万
查看次数

标签 统计

caching ×1

spring ×1

spring-boot ×1

spring-cache ×1

spring-mvc ×1