From 631b9894ca6d36aa825ac05262444ed37960b750 Mon Sep 17 00:00:00 2001 From: Inthirakumaaran Date: Tue, 4 Jun 2024 10:12:28 +0530 Subject: [PATCH] Revert "Revert "Add support for periodic cache clean up."" --- .../wso2/carbon/caching/impl/CacheImpl.java | 7 ++++++- .../carbon/caching/impl/CachingConstants.java | 1 + .../org/wso2/carbon/caching/impl/Util.java | 18 ++++++++++++++++++ .../templates/repository/conf/carbon.xml.j2 | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CacheImpl.java b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CacheImpl.java index 251f013c8b6..dc67f9e0cac 100644 --- a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CacheImpl.java +++ b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CacheImpl.java @@ -69,6 +69,8 @@ import static org.wso2.carbon.caching.impl.CachingConstants.CLEAR_ALL_PREFIX; import static org.wso2.carbon.caching.impl.CachingConstants.ILLEGAL_STATE_EXCEPTION_MESSAGE; +import static org.wso2.carbon.caching.impl.Util.isPeriodicCacheCleanUpEnabled; + /** * TODO: class description *

@@ -1000,7 +1002,10 @@ public int compare(CacheEntry o1, CacheEntry o2) { long lastModified = localCacheEntry.getLastModified(); long now = System.currentTimeMillis(); - if (now - lastAccessed >= accessedExpiryDuration || now - lastModified >= modifiedExpiryDuration) { + boolean performCleanup = isPeriodicCacheCleanUpEnabled() ? (now - lastAccessed >= accessedExpiryDuration) : + (now - lastAccessed >= accessedExpiryDuration || now - lastModified >= modifiedExpiryDuration); + + if (performCleanup) { expire(key); if (log.isDebugEnabled()) { log.debug("Expired: Cache:" + cacheName + ", entry:" + key); diff --git a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CachingConstants.java b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CachingConstants.java index 5feb6d6ac21..68b2bae492b 100644 --- a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CachingConstants.java +++ b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/CachingConstants.java @@ -48,6 +48,7 @@ public final class CachingConstants { public static final String ILLEGAL_STATE_EXCEPTION_MESSAGE = "The cache status is not STARTED"; public static final String CACHE_INVALIDATION_IMPL = "Cache.CacheInvalidationImpl"; public static final String CACHE_INVALIDATION_PROPAGATION_ENABLED = "Cache.CachePropagationEnabled"; + public static final String PERIODIC_CACHE_CLEANUP_ENABLED = "Cache.CachePeriodicCleanupEnabled"; public static final String DEFAULT_CACHE_INVALIDATION_CLASS = "org.wso2.carbon.caching.impl.clustering" + ".ClusterCacheInvalidationRequestSender"; diff --git a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/Util.java b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/Util.java index 7bc3d5b7f09..bfa2b4cba5e 100644 --- a/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/Util.java +++ b/core/javax.cache/src/main/java/org/wso2/carbon/caching/impl/Util.java @@ -170,6 +170,24 @@ public static boolean isCacheInvalidationPropagationEnabled() { return false; } + /** + * Return whether the cache periodic cleanup enabled or not. + * + * @return boolean. + */ + public static boolean isPeriodicCacheCleanUpEnabled() { + + ServerConfigurationService serverConfigService = DataHolder.getInstance().getServerConfigurationService(); + if (serverConfigService != null) { + String cacheCleanUp = serverConfigService.getFirstProperty( + CachingConstants.PERIODIC_CACHE_CLEANUP_ENABLED); + if (StringUtils.isNotEmpty(cacheCleanUp)) { + return Boolean.parseBoolean(cacheCleanUp.trim()); + } + } + return false; + } + private Util() { } } diff --git a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 index 2ce9c215301..9e67e798e73 100644 --- a/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 +++ b/distribution/kernel/carbon-home/repository/resources/conf/templates/repository/conf/carbon.xml.j2 @@ -324,7 +324,12 @@ {% else %} org.wso2.carbon.caching.impl.clustering.ClusterCacheInvalidationRequestSender {% endif %} + <{% if server.cache.propagation_enabled is defined %} {{server.cache.propagation_enabled}} + {% endif %} + {% if server.cache.periodic_cleanup_enabled is defined %} + {{server.cache.periodic_cleanup_enabled}} + {% endif %}