The Cacheable interface (used by SOQLCache) provides a limited set of operations specifically for retrieving and managing cached records. Unlike the standard SOQL Library, methods related to sharing rules (e.g., withSharing(), withoutSharing()) or query scoping (e.g., forView()) are not available because cached records are retrieved directly from storage, bypassing standard database access controls.
Key categories of methods available in the Cacheable interface include:
- Configuration:
cacheInApexTransaction(), cacheInOrgCache(), cacheInSessionCache(), and maxHoursWithoutRefresh(Integer hours). - Selection:
with(...) to specify fields, whereEqual(...) for filtering, and byId(...) for direct ID lookups. - Security:
stripInaccessible() to enforce field-level security. - Results:
toId(), doExist(), toObject(), and toValueOf(SObjectField fieldToExtract) to extract the data.
public interface Cacheable {
// CONFIG
Cacheable cacheInApexTransaction();
Cacheable cacheInOrgCache();
Cacheable cacheInSessionCache();
Cacheable maxHoursWithoutRefresh(Integer hours);
// SELECT
Cacheable with(SObjectField field);
Cacheable with(List<SObjectField> fields);
Cacheable with(String fields);
// WHERE
Cacheable whereEqual(SObjectField field, Object value);
Cacheable whereEqual(String field, Object value);
// FIELD-LEVEL SECURITY
Cacheable stripInaccessible();
Cacheable stripInaccessible(AccessType accessType);
// MOCKING
Cacheable mockId(String queryIdentifier);
// DEBUGGING
Cacheable preview();
// PREDEFINED
Cacheable byId(SObject record);
Cacheable byId(Id recordId);
// RESULT
Id toId();
Boolean doExist();
SObject toObject();
Object toValueOf(SObjectField fieldToExtract);
}