Skip to content

Commit

Permalink
Added single object puts
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 14, 2022
1 parent c40e580 commit 678acfd
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,25 @@ public inherited sharing class ObjectCache
*
* @param String The base key to put the objects into
* @param List<Sobject> 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<Sobject> 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<Sobject>{ 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
Expand All @@ -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<Sobject>{ sobjectToStore } );
}

/**
* Put the given Objects into the cache, using the given base key combined with the value that is returned from
Expand Down Expand Up @@ -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<Object>{ 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 678acfd

Please sign in to comment.