Skip to content

Commit

Permalink
Implemented version with a cache entry per sobject
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 8, 2022
1 parent 6a6ba9f commit 577e8b0
Showing 1 changed file with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
public with sharing class SobjectCache
{
// TODO: try writing something that gets some things from the cache, then deals with the difference.
// TODO: maybe we need a return object that contains a list of the cache misses.

public class CacheAccessViolationException extends Exceptions.DeveloperException {} // this looks like a config exception, but actually the system should be built
// in such a way that it's never possible to get this exception
public enum CacheScope { ORG, SESSION }
Expand Down Expand Up @@ -47,27 +44,77 @@ public with sharing class SobjectCache
return this;
}

// returns as many of the objects from the cache as can be returned
public CacheRetrieval get( String key, Set<Id> ids )
{
return new CacheRetrieval();
CacheRetrieval values = new CacheRetrieval();
for ( Id thisId : ids )
{
SObject thisValue = (SObject)cacheWrapper.get( createFullyQualifiedKey( key, thisId ) );
if ( thisValue != null )
{
values.addCacheHit( thisId, thisValue );
}
else
{
values.addCacheMiss( thisId );
}
}
return values;
}

public SobjectCache put( String key, List<Sobject> sobjects )
{
return put( key, 'Id', sobjects );
}

public SobjectCache put( String key, String idField, List<Sobject> sobjects )
{
for ( Sobject thisSobject : sobjects )
{
final String thisKey = createFullyQualifiedKey( key, (Id)thisSobject.get( idField ) );
cacheWrapper.put( thisKey, thisSobject, CACHE_LIFESPAN_SECONDS );
}
return this;
}

public SobjectCache clear( String key )
public SobjectCache remove( String subkeyToRemove )
{
Set<String> keys = cacheWrapper.getKeys();

for ( String thisKey : keys )
{
if ( isAKeyFor( thisKey, subkeyToRemove ) )
{
cacheWrapper.remove( thisKey );
}
}
return this;
}

public SobjectCache clear( String key, Set<Id> ids )
public SobjectCache remove( String key, Set<Id> ids )
{
for ( Id thisId : ids )
{
cacheWrapper.remove( createFullyQualifiedKey( key, thisId ) );
}
return this;
}

private Boolean isAKeyFor( String keyToCheck, String keyToCheckAgainst )
{
return keyToCheck.startsWith( createKeyPrefix( keyToCheckAgainst ) );
}

private String createFullyQualifiedKey( String subKey, String id )
{
return createKeyPrefix( subKey ) + id;
}

private String createKeyPrefix( String subKey )
{
return cacheWrapper.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, PARTITION_NAME, subkey ) + 'x';
}

public class CacheRetrieval
{
public Map<Id,SObject> cacheHits { get; private set; }
Expand Down

0 comments on commit 577e8b0

Please sign in to comment.