Skip to content

Commit e0d923c

Browse files
Introduce Cache.putIfNoDuplicate function and unit tests
1 parent 5ec17d7 commit e0d923c

File tree

5 files changed

+61
-179
lines changed

5 files changed

+61
-179
lines changed

components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/cache/LocalClaimCache.java

-40
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,4 @@ private LocalClaimCache() {
3838
public static LocalClaimCache getInstance() {
3939
return instance;
4040
}
41-
42-
/**
43-
* Add a cache entry. Avoid adding entry if the current entry value is the same.
44-
*
45-
* @param key Key which cache entry is indexed.
46-
* @param entry Actual object where cache entry is placed.
47-
* @param tenantId The tenant Id where the cache is maintained.
48-
*/
49-
@Override
50-
public void addToCache(Integer key, ArrayList<LocalClaim> entry, int tenantId) {
51-
52-
if (key == null ) {
53-
return;
54-
}
55-
ArrayList<LocalClaim> currentEntry = getValueFromCache(key, tenantId);
56-
if (currentEntry != null && currentEntry.equals(entry)) {
57-
return;
58-
}
59-
super.addToCache(key, entry, tenantId);
60-
}
61-
62-
/**
63-
* Add a cache entry. Avoid adding entry if the current entry value is the same.
64-
*
65-
* @param key Key which cache entry is indexed.
66-
* @param entry Actual object where cache entry is placed.
67-
* @param tenantDomain The tenant domain where the cache is maintained.
68-
*/
69-
@Override
70-
public void addToCache(Integer key, ArrayList<LocalClaim> entry, String tenantDomain) {
71-
72-
if (key == null ) {
73-
return;
74-
}
75-
ArrayList<LocalClaim> currentEntry = getValueFromCache(key, tenantDomain);
76-
if (currentEntry != null && currentEntry.equals(entry)) {
77-
return;
78-
}
79-
super.addToCache(key, entry, tenantDomain);
80-
}
8141
}

components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/main/java/org/wso2/carbon/identity/claim/metadata/mgt/dao/CacheBackedLocalClaimDAO.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public List<LocalClaim> getLocalClaims(int tenantId) throws ClaimMetadataExcepti
5757
log.debug("Cache miss for local claim list for tenant: " + tenantId);
5858
}
5959
localClaimList = localClaimDAO.getLocalClaims(tenantId);
60-
localClaimInvalidationCache.addToCache(tenantId, new ArrayList<>(localClaimList), tenantId);
60+
localClaimInvalidationCache.addToCacheIfNoDuplicate(tenantId, new ArrayList<>(localClaimList), tenantId);
6161
} else {
6262
if (log.isDebugEnabled()) {
6363
log.debug("Cache hit for local claim list for tenant: " + tenantId);

components/claim-mgt/org.wso2.carbon.identity.claim.metadata.mgt/src/test/java/org/wso2/carbon/identity/claim/metadata/mgt/LocalClaimCacheTest.java

-138
This file was deleted.

components/identity-core/org.wso2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/cache/BaseCache.java

+46
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,52 @@ public void addToCache(K key, V entry, int tenantId) {
182182
}
183183
}
184184

185+
/**
186+
* Add a cache entry if the same value is not already there.
187+
*
188+
* @param key Key which cache entry is indexed.
189+
* @param entry Actual object where cache entry is placed.
190+
*/
191+
public void addToCacheIfNoDuplicate(K key, V entry, String tenantDomain) {
192+
193+
if (!isEnabled()) {
194+
return;
195+
}
196+
197+
try {
198+
startTenantFlow(tenantDomain);
199+
Cache<K, V> cache = getBaseCache();
200+
if (cache != null) {
201+
cache.putIfNoDuplicate(key, entry);
202+
}
203+
} finally {
204+
PrivilegedCarbonContext.endTenantFlow();
205+
}
206+
}
207+
208+
/**
209+
* Add a cache entry if the same value is not already there.
210+
*
211+
* @param key Key which cache entry is indexed.
212+
* @param entry Actual object where cache entry is placed.
213+
*/
214+
public void addToCacheIfNoDuplicate(K key, V entry, int tenantId) {
215+
216+
if (!isEnabled()) {
217+
return;
218+
}
219+
220+
try {
221+
startTenantFlow(tenantId);
222+
Cache<K, V> cache = getBaseCache();
223+
if (cache != null) {
224+
cache.putIfNoDuplicate(key, entry);
225+
}
226+
} finally {
227+
PrivilegedCarbonContext.endTenantFlow();
228+
}
229+
}
230+
185231
/**
186232
* Retrieves a cache entry.
187233
*

components/identity-core/org.wso2.carbon.identity.core/src/test/java/org/wso2/carbon/identity/core/cache/BaseCacheTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ public void testAddition() {
9090
assertNull(entry);
9191
}
9292

93+
@Test
94+
public void testAdditionWithoutDuplicate() {
95+
96+
TestCache.getInstance().addToCacheIfNoDuplicate(new TestCacheKey("test"), new TestCacheEntry("value"), 1);
97+
98+
TestCacheEntry entry = TestCache.getInstance().getValueFromCache(new TestCacheKey("test"), 1);
99+
assertEquals(entry.getValue(), "value");
100+
entry = TestCache.getInstance().getValueFromCache(new TestCacheKey("test"), "foo.com");
101+
assertEquals(entry.getValue(), "value");
102+
103+
entry = TestCache.getInstance().getValueFromCache(new TestCacheKey("test"), "bar.com");
104+
assertNull(entry);
105+
}
106+
93107
@Test
94108
public void testUpdate() {
95109

0 commit comments

Comments
 (0)