I use Redis for caching and have the following service method:
@Cacheable(value = "productCache")
@Override
public List<ProductDTO> findAllByCategory(Category category) {
// code omitted
return productDTOList;
}
When I pass categoryA
to this method, the result is cached and is kept during expiration period. If I pass categoryB
to this method, it is retrieved from database and then kept in cache. Then if I pass categoryA
again, it is retrieved from cache.
1. I am not sure if it is normal, because I just use value
parameter ("productCache") of @Cacheable
annotation and have no idea how it caches categoryA
and categoryB
results separately. Could you please explain how it works?
2. As mentioned on this page, there is also key
parameter. But when using it as shown below, I think it does not make any sense and it works as above. Is that normal or am I missing something?
@Cacheable(value = "productCache", key="#category")
@Override
public List<ProductDTO> findAllByCategory(Category category) {
// code omitted
return productDTOList;
}
3. Should I get cache via Cache cache = cacheManager.getCache("productCache#" + category);
?