Implement expire after access policy for cache

Description

As it is, for non-distributed cache with expire time set, all the cached entries are remove after the expire time regardless of whether the entries is sitting idle or have been accessed recently.

This ticket intends to improve on that by replacing non-distributed cache with https://github.com/ben-manes/caffeine/ and using the expire after access (read/write/update) support provided by this library.

For cache that doesn’t set maximum size and expire time, we should continue to use a simple ConcurrentHashMap.

Environment

None

Activity

Carlos Ruiz 
April 26, 2024 at 2:01 PM

answered:

By right, key should never be null. Anyway, it is harmless to add that to CCache.get

Carlos Ruiz 
April 26, 2024 at 2:00 PM

Hi @Heng Sin Low

I think is because of this ticket that REST is throwing this error when executing the endpoint /api/v1/forms without parameters:
13:42:45.084===========> FormResourceImpl.getForms: Cannot invoke "Object.hashCode()" because "key" is null [182]

java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "key" is null

at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936)

at com.github.benmanes.caffeine.cache.BoundedLocalCache.getIfPresent(BoundedLocalCache.java:2179)

at com.github.benmanes.caffeine.cache.BoundedLocalCache.get(BoundedLocalCache.java:2174)

at org.compiere.util.CCache.get(CCache.java:400)

at com.trekglobal.idempiere.rest.api.json.filter.DefaultQueryConverter.convertStatement(DefaultQueryConverter.java:55)

at com.trekglobal.idempiere.rest.api.v1.resource.impl.FormResourceImpl.getForms(FormResourceImpl.java:68)

Line 55 on REST is:
ConvertedQuery cache = convertCache.get(queryStatement);

and queryStatement is null, the fix in REST is easy, but as is not failing in release-11 I think the fix maybe must be on CCache.get to return null when the key is null?

WDYT?

Heng Sin Low 
April 1, 2024 at 9:09 AM

Yes, will be clear as well.

Carlos Ruiz 
April 1, 2024 at 8:42 AM

Hi - about this:

For cache that doesn’t set maximum size and expire time, we should continue to use a simple ConcurrentHashMap.

are those ConcurrentHashMap also cleared with Cache Reset?

Fixed

Details

Assignee

Reporter

Components

Fix versions

Priority

Created April 1, 2024 at 8:36 AM
Updated June 1, 2024 at 3:18 PM
Resolved April 26, 2024 at 3:06 PM