I work with Spring and EhCache
I have the following method
@Override @Cacheable(value="products", key="#root.target.PRODUCTS") public Set<Product> findAll() { return new LinkedHashSet<>(this.productRepository.findAll()); }
I have other methods for working with @Cacheable and @CachePut and @CacheEvict.
Now imagine that the database returns 100 products, and they are cached via key="#root.target.PRODUCTS" , then another method will insert - update - delete the item in the database. Therefore, products cached via key="#root.target.PRODUCTS" are no longer the same as the database.
I mean, check the following two two methods: they can update / delete the item and that the same item is cached in another key="#root.target.PRODUCTS"
@Override @CachePut(value="products", key="#product.id") public Product update(Product product) { return this.productRepository.save(product); } @Override @CacheEvict(value="products", key="#id") public void delete(Integer id) { this.productRepository.delete(id); }
I want to know if it is possible to update / delete an item in the cache via key="#root.target.PRODUCTS" , it will be 100 with the updated product or 499 if the Product was deleted.
My point is, I want to avoid the following:
@Override @CachePut(value="products", key="#product.id") @CacheEvict(value="products", key="#root.target.PRODUCTS") public Product update(Product product) { return this.productRepository.save(product); } @Override @Caching(evict={ @CacheEvict(value="products", key="#id"), @CacheEvict(value="products", key="#root.target.PRODUCTS") }) public void delete(Integer id) { this.productRepository.delete(id); }
I do not want to call again 500 or 499 products that will be cached in key="#root.target.PRODUCTS"
Is it possible? How?
Thanks in advance.