---
schema_version: "1"
id: d6c8d71370ee5a8409bc57b64a68518ba116cc1e0148c0b68db8b9b2b5a71385
title: CacheBuilder Interface
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_interface_cache_CacheBuilder.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_cache_CacheBuilder.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 80e4af38b307ecc9a16971eaf08d531238ff81a696dd28ec212debea65c7bd40
description: 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.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.
---
# 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_cache.htm "The Cache namespace contains methods for managing the platform cache.")

- **[CacheBuilder Methods](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_cache_CacheBuilder.htm#apex_cache_CacheBuilder_methods)**
- **[CacheBuilder Example Implementation](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_cache_CacheBuilder.htm#apex_interface_cache_CacheBuilder_example)**

#### See Also

- [_Apex Developer Guide_: Safely Cache Values with the CacheBuilder Interface](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_platform_cache_builder.htm "Apex Developer Guide: Safely Cache Values with the CacheBuilder Interface - HTML (New Window)")

## CacheBuilder Methods

The following are methods for CacheBuilder.

- **[doLoad(var)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_interface_cache_CacheBuilder.htm#apex_cache_CacheBuilder_doLoad)**  
    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.

### 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
    
    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.

public with sharing class UserInfoCache implements Cache.CacheBuilder {
    public Object doLoad(String userid) {
        User u = (User)\[SELECT Id, IsActive, username FROM User WHERE id =: userid WITH USER\_MODE\];
        return u;
    }
}

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.

User batman = (User) Cache.Org.get(UserInfoCache.class, '005XXXXXXXXXXXX');