Skip to content

Commit

Permalink
Started experimentation with Org Wide SOQL results cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-baillie-ortoo committed Mar 3, 2022
1 parent 50aab44 commit 513d3d9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
public inherited sharing class OrgCachedSoqlExecutor
{
private final static string SOQL_PARTITION_NAME = 'soql';

public List<Sobject> query( String soql )
{
String key = generateKey( soql );
List<Sobject> returnValues = null;

try
{
returnValues = (List<Sobject>)Cache.Org.get( key );
}
catch ( cache.Org.OrgCacheException e )
{
System.debug( LoggingLevel.ERROR, 'Attempt to read from the Org Platform Cache failed for the SOQL: ' + soql );
System.debug( LoggingLevel.ERROR, e );
}
if ( returnValues == null )
{
System.debug( LoggingLevel.INFO, 'Org Platform Cache miss when running the SOQL: ' + soql );

returnValues = Database.query( soql );

try
{
Cache.Org.put( key, returnValues, 43200, Cache.Visibility.NAMESPACE, false ); // immutable results
}
catch ( Exception e )
{
System.debug( LoggingLevel.ERROR, 'Attempt to write into the Org Platform Cache failed for the SOQL: ' + soql );
System.debug( LoggingLevel.ERROR, e );
}
}

return returnValues;
}

public void clearCacheFor( String soql )
{
Cache.Org.remove( generateKey( soql ) );
}

// TODO: how do we get this to just try to remove things that are in this partition?
public void clearAllCache()
{
String fullSoqlPartitionName = Cache.OrgPartition.createFullyQualifiedPartition( PackageUtils.NAMESPACE_PREFIX, SOQL_PARTITION_NAME );
for ( String thisKey : Cache.Org.getKeys() )
{
String qualifiedKey = qualifiedKey( thisKey );
if ( Cache.Org.contains( qualifiedKey ) )
{
Cache.Org.remove( qualifiedKey );
}
}
}

public String generateKey( String soql )
{
String subkey = EncodingUtil.convertToHex( Crypto.generateDigest( 'SHA1', Blob.valueOf( soql ) ) );
return qualifiedKey( subkey );
}

private String qualifiedKey( String subkey )
{
return Cache.OrgPartition.createFullyQualifiedKey( PackageUtils.NAMESPACE_PREFIX, SOQL_PARTITION_NAME, subkey );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 513d3d9

Please sign in to comment.