Skip to main content
FCORE_BASE.CacheService is an in-memory, per-transaction record cache that prevents duplicate SOQL. The first request for a given record-and-field combination queries and caches the result; later requests in the same transaction only query the Ids and fields that have not yet been cached. There are two singletons, one per sharing mode:
  • FCORE_BASE.CacheServiceWithSharing — respects the running user’s sharing rules.
  • FCORE_BASE.CacheServiceWithoutSharing — ignores sharing.
Get either with getInstance(). Most feature code reaches the cache indirectly through a DB class, which is the preferred way to query an object; use CacheService directly when you are writing or extending a DB class.

Caching Fields on a Record

Use getById(SObjectType, Id, <fields>) for a single record and getByIds(SObjectType, Set<Id>, <fields>) for many. The id argument is a single Id for getById and a Set<Id> (not a List) for getByIds. The <fields> argument can be a CSV String, a Set<String>, or a List<SObjectField>:
A second call for the same record and fields is served from the cache with no additional SOQL. If you ask for a field that is not yet cached, only that field is queried.

Caching Relationships With QueryUnit

To cache a parent record together with its child records (a subquery), use a FCORE_BASE.QueryUnit. QueryUnit is its own top-level class — not an inner class of CacheService. Constructors:
  • QueryUnit(SObjectType, Set<String>)
  • QueryUnit(SObjectType, List<SObjectField>)
  • QueryUnit(SObjectType, String)
Add a child query with addChildQueryUnit(QueryUnit queryUnit, String relation), then pass the parent QueryUnit to getById(QueryUnit, Id) or getByIds(QueryUnit, Set<Id>):

Invalidating the Cache

If you update records and need fresh data later in the same transaction, clear the relevant cache entries: