cachingignitecache-expiration

Need to execute code before Ignite cache ExpiryPolicy


I am using Ignite cache with ModifiedExpiryPolicy and need to execute a line of code before event execution. Any help?

IgniteCache<String, Object> expiresCache = cache.withExpiryPolicy(new ModifiedExpiryPolicy(new Duration(Time.MINUTES, timeInMins)));

public class ClassName {
public IgnitePredicate<CacheEvent> functionName() {
    return new IgnitePredicate<CacheEvent>() {  
        @Override
        public boolean apply(CacheEvent evt) {
            //code to be executed after event.
            return true;
            }
        };
    }
}

Solution

  • I think you need to use events to listen for expiry events.

    Ignite ignite = Ignition.ignite();
    
    // Local listener that listenes to local events.
    IgnitePredicate<CacheEvent> locLsnr = evt -> {
      System.out.println("Received expiry event [evt=" + evt.name() + ", key=" + evt.key());
    
      return true; // Continue listening.
    };
    
    // Subscribe to specified cache events occuring on local node.
    ignite.events().localListen(locLsnr, EventType.EVT_CACHE_OBJECT_EXPIRED);
    

    Note that this is just a local (node) listener, you'll need a remote listener to find expiry events on remote nodes. You'll also need to configure includeEventTypes in your configuration file (events are disabled by default for performance reasons.