Skip to content

Commit e3a5c0c

Browse files
authored
Implemented invalidateRecords() + Unit tests (#143)
* Implemented invalidateRecords() + tests * Changed the way how entries are removed from cache * Changed the way how sobject type is deferred to assume there is collection of single type passed in. Refactoring. * Changed key in unit tests + removed static string key * Changed unit test object to profile
1 parent 1deffb4 commit e3a5c0c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

force-app/main/default/classes/main/cached-soql/SOQLCache.cls

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
**/
1515
@SuppressWarnings('PMD.ExcessivePublicCount, PMD.ExcessiveClassLength, PMD.CyclomaticComplexity, PMD.CognitiveComplexity, PMD.PropertyNamingConventions, PMD.FieldDeclarationsShouldBeAtStart, PMD.ApexDoc, PMD.ExcessiveParameterList')
1616
public virtual inherited sharing class SOQLCache implements Cacheable {
17+
1718
public interface Selector {
1819
Cacheable query();
1920
}
@@ -61,6 +62,37 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
6162
mock.setMock(mockId, new List<SObject>{ record });
6263
}
6364

65+
public static void invalidateRecords(List<SObject> records) {
66+
if (records.isEmpty()) {
67+
return;
68+
}
69+
70+
List<CacheManager.Cacheable> caches = new List<CacheManager.Cacheable>{
71+
CacheManager.SOQLOrgCache,
72+
CacheManager.SOQLSessionCache,
73+
CacheManager.ApexTransaction
74+
};
75+
76+
Set<Id> recordIdsToBeRemovedFromCache = new Map<Id, SObject>(records).keySet();
77+
String key = records[0].getSObjectType().toString();
78+
79+
for (CacheManager.Cacheable cache : caches) {
80+
if (!cache.contains(key)) {
81+
break;
82+
}
83+
84+
List<CacheItem> clearedCacheItems = new List<CacheItem>();
85+
86+
for (CacheItem cacheItem : (List<CacheItem>) cache.get(key)) {
87+
if (!recordIdsToBeRemovedFromCache.contains(cacheItem.id)) {
88+
clearedCacheItems.add(cacheItem);
89+
}
90+
}
91+
92+
cache.put(key, clearedCacheItems);
93+
}
94+
}
95+
6496
public static Cacheable of(SObjectType ofObject) {
6597
return new SOQLCache(ofObject);
6698
}

force-app/main/default/classes/main/cached-soql/SOQLCache_Test.cls

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,31 @@ private class SOQLCache_Test {
559559
Assert.isTrue(profile2.isSet('UserLicenseId'), 'The profile UserLicenseId should not be set.');
560560
}
561561

562+
@IsTest
563+
static void recordsClearedFromCache() {
564+
// Setup
565+
Profile profile = (Profile) SOQLCache.of(Profile.SObjectType)
566+
.with(Profile.Id, Profile.Name)
567+
.whereEqual(Profile.Name, 'System Administrator')
568+
.cacheInOrgCache()
569+
.maxHoursWithoutRefresh(5)
570+
.toObject();
571+
572+
List<SOQLCache.CacheItem> cacheItems = (List<SOQLCache.CacheItem>) CacheManager.SOQLOrgCache.get('Profile');
573+
574+
//Verify initial setup
575+
Assert.isTrue(CacheManager.SOQLOrgCache.contains('Profile'), 'Key should exist.');
576+
Assert.isFalse(cacheItems.isEmpty(), 'Cache item should be present');
577+
578+
//Test
579+
SOQLCache.invalidateRecords(new List<Profile>{profile});
580+
581+
// Verify
582+
Assert.isTrue(CacheManager.SOQLOrgCache.contains('Profile'), 'Key should still exist.');
583+
cacheItems = (List<SOQLCache.CacheItem>) CacheManager.SOQLOrgCache.get('Profile');
584+
Assert.isTrue(cacheItems.isEmpty(), 'Cache items should be empty');
585+
}
586+
562587
static User minimumAccessUser() {
563588
return new User(
564589
Alias = 'newUser',

0 commit comments

Comments
 (0)