Spring Boot Sping Boot 应用程序上的多条件缓存

jdg4fx2g  于 2022-11-05  发布在  Spring
关注(0)|答案(1)|浏览(198)

我想缓存基于多个条件的参数的响应。

  1. @Caching(
  2. cacheable = {
  3. @Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 1"),
  4. @Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 2")
  5. }
  6. )
  7. public Student getStudentByID(String id)
  8. {
  9. try
  10. {
  11. System.out.println("Going to sleep for 5 Secs.. to simulate backend call.");
  12. Thread.sleep(1000*5);
  13. }
  14. catch (InterruptedException e)
  15. {
  16. e.printStackTrace();
  17. }
  18. if(id.equalsIgnoreCase("1")) return new Student(id,"Venkat","V");
  19. else if(id.equalsIgnoreCase("2")) return new Student(id,"Jeshwin","J");
  20. else
  21. return new Student(id,"Sajal" ,"V"+ new java.util.Date());
  22. }

在上面的例子中,id为1和2的Student被缓存,其余的应该以正常的流来获取结果。
请让我知道,你的想法。
已尝试使用这些注解缓存响应...但未按预期工作...

  1. @Caching(
  2. cacheable = {
  3. @Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 1"),
  4. @Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 2")
  5. }
  6. )
flseospp

flseospp1#

用Spring表达式语言试试这个。只使用下面的表达式,不使用其他的。

  1. @Cacheable(cacheNames ="student_name_", key= "#id", condition = "{#id == 1 && #id == 2}")

相关问题