I would like to cache the response based on parameter with multiple conditions. I'm giving below example
@Caching(
cacheable = {
@Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 1"),
@Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 2")
}
)
public Student getStudentByID(String id)
{
try
{
System.out.println("Going to sleep for 5 Secs.. to simulate backend call.");
Thread.sleep(1000*5);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(id.equalsIgnoreCase("1")) return new Student(id,"Venkat","V");
else if(id.equalsIgnoreCase("2")) return new Student(id,"Jeshwin","J");
else
return new Student(id,"Sajal" ,"V"+ new java.util.Date());
}
In above example, Student with id 1 and 2 are cached and rest should be going to fetch results with normal flow.
Please let me know, your thoughts.
Tried caching the response with these annotations...but it is not working as expected...
@Caching(
cacheable = {
@Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 1"),
@Cacheable(cacheNames ="student_name_", key= "#id", condition = "#id == 2")
}
)