Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

CacheBuilder Interface

An interface for safely retrieving and removing values from a session or org cache. Use the interface to generate a value that you want to store in the cache. The interface checks for cache misses, which means you no longer need to check for null cache values yourself.

Namespace

Cache

CacheBuilder Methods

The following are methods for CacheBuilder.

doLoad(var)

Contains the logic that builds a cached value. You don’t call this method directly. Instead, it’s called indirectly when you reference the class that implements the CacheBuilder interface.

Signature

public Object doLoad(String var)

Parameters

  • var:

    Type: String

    A case-sensitive alphanumeric string value used to build a cached value. This parameter is also used as part of the unique key that identifies the cached value.

Return Value

Type:

Object The value that was cached. Cast the return value to the appropriate type.

CacheBuilder Example Implementation

This example creates a class called UserInfoCache that implements the CacheBuilder interface. The class caches the results of a SOQL query run against the User object.

1class UserInfoCache implements Cache.CacheBuilder {
2    public Object doLoad(String userid) {
3        User u = (User)[SELECT Id, IsActive, username FROM User WHERE id =: userid];
4        return u;
5    }
6}

This example gets a cached User record based on a user ID. If the value exists in the org cache, it is returned. If the value doesn’t exist, the doLoad(String var) method is re-executed, and the new value is cached and returned.

1User batman = (User) Cache.Org.get(UserInfoCache.class, ‘00541000000ek4c');