skip to Main Content

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

  1. Reload cache with new data after a certain time limit.
  2. Should be working in multiple nodes.

Looking for a elegant design.

2

Answers


  1. Did you try using cache manager?

    https://www.baeldung.com/spring-multiple-cache-managers

    1. Spring @CacheEvict annotation is used to evict cache.
    2. The @CacheEvict is used at method level.
    3. The @Cacheable annotation sets the value in cache and on the contrary @CacheEvict evicts the cache.
    4. At one method we can use @Cacheable to cache result and at another method we can use @CacheEvict to evict cache.
    5. The @CacheEvict annotation is introduced in Spring 3.1.

    The @CacheEvict has following attributes.

    1. String[] cacheNames
      Cache names to evict.
    2. String[] value
      Alias for cacheNames.
    3. String key
      SpEL expression for computing the key dynamically.
    4. String keyGenerator
      The bean name of the custom KeyGenerator to use.
    5. String cacheManager
      The bean name of the custom CacheManager. It is used to create default CacheResolver if none is set already.
    6. String cacheResolver
      The bean name of the custom CacheResolver to use.
    7. String condition
      SpEL expression used for making the cache eviction operation conditional.
    8. boolean allEntries
      If true, all the entries inside the cache are removed.
    9. boolean beforeInvocation

    If true, the cache eviction will occur before the method is invoked.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search