Skip to content

Commit

Permalink
Merge pull request #2056 from LahiruLS/4.4.x
Browse files Browse the repository at this point in the history
Fix the cache update issue when ForceLocalCache property is enabled
  • Loading branch information
madurangasiriwardena committed May 28, 2019
2 parents 00365a6 + 1d78c7d commit 189dcb6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.caching.impl.CachingConstants;

import javax.cache.Cache;
import javax.cache.CacheManager;
Expand All @@ -31,6 +33,10 @@ public class ClaimInvalidationCache {
private String INVALIDATE_CACHE_KEY = "Invalidate.Cache.Key";

private int myHashCode;
/**
* This boolean is used to flag the cache, once the claims are loaded to the cache.
*/
private boolean isAlreadyInitialize;

private ClaimInvalidationCache() {
}
Expand Down Expand Up @@ -64,6 +70,12 @@ public boolean isInvalid() {
return true;
}
}
if (ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE) != null &&
ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE).equals("true") &&
hashCode == null && this.isAlreadyInitialize) {
updateCache(INVALIDATE_CACHE_KEY, myHashCode);
return true;
}
return false;
}

Expand Down Expand Up @@ -116,6 +128,25 @@ private void clearCacheEntry(String key) {
cache.remove(key);
}
}

public void setIsAlreadyInitialize(Boolean bool) {
this.isAlreadyInitialize = bool;
}

/**
* Update the cache entry without clearing.
*
* @param key Key which cache entry is indexed.
* @param entry Actual object where cache entry is placed.
*/
private void updateCache(String key, Integer entry) {
Cache<String, Integer> cache = getClaimCache();
if (cache != null) {
cache.put(key, entry);
} else {
log.debug("Error while updating the claim cache. ClaimCache is null");
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.wso2.carbon.user.core.claim;

import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.caching.impl.CachingConstants;
import org.wso2.carbon.user.core.UserStoreException;
import org.wso2.carbon.user.core.claim.builder.ClaimBuilder;
import org.wso2.carbon.user.core.claim.builder.ClaimBuilderException;
Expand Down Expand Up @@ -67,6 +69,10 @@ public DefaultClaimManager(Map<String, ClaimMapping> claimMapping, DataSource da
this.claimMapping = new ConcurrentHashMap<String, ClaimMapping>();
this.claimMapping.putAll(claimMapping);
this.claimCache.invalidateCache();
if (ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE) != null &&
ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE).equals("true")) {
this.claimCache.setIsAlreadyInitialize(true);
}
}
}

Expand Down

0 comments on commit 189dcb6

Please sign in to comment.