Skip to content

Commit

Permalink
Merge pull request apex-enterprise-patterns#37 from OrtooApps/feature…
Browse files Browse the repository at this point in the history
…/slight-improvement-to-session-cache

Added removeAll to session cache
  • Loading branch information
rob-baillie-ortoo committed Apr 26, 2022
2 parents 4683f0b + efc4672 commit a6c4484
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ public inherited sharing class SessionCache implements ICacheAdaptor
Cache.Session.remove( key );
}

/**
* Instructs the cache to remove all objects from the cache.
*
* Is useful for clearing the cache out completely when the majority of entries would otherwise be dirty.
*/
public void removeAll()
{
for ( String thisKey : Cache.Session.getKeys() )
{
Cache.Session.remove( thisKey );
}
}

private String createFullyQualifiedPartitionName()
{
return Cache.SessionPartition.createFullyQualifiedPartition( PackageUtils.NAMESPACE_PREFIX, PARTITION_NAME );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,19 @@ private without sharing class SessionCacheTest
System.assertEquals( false, new SessionCache().contains( 'keythatneverexisted' ), 'remove, when called for a key that was put, will remove it - checking contains' );
System.assertEquals( null, new SessionCache().get( 'keythatneverexisted' ), 'remove, when called for a key that was put, will remove it - checking get' );
}

@isTest
private static void removeAll_whenCalled_removesEverythingFromTheCache() // NOPMD: Test method name format
{
new SessionCache().put( 'key1', '1', DEFAULT_LIFESPAN );
new SessionCache().put( 'key2', '2', DEFAULT_LIFESPAN );
new SessionCache().put( 'key3', '3', DEFAULT_LIFESPAN );

SessionCache cache = new SessionCache();
cache.removeAll();

System.assertEquals( false, new SessionCache().contains( 'key1' ), 'remove, when called for a key that was put, will remove it - checking contains' );
System.assertEquals( false, new SessionCache().contains( 'key2' ), 'remove, when called for a key that was put, will remove it - checking contains' );
System.assertEquals( false, new SessionCache().contains( 'key3' ), 'remove, when called for a key that was put, will remove it - checking contains' );
}
}

0 comments on commit a6c4484

Please sign in to comment.