forked from apex-enterprise-patterns/fflib-apex-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started experimentation with Org Wide SOQL results cache
- Loading branch information
1 parent
50aab44
commit 513d3d9
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
framework/default/ortoo-core/default/classes/fflib-extension/OrgCachedSoqlExecutor.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ork/default/ortoo-core/default/classes/fflib-extension/OrgCachedSoqlExecutor.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |