Skip to main content

Building Cached Selector

Check examples in the repository.

SOQL-Lib is agile, so you can adjust the solution according to your needs. We don't force one approach over another; you can choose your own. Here are our suggestions:

In this configuration, you can specify default fields, the cache storage type cacheIn... (Org Cache, Session Cache, or Apex transaction), and the time in hours before a refresh is required (maxHoursWithoutRefresh). Additionally, you can add an initialQuery() to prepopulate records in the cache, ensuring that subsequent queries retrieve records from the prepopulated set. The additionalAllowedConditionFields allows you to specify which additional fields can be used in conditions besides Id, Name, DeveloperName and unique fields. You change it at your own risk, because non-unique fields cannot guarantee that the correct records are returned from the cache. More details here.

public with sharing class SOQL_ProfileCache extends SOQLCache implements SOQLCache.Selector {
public static SOQL_ProfileCache query() {
return new SOQL_ProfileCache();
}

private SOQL_ProfileCache() {
super(Profile.SObjectType);
cacheInOrgCache();
maxHoursWithoutRefresh(48);
with(Profile.Id, Profile.Name, Profile.UserType);
}

public override SOQL.Queryable initialQuery() {
return SOQL.of(Profile.SObjectType);
}

public override List<SObjectField> additionalAllowedConditionFields() {
return new List<SObjectField>{
Profile.UserType // not unique field that !!!
};
}

public SOQL_ProfileCache byName(String name) {
whereEqual(Profile.Name, name);
return this;
}
}