From 678acfd654416c3785750535c466c9ad2db6e416 Mon Sep 17 00:00:00 2001 From: Robert Baillie Date: Mon, 14 Mar 2022 11:29:21 +0000 Subject: [PATCH] Added single object puts --- .../fflib-extension/caching/ObjectCache.cls | 65 +++++++++++++++++-- .../caching/tests/ObjectCacheTest.cls | 64 ++++++++++++++++++ 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls index 87f622778b1..cb9d41fab8e 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/ObjectCache.cls @@ -182,13 +182,25 @@ public inherited sharing class ObjectCache * * @param String The base key to put the objects into * @param List The SObjects to store - * @return ObjectCache Itself, allowing for a fluent interface + * @return ObjectCache Itself, allowing for a fluent interface */ public ObjectCache put( String key, List sobjects ) { return put( key, 'Id', sobjects ); } + /** + * Put the given SObject into the cache, using the given base key combined with the SObject's Id + * + * @param String The base key to put the objects into + * @param Sobject The SObject to store + * @return ObjectCache Itself, allowing for a fluent interface + */ + public ObjectCache put( String key, Sobject sobjectToStore ) + { + return put( key, new List{ sobjectToStore } ); + } + /** * Put the given SObjects into the cache, using the given base key combined with the value stored * in the SObject field as defined by idField @@ -204,9 +216,19 @@ public inherited sharing class ObjectCache return put( key, new SobjectIdGetter( idField ), sobjects ); } - // TODO: implement put( String key, IdGetter idGetter, Object objectToStore ) - // TODO: implement put( String key, String idField, Sobject sobjectToStore ) - // TODO: implement put( String key, Sobject sobjectToStore ) + /** + * Put the given SObject into the cache, using the given base key combined with the value stored + * in the SObject field as defined by idField + * + * @param String The base key to put the object into + * @param String The field to get the SObject identifiersfrom (field value must be a non-null String) + * @param Sobject The SObject to store + * @return ObjectCache Itself, allowing for a fluent interface + */ + public ObjectCache put( String key, String idField, Sobject sobjectToStore ) + { + return put( key, idField, new List{ sobjectToStore } ); + } /** * Put the given Objects into the cache, using the given base key combined with the value that is returned from @@ -235,12 +257,45 @@ public inherited sharing class ObjectCache for ( Object thisObject : objects ) { String thisId = idGetter.getIdFor( thisObject ); - cacheWrapper.put( buildFullKey( key, thisId ), thisObject, CACHE_LIFESPAN_SECONDS ); + put( key, thisId, thisObject ); } return this; } + /** + * Put the given Object into the cache, using the given base key combined with the value that is returned from + * the object when the IdGetter is called against it + * + * @param String The base key to put the object into + * @param IdGetter The mechanism for getting the Id from the object + * @param Object The Object to store + * @return ObjectCache Itself, allowing for a fluent interface + */ + public ObjectCache put( String key, IdGetter idGetter, Object objectToStore ) + { + return put( key, idGetter, new List{ objectToStore } ); + } + + /** + * Put the given Object into the cache, using the given base key combined with given id + * + * @param String The base key to put the object into + * @param String The Id within the key to put the object into + * @param Object The Object to store + * @return ObjectCache Itself, allowing for a fluent interface + */ + public ObjectCache put( String key, String id, Object objectToStore ) + { + Contract.requires( String.isNotBlank( key ), 'put called with a blank key' ); + Contract.requires( String.isNotBlank( id ), 'put called with a blank id' ); + Contract.requires( objectToStore != null, 'put called with a null objectToStore' ); + + cacheWrapper.put( buildFullKey( key, id ), objectToStore, CACHE_LIFESPAN_SECONDS ); + + return this; + } + /** * Remove all the cached objects from the given base key. * diff --git a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls index baee24dfb94..65c9f81b0f0 100644 --- a/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls +++ b/framework/default/ortoo-core/default/classes/fflib-extension/caching/tests/ObjectCacheTest.cls @@ -422,6 +422,70 @@ private without sharing class ObjectCacheTest System.assertEquals( objectsToStore[1], retrieval.cacheHits.get( OBJECT_ID ), 'put, when given objects with the same ids and keys, the latter will overwrite the earlier' ); } + @isTest + private static void put_whenGivenASingleSobject_storesIt() // NOPMD: Test method name format + { + Contact contact = new Contact( Id = TestIdUtils.generateId( Contact.SobjectType ) ); + + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', contact ); + + Test.stopTest(); + + ObjectCache.cacheRetrieval got = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .get( 'key', contact.Id ); + + System.assertEquals( contact, (Contact)got.cacheHits.get( contact.Id ), 'put, when given a single SObject, will store it' ); + } + + @isTest + private static void put_whenGivenASingleSobjectAndAFieldName_storesIt() // NOPMD: Test method name format + { + Contact contact = new Contact( AccountId = TestIdUtils.generateId( Account.SobjectType ) ); + + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', 'AccountId', contact ); + + Test.stopTest(); + + ObjectCache.cacheRetrieval got = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .get( 'key', contact.AccountId ); + + System.assertEquals( contact, (Contact)got.cacheHits.get( contact.AccountId ), 'put, when given a single SObject and a field name, will store it' ); + } + + @isTest + private static void put_whenGivenASingleObjectAndAnIdGetter_storesIt() // NOPMD: Test method name format + { + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', new StringIdGetter(), 'value1' ); + + Test.stopTest(); + + ObjectCache.cacheRetrieval got = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .get( 'key', 'value1Id' ); + + System.assertEquals( 'value1', got.cacheHits.get( 'value1Id' ), 'put, when given a single object and an id getter, will store it' ); + } + + @isTest + private static void put_whenGivenASingleObjectAndAnId_storesIt() // NOPMD: Test method name format + { + Test.startTest(); + new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .put( 'key', 'id1', 'value1' ); + + Test.stopTest(); + + ObjectCache.cacheRetrieval got = new ObjectCache().setScope( ObjectCache.CacheScope.SESSION ) + .get( 'key', 'id1' ); + + System.assertEquals( 'value1', got.cacheHits.get( 'id1' ), 'put, when given a single object and an id, will store it' ); + } + @isTest private static void remove_whenGivenAKeyThatExists_willRemoveAllObjectsFromThatKey() // NOPMD: Test method name format {