All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.jcache.interceptor.ExpirationTrackingInterceptor Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.infinispan.jcache.interceptor;

import org.infinispan.commands.read.GetKeyValueCommand;
import org.infinispan.container.DataContainer;
import org.infinispan.container.entries.InternalCacheEntry;
import org.infinispan.context.InvocationContext;
import org.infinispan.interceptors.base.CommandInterceptor;
import org.infinispan.jcache.JCacheNotifier;
import org.infinispan.util.TimeService;

import javax.cache.Cache;

/**
 * An interceptor that tracks expiration of entries and notifies JCache
 * {@link javax.cache.event.CacheEntryExpiredListener} instances.
 *
 * This interceptor must be placed before
 * {@link org.infinispan.interceptors.EntryWrappingInterceptor} because this
 * interceptor can result in container entries being removed upon expiration
 * (alongside their metadata).
 *
 * TODO: How to track expired entry in cache stores?
 * TODO: Could this be used as starting point to centrally track expiration?
 * Currently, logic split between data container, cache stores...etc.
 *
 * @author Galder Zamarreño
 * @since 5.3
 */
public class ExpirationTrackingInterceptor extends CommandInterceptor {

   private final DataContainer container;
   private final Cache cache;
   private final JCacheNotifier notifier;
   private final TimeService timeService;

   @SuppressWarnings("unchecked")
   public ExpirationTrackingInterceptor(DataContainer container,
         Cache cache, JCacheNotifier notifier, TimeService timeService) {
      this.container = container;
      this.timeService = timeService;
      this.cache = (Cache) cache;
      this.notifier = (JCacheNotifier) notifier;
   }

   @Override
   public Object visitGetKeyValueCommand
         (InvocationContext ctx, GetKeyValueCommand command) throws Throwable {
      Object key = command.getKey();
      InternalCacheEntry entry = container.peek(key);
      if (entry != null && entry.canExpire() && entry.isExpired(timeService.wallClockTime()))
         notifier.notifyEntryExpired(cache, key, entry.getValue());

      return super.visitGetKeyValueCommand(ctx, command);
   }

   // TODO: Implement any other visitX methods?

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy