@@ -24,7 +24,7 @@ private class SOQLCache_Test {
2424
2525 // Test
2626 new SOQL_ProfileCache ().whereEqual (' Name' , ' System Administrator' ).toObject (); // initial query will be executed
27- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
27+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
2828
2929 // Verify
3030 Assert .areEqual (mockedProfiles .size (), cachedProfiles .size (), ' The cached profiles should be identical to those in the initial query.' );
@@ -34,7 +34,7 @@ private class SOQLCache_Test {
3434 static void noInitialQuery () {
3535 // Test
3636 new SOQL_ProfileCacheDefault ().whereEqual (' Name' , ' System Administrator' ).toObject ();
37- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
37+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
3838 Profile cachedProfile = (Profile ) cachedProfiles [0 ].record ;
3939
4040 // Verify
@@ -75,7 +75,7 @@ private class SOQLCache_Test {
7575 .whereEqual (Profile .Name , ' System Administrator' )
7676 .toObject ();
7777
78- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
78+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
7979 Profile cachedProfile = (Profile ) cachedProfiles [0 ].record ;
8080
8181 // Verify
@@ -93,7 +93,7 @@ private class SOQLCache_Test {
9393 .whereEqual (Profile .Name , ' System Administrator' )
9494 .toObject ();
9595
96- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' ProfileTest ' );
96+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' Profile ' );
9797 Profile cachedProfile = (Profile ) cachedProfiles [0 ].record ;
9898
9999 // Verify
@@ -111,7 +111,7 @@ private class SOQLCache_Test {
111111 .whereEqual (Profile .Name , ' System Administrator' )
112112 .toObject ();
113113
114- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .SOQLSessionCache .get (' ProfileTest ' );
114+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .SOQLSessionCache .get (' Profile ' );
115115 Profile cachedProfile = (Profile ) cachedProfiles [0 ].record ;
116116
117117 // Verify
@@ -127,7 +127,7 @@ private class SOQLCache_Test {
127127 SELECT Id , Name FROM Profile WHERE Name = ' System Administrator' LIMIT 1
128128 ]);
129129
130- CacheManager .ApexTransaction .put (' ProfileTest ' , new List <SOQLCache .CacheItem >{ cachedItem });
130+ CacheManager .ApexTransaction .put (' Profile ' , new List <SOQLCache .CacheItem >{ cachedItem });
131131
132132 // Test
133133 Profile profile = (Profile ) SOQLCache .of (Profile .SObjectType )
@@ -150,7 +150,7 @@ private class SOQLCache_Test {
150150 ]);
151151 cachedItem .cachedDate = DateTime .now ().addHours (- 6 );
152152
153- CacheManager .ApexTransaction .put (' ProfileTest ' , new List <SOQLCache .CacheItem >{ cachedItem });
153+ CacheManager .ApexTransaction .put (' Profile ' , new List <SOQLCache .CacheItem >{ cachedItem });
154154
155155 // Test
156156 Profile profile = (Profile ) SOQLCache .of (Profile .SObjectType )
@@ -159,12 +159,12 @@ private class SOQLCache_Test {
159159 .maxHoursWithoutRefresh (3 )
160160 .toObject ();
161161
162- List <SOQLCache .CacheItem > updatedCacheItems = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
162+ List <SOQLCache .CacheItem > updatedCacheItems = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
163163
164164 // Verify
165165 Assert .areEqual (2 , Limits .getQueries (), ' Two queries should be issued. The second query should update cached record.' );
166166 Assert .areEqual (1 , updatedCacheItems .size (), ' The Apex transaction cache should contain exactly one record.' );
167- Assert .areEqual ( System .now (), updatedCacheItems [0 ].cachedDate , ' The cached record should be updated.' );
167+ Assert .isTrue ( Math . abs (( System .now (). getTime () - updatedCacheItems [0 ].cachedDate . getTime ()) / 1000 ) < 10 , ' The cached record should be updated. The time difference should be less than 10 seconds .' );
168168 Assert .isNotNull (profile , ' Profile should be not null.' );
169169 Assert .areEqual (' System Administrator' , profile .Name , ' The cached profile record should be "System Administrator".' );
170170 }
@@ -176,7 +176,7 @@ private class SOQLCache_Test {
176176 cachedItem1 .cachedDate = DateTime .now ().addHours (- 6 );
177177 SOQLCache .CacheItem cachedItem2 = new SOQLCache .CacheItem (new Profile (Id = ' 00e3V000000Nme3QAD' , Name = ' System Administrator' ));
178178
179- CacheManager .ApexTransaction .put (' ProfileTest ' , new List <SOQLCache .CacheItem >{ cachedItem1 , cachedItem2 });
179+ CacheManager .ApexTransaction .put (' Profile ' , new List <SOQLCache .CacheItem >{ cachedItem1 , cachedItem2 });
180180
181181 // Test
182182 Profile profile = (Profile ) SOQLCache .of (Profile .SObjectType )
@@ -185,7 +185,7 @@ private class SOQLCache_Test {
185185 .maxHoursWithoutRefresh (3 )
186186 .toObject ();
187187
188- List <SOQLCache .CacheItem > updatedCacheItems = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
188+ List <SOQLCache .CacheItem > updatedCacheItems = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
189189 Profile cachedProfile = (Profile ) updatedCacheItems [0 ].record ;
190190
191191 // Verify
@@ -353,7 +353,7 @@ private class SOQLCache_Test {
353353 @IsTest
354354 static void stripInaccessible () {
355355 // Setup
356- Task testTask = new Task (Subject = ' Test' , Type = ' Other' );
356+ Task testTask = new Task (Subject = ' Test Task ' , Type = ' Other' );
357357 insert testTask ;
358358
359359 System .runAs (minimumAccessUser ()) {
@@ -545,7 +545,7 @@ private class SOQLCache_Test {
545545 .whereEqual (Profile .Name , ' System Administrator' )
546546 .toObject ();
547547
548- List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' ProfileTest ' );
548+ List <SOQLCache .CacheItem > cachedProfiles = (List <SOQLCache .CacheItem >) CacheManager .ApexTransaction .get (' Profile ' );
549549
550550 // Verify
551551 Assert .areEqual (2 , Limits .getQueries (), ' Two queries should be issued. The second query should retrieve the missing fields.' );
@@ -559,6 +559,48 @@ 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+ .toObject ();
570+
571+ // Verify initial setup
572+ Assert .isTrue (CacheManager .SOQLOrgCache .contains (' Profile' ), ' Key should exist.' );
573+ Assert .isFalse (((List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' Profile' )).isEmpty (), ' Cache item should be present.' );
574+
575+ // Test
576+ SOQLCache .removeFromCache (new List <Profile >{ profile });
577+
578+ // Verify
579+ Assert .isTrue (CacheManager .SOQLOrgCache .contains (' Profile' ), ' Key should still exist.' );
580+ Assert .isTrue (((List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' Profile' )).isEmpty (), ' Cache items should be empty.' );
581+ }
582+
583+ @IsTest
584+ static void emptyRecordsClearedFromCache () {
585+ // Setup
586+ Profile profile = (Profile ) SOQLCache .of (Profile .SObjectType )
587+ .with (Profile .Id , Profile .Name )
588+ .whereEqual (Profile .Name , ' System Administrator' )
589+ .cacheInOrgCache ()
590+ .toObject ();
591+
592+ // Verify initial setup
593+ Assert .isTrue (CacheManager .SOQLOrgCache .contains (' Profile' ), ' Key should exist.' );
594+ Assert .isFalse (((List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' Profile' )).isEmpty (), ' Cache item should be present.' );
595+
596+ // Test
597+ SOQLCache .removeFromCache (new List <Profile >());
598+
599+ // Verify
600+ Assert .isTrue (CacheManager .SOQLOrgCache .contains (' Profile' ), ' Key should still exist.' );
601+ Assert .isFalse (((List <SOQLCache .CacheItem >) CacheManager .SOQLOrgCache .get (' Profile' )).isEmpty (), ' Cache items should be not empty.' );
602+ }
603+
562604 static User minimumAccessUser () {
563605 return new User (
564606 Alias = ' newUser' ,
0 commit comments