1414**/
1515@SuppressWarnings(' PMD.ExcessivePublicCount, PMD.ExcessiveClassLength, PMD.CyclomaticComplexity, PMD.CognitiveComplexity, PMD.PropertyNamingConventions, PMD.FieldDeclarationsShouldBeAtStart, PMD.ApexDoc, PMD.ExcessiveParameterList' )
1616public 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 }
0 commit comments