nilan59
nilan59

Reputation: 1166

Do Something when putifabsent time expired [infinispan]

Intro

This is regarding infinispan cache but I think this is a generic enough question.

In my infinispan cache Im inputting items into cache using putIfAbsent method and removing them with remove method. (jboss doc here)

Basic Behavior

I can put items into my cache and remove items from cache using a id. But if I do not remove the cache item explicitly, infinispan will remove it automatically after the specified life span is passed.

In my remove method I run some custom code before removal. But when I am not invoking the remove method, infinispan remove the cache entry due to the expiry of provided life span.

In that automatic removal scenario I cannot run my custom code because it happens under the hood. (by infinispan). So what is the possible way? Following code will explain this a bit more hopefully.

I belive I am clear enough. Please give me some insight. Thank you.

void putToCache(String id){
   myCache.putIfAbsent(id,value,LIFESPAN_DURATION,timeUnit);
}

void removeFromCache(String id){
  //MY CUSTOM CODE - WANT TO RUN THIS IN LIFE SPAN DURATION EXPIRY ALSO
   myCache.remove(id);
}

Upvotes: 3

Views: 443

Answers (1)

Mudokonman
Mudokonman

Reputation: 909

Before going into the answer I want to clarify between eviction and expiration.

Eviction

Eviction is when an entry is removed from the in memory container due to it growing outside of the configured maximum size (max-entries). Eviction can only be enabled at configuration time and is not controlled at runtime which Expiration can be, please see here. Note that eviction never removes an entry from a store if you have one configured, thus you don't just lose an entry due to having too many if you don't want to.

Infinispan 7 and older versions have a listener for Eviction as you already found, and the event raised would be this one.

Expiration

Expiration defines an entry being removed after a period of inactivity or a set duration. This can be configured through configuration or you can override the configured value by using the API as you did your in example.

Now to answer your question, an Infinispan Expiration event was added with Infinispan 8. It is detailed more here. Note an important piece is that the expiration event will be fired holding the lock for the key that is expiring (unless your listener is defined as async). This is important to guarantee proper ordering.

Also you should keep in mind that expiration events are not guaranteed to fire exactly when the entry expires. Rather the event is only fired upon accessing said expired entry or if the expiration reaper thread finds the expired entry. The expiration reaper defaults to run every minute, you can disable this or change the time by changing the expiration interval configuration setting.

Upvotes: 5

Related Questions