Currently I am using annotation @CacheEvict using cron job, after that reloading again using new calls, but not happy with this approach as I have multiple nods.
My requirement is
- Reload cache with new data after a certain time limit.
- Should be working in multiple nodes.
Looking for a elegant design.
2
Answers
Did you try using cache manager?
https://www.baeldung.com/spring-multiple-cache-managers
The @CacheEvict has following attributes.
Cache names to evict.
Alias for cacheNames.
SpEL expression for computing the key dynamically.
The bean name of the custom KeyGenerator to use.
The bean name of the custom CacheManager. It is used to create default CacheResolver if none is set already.
The bean name of the custom CacheResolver to use.
SpEL expression used for making the cache eviction operation conditional.
If true, all the entries inside the cache are removed.
If true, the cache eviction will occur before the method is invoked.
I think you take the risk to never be satisfied by a solution crafted inside your application code to solve an architectural design problematic of the deployment of the application.
If you need to control the behavior of a variable set of nodes, you better have to design a small independent tool (e.g. in the form of a micro-service) that will receive "meta" requests and do the right thing: calling @CacheEvict and then @Cache entry points of all nodes in the right order. The list of nodes and the list of entry points to be called could be easily defined in parameter files or in a datasource.
You can also make your pods listening to a stream of events or an AMQP broadcaster.