Re: removallistener in guava maps

64 views
Skip to first unread message

Jayanth Anish

unread,
Jun 19, 2023, 3:38:38 AM6/19/23
to guava-discuss
I have created my cahe map and added removal listener like the below code:

userTokenCache = CacheBuilder.newBuilder()
.expireAfterAccess(1*60*1000,
TimeUnit.MILLISECONDS)
.removalListener(new CustomRemovalListener())
.build();

static class CustomRemovalListener implements RemovalListener<String, String> {

@Override

public void onRemoval(RemovalNotification<String, String> notification) {

String removedSessionId = notification.getKey();

String removedUserName = notification.getValue();

// Perform logging or any other action

System.out.println("Expired session: " + removedSessionId + " - " + removedUserName);

}

} But still when the entry expires , the values are not printing. I have few doubts. 1) will the removelistener will be called automatically whenever an entry expires in the map? 2) If not what are the ways to get the removelistener to be triggered automatically when an entry gets expired from the map. Thanks !

Ben Manes

unread,
Jun 19, 2023, 3:42:19 AM6/19/23
to Jayanth Anish, guava-discuss
It piggybacks on other activity, so if the cache is idle then the notification will be delayed. You can set a periodic task to trigger cleanUp(). Alternatively, you can use Caffeine and set a scheduler to do this for you.

--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guava-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/9be81402-7430-48d7-abc7-68dcdddc951cn%40googlegroups.com.

Jayanth Anish

unread,
Jun 19, 2023, 3:45:41 AM6/19/23
to guava-discuss
So it means without any explicit method like cleanup or running any thread explicitly, the expired entries wont be printed using removallistener?

Is my understanding is correct?

Ben Manes

unread,
Jun 19, 2023, 3:52:22 AM6/19/23
to Jayanth Anish, guava-discuss
Correct. The design assumption was that a cache is a transient data store, not a timing facility, and the data is in-memory anyway as GCs likewise did not collect when idle. There was also a dislike to create threads as expensive, incompatible with JEE/GAE, and not in the spirit of a data structure library. In Caffeine, we provide a scheduler option so that you can provide the thread while we inspect the metadata to schedule the cleanup task.

Jayanth Anish

unread,
Jun 19, 2023, 4:19:10 AM6/19/23
to guava-discuss
Can you provide me any reference material , regarding caffeine and how to use it for getting notified when there is an entry expired.

Ben Manes

unread,
Jun 19, 2023, 4:41:39 AM6/19/23
to Jayanth Anish, guava-discuss
The user guide has an example,
This is basically your existing code except using the Caffeine builder and setting the scheduler option.

Jayanth Anish

unread,
Jun 19, 2023, 4:44:43 AM6/19/23
to guava-discuss
is this cleanup not present in guava map (cachebuilder) itself?
Please do confirm this.

Thanks!

Ben Manes

unread,
Jun 19, 2023, 4:50:53 AM6/19/23
to Jayanth Anish, guava-discuss
Correct. Guava’s cache is in maintenance and the documentation recommends Caffeine, it’s successor.

Reply all
Reply to author
Forward
0 new messages