Skip to content

Commit 8f56426

Browse files
committed
refactoring
Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent e3a5c0c commit 8f56426

File tree

2 files changed

+28
-42
lines changed

2 files changed

+28
-42
lines changed

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

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
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-
1817
public interface Selector {
1918
Cacheable query();
2019
}
@@ -68,28 +67,19 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
6867
}
6968

7069
List<CacheManager.Cacheable> caches = new List<CacheManager.Cacheable>{
71-
CacheManager.SOQLOrgCache,
72-
CacheManager.SOQLSessionCache,
73-
CacheManager.ApexTransaction
70+
CacheManager.SOQLOrgCache,
71+
CacheManager.SOQLSessionCache,
72+
CacheManager.ApexTransaction
7473
};
7574

76-
Set<Id> recordIdsToBeRemovedFromCache = new Map<Id, SObject>(records).keySet();
7775
String key = records[0].getSObjectType().toString();
7876

7977
for (CacheManager.Cacheable cache : caches) {
8078
if (!cache.contains(key)) {
8179
break;
8280
}
8381

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);
82+
cache.put(key, new ListFilter().apply((List<CacheItem>) cache.get(key), records));
9383
}
9484
}
9585

@@ -328,17 +318,17 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
328318
}
329319

330320
public void save(List<SObject> databaseRecords) {
331-
List<CacheItem> newObjectCachedRecords = new List<CacheItem>();
321+
List<CacheItem> newCacheRecords = new List<CacheItem>();
332322

333323
if (this.isRecordMissingFromCache()) {
334-
newObjectCachedRecords = new InsertAction().get(storage.getCachedRecords(), databaseRecords);
324+
newCacheRecords = new ListConcatenator().apply(storage.getCachedRecords(), databaseRecords);
335325
} else if (databaseRecords.isEmpty()) { // record does not exist in database anymore
336-
newObjectCachedRecords = new DeleteAction().get(storage.getCachedRecords(), this.toList());
326+
newCacheRecords = new ListFilter().apply(storage.getCachedRecords(), this.toList());
337327
} else if (this.areRequestedFieldsMissing() || this.areRecordsOutdated()) {
338-
newObjectCachedRecords = new UpdateAction().get(storage.getCachedRecords(), databaseRecords);
328+
newCacheRecords = new ListMapper().apply(storage.getCachedRecords(), databaseRecords);
339329
}
340330

341-
storage.putRecordsToCache(newObjectCachedRecords);
331+
storage.putRecordsToCache(newCacheRecords);
342332
}
343333

344334
public Boolean isRecordMissingFromCache() {
@@ -354,12 +344,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
354344
}
355345
}
356346

357-
private interface CacheAction {
358-
List<CacheItem> get(List<CacheItem> allRecordsCachedForObject, List<SObject> records);
347+
private interface ListOperation {
348+
List<CacheItem> apply(List<CacheItem> allRecordsCachedForObject, List<SObject> records);
359349
}
360350

361-
private class InsertAction implements CacheAction {
362-
public List<CacheItem> get(List<CacheItem> allRecordsCachedForObject, List<SObject> records) {
351+
private class ListConcatenator implements ListOperation {
352+
public List<CacheItem> apply(List<CacheItem> allRecordsCachedForObject, List<SObject> records) {
363353
for (SObject databaseRecord : records) {
364354
allRecordsCachedForObject.add(new CacheItem(databaseRecord));
365355
}
@@ -368,8 +358,8 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
368358
}
369359
}
370360

371-
private class UpdateAction implements CacheAction {
372-
public List<CacheItem> get(List<CacheItem> allRecordsCachedForObject, List<SObject> records) {
361+
private class ListMapper implements ListOperation {
362+
public List<CacheItem> apply(List<CacheItem> allRecordsCachedForObject, List<SObject> records) {
373363
for (SObject updatedRecord : records) {
374364
for (CacheItem cachedRecord : allRecordsCachedForObject) {
375365
if (updatedRecord.Id == cachedRecord.id) {
@@ -383,13 +373,13 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
383373
}
384374
}
385375

386-
private class DeleteAction implements CacheAction {
387-
public List<CacheItem> get(List<CacheItem> allRecordsCachedForObject, List<SObject> records) {
388-
Set<Id> recordsToRemoveFromCacheIds = new Map<Id, SObject>(records).keySet();
376+
private class ListFilter implements ListOperation {
377+
public List<CacheItem> apply(List<CacheItem> allRecordsCachedForObject, List<SObject> recordsToRemove) {
378+
Set<Id> recordsToRemoveIds = new Map<Id, SObject>(recordsToRemove).keySet();
389379
List<CacheItem> filteredCachedItems = new List<CacheItem>();
390380

391381
for (CacheItem cachedRecord : allRecordsCachedForObject) {
392-
if (!recordsToRemoveFromCacheIds.contains(cachedRecord.id)) {
382+
if (!recordsToRemoveIds.contains(cachedRecord.id)) {
393383
filteredCachedItems.add(cachedRecord);
394384
}
395385
}

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -563,25 +563,21 @@ private class SOQLCache_Test {
563563
static void recordsClearedFromCache() {
564564
// Setup
565565
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');
566+
.with(Profile.Id, Profile.Name)
567+
.whereEqual(Profile.Name, 'System Administrator')
568+
.cacheInOrgCache()
569+
.toObject();
573570

574-
//Verify initial setup
571+
// Verify initial setup
575572
Assert.isTrue(CacheManager.SOQLOrgCache.contains('Profile'), 'Key should exist.');
576-
Assert.isFalse(cacheItems.isEmpty(), 'Cache item should be present');
573+
Assert.isFalse(((List<SOQLCache.CacheItem>) CacheManager.SOQLOrgCache.get('Profile')).isEmpty(), 'Cache item should be present.');
577574

578-
//Test
579-
SOQLCache.invalidateRecords(new List<Profile>{profile});
575+
// Test
576+
SOQLCache.invalidateRecords(new List<Profile>{ profile });
580577

581578
// Verify
582579
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');
580+
Assert.isTrue(((List<SOQLCache.CacheItem>) CacheManager.SOQLOrgCache.get('Profile')).isEmpty(), 'Cache items should be empty.');
585581
}
586582

587583
static User minimumAccessUser() {

0 commit comments

Comments
 (0)