@@ -66,21 +66,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
6666 return ;
6767 }
6868
69- List <CacheManager .Cacheable > caches = new List <CacheManager .Cacheable >{
70- CacheManager .SOQLOrgCache ,
71- CacheManager .SOQLSessionCache ,
72- CacheManager .ApexTransaction
73- };
69+ String ofObject = records [0 ].getSObjectType ().toString ();
7470
75- String key = records [0 ].getSObjectType ().toString ();
76-
77- for (CacheManager .Cacheable cache : caches ) {
78- if (! cache .contains (key )) {
79- break ;
80- }
81-
82- cache .put (key , new ListFilter ().apply ((List <CacheItem >) cache .get (key ), records ));
83- }
71+ // Record deletion will trigger an automatic cache refresh when the query is executed.
72+ new CacheStorageProxy (ofObject ).apexTransaction ().removeRecordsFromCache (records );
73+ new CacheStorageProxy (ofObject ).orgCache ().removeRecordsFromCache (records );
74+ new CacheStorageProxy (ofObject ).sessionCache ().removeRecordsFromCache (records );
8475 }
8576
8677 public static Cacheable of (SObjectType ofObject ) {
@@ -116,17 +107,17 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
116107 }
117108
118109 public Cacheable cacheInApexTransaction () {
119- this .cache .storage .cacheInApexTransaction ();
110+ this .cache .storage .apexTransaction ();
120111 return this ;
121112 }
122113
123114 public Cacheable cacheInOrgCache () {
124- this .cache .storage .cacheInOrgCache ();
115+ this .cache .storage .orgCache ();
125116 return this ;
126117 }
127118
128119 public Cacheable cacheInSessionCache () {
129- this .cache .storage .cacheInSessionCache ();
120+ this .cache .storage .sessionCache ();
130121 return this ;
131122 }
132123
@@ -245,16 +236,19 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
245236 this .ofObject = ofObject ;
246237 }
247238
248- public void cacheInApexTransaction () {
239+ public CacheStorageProxy apexTransaction () {
249240 this .storage = CacheManager .ApexTransaction ;
241+ return this ;
250242 }
251243
252- public void cacheInOrgCache () {
244+ public CacheStorageProxy orgCache () {
253245 this .storage = CacheManager .SOQLOrgCache ;
246+ return this ;
254247 }
255248
256- public void cacheInSessionCache () {
249+ public CacheStorageProxy sessionCache () {
257250 this .storage = CacheManager .SOQLSessionCache ;
251+ return this ;
258252 }
259253
260254 public void putInitialRecordsToCache (List <SObject > records ) {
@@ -268,19 +262,54 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
268262 }
269263
270264 public void putRecordsToCache (List <CacheItem > records ) {
271- this .storage .put (this .key () , records );
265+ this .storage .put (this .ofObject , records );
272266 }
273267
274268 public Boolean hasCachedRecords () {
275- return this .storage .contains (this .key () );
269+ return this .storage .contains (this .ofObject );
276270 }
277271
278272 public List <CacheItem > getCachedRecords () {
279- return (List <CacheItem >) (this .storage .get (this .key ()) ?? new List <CacheItem >());
273+ return (List <CacheItem >) (this .storage .get (this .ofObject ) ?? new List <CacheItem >());
274+ }
275+
276+ public void addRecordsToCache (List <SObject > recordsToAdd ) {
277+ List <CacheItem > allCachedRecords = getCachedRecords ();
278+
279+ for (SObject databaseRecord : recordsToAdd ) {
280+ allCachedRecords .add (new CacheItem (databaseRecord ));
281+ }
282+
283+ putRecordsToCache (allCachedRecords );
284+ }
285+
286+ public void updateRecordsInCache (List <SObject > recordsToUpdate ) {
287+ List <CacheItem > allCachedRecords = getCachedRecords ();
288+
289+ for (SObject updatedRecord : recordsToUpdate ) {
290+ for (CacheItem cachedRecord : allCachedRecords ) {
291+ if (updatedRecord .Id == cachedRecord .id ) {
292+ cachedRecord .record = updatedRecord ;
293+ cachedRecord .cachedDate = System .now ();
294+ }
295+ }
296+ }
297+
298+ putRecordsToCache (allCachedRecords );
280299 }
281300
282- private String key () {
283- return this .ofObject ;
301+ public void removeRecordsFromCache (List <SObject > recordsToRemove ) {
302+ Set <Id > recordsToRemoveIds = new Map <Id , SObject >(recordsToRemove ).keySet ();
303+ List <CacheItem > allCachedRecords = getCachedRecords ();
304+ List <CacheItem > filteredCachedItems = new List <CacheItem >();
305+
306+ for (CacheItem cachedRecord : allCachedRecords ) {
307+ if (! recordsToRemoveIds .contains (cachedRecord .id )) {
308+ filteredCachedItems .add (cachedRecord );
309+ }
310+ }
311+
312+ putRecordsToCache (filteredCachedItems );
284313 }
285314 }
286315
@@ -321,14 +350,12 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
321350 List <CacheItem > newCacheRecords = new List <CacheItem >();
322351
323352 if (this .isRecordMissingFromCache ()) {
324- newCacheRecords = new ListConcatenator (). apply ( storage .getCachedRecords (), databaseRecords );
353+ storage .addRecordsToCache ( databaseRecords );
325354 } else if (databaseRecords .isEmpty ()) { // record does not exist in database anymore
326- newCacheRecords = new ListFilter (). apply ( storage .getCachedRecords (), this .toList ());
355+ storage .removeRecordsFromCache ( this .toList ());
327356 } else if (this .areRequestedFieldsMissing () || this .areRecordsOutdated ()) {
328- newCacheRecords = new ListMapper (). apply ( storage .getCachedRecords (), databaseRecords );
357+ storage .updateRecordsInCache ( databaseRecords );
329358 }
330-
331- storage .putRecordsToCache (newCacheRecords );
332359 }
333360
334361 public Boolean isRecordMissingFromCache () {
@@ -344,50 +371,6 @@ public virtual inherited sharing class SOQLCache implements Cacheable {
344371 }
345372 }
346373
347- private interface ListOperation {
348- List <CacheItem > apply (List <CacheItem > allRecordsCachedForObject , List <SObject > records );
349- }
350-
351- private class ListConcatenator implements ListOperation {
352- public List <CacheItem > apply (List <CacheItem > allRecordsCachedForObject , List <SObject > records ) {
353- for (SObject databaseRecord : records ) {
354- allRecordsCachedForObject .add (new CacheItem (databaseRecord ));
355- }
356-
357- return allRecordsCachedForObject ;
358- }
359- }
360-
361- private class ListMapper implements ListOperation {
362- public List <CacheItem > apply (List <CacheItem > allRecordsCachedForObject , List <SObject > records ) {
363- for (SObject updatedRecord : records ) {
364- for (CacheItem cachedRecord : allRecordsCachedForObject ) {
365- if (updatedRecord .Id == cachedRecord .id ) {
366- cachedRecord .record = updatedRecord ;
367- cachedRecord .cachedDate = System .now ();
368- }
369- }
370- }
371-
372- return allRecordsCachedForObject ;
373- }
374- }
375-
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 ();
379- List <CacheItem > filteredCachedItems = new List <CacheItem >();
380-
381- for (CacheItem cachedRecord : allRecordsCachedForObject ) {
382- if (! recordsToRemoveIds .contains (cachedRecord .id )) {
383- filteredCachedItems .add (cachedRecord );
384- }
385- }
386-
387- return filteredCachedItems ;
388- }
389- }
390-
391374 @TestVisible
392375 private class CacheItem {
393376 public Id id ;
0 commit comments