From efc4672eb9f4bd534a141918b21bcecc23342b44 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Mon, 25 Apr 2022 14:47:38 +0100 Subject: [PATCH] Added removeAll to session cache --- .../fflib-extension/caching/SessionCache.cls | 13 +++++++++++++ .../caching/tests/SessionCacheTest.cls | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls index 1abb8b6873a..38b4f7f7cfe 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/SessionCache.cls @@ -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 ); diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls index 8b89febd68e..15ede758276 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/SessionCacheTest.cls @@ -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' ); + } } \ No newline at end of file