Partition Class

Base class of Cache.OrgPartition and Cache.SessionPartition. Use the subclasses to manage the cache partition for org caches and session caches.

Cache

After you obtain the partition object (an instance of Cache.OrgPartition or Cache.SessionPartition), the methods to add, retrieve, and manage the cache values in a partition take the key name. The key name that you supply to these methods (get(), put(), remove(), and contains()) doesn’t include the namespace.partition prefix.

See Also

The following are methods for Partition.

  • contains(key)

    Returns true if the cache partition contains a cached value corresponding to the specified key.

  • contains(setOfKeys)

    Returns true if the cache partition contains values for a specified set of keys.

  • createFullyQualifiedKey(namespace, partition, key)

    Generates a fully qualified key from the passed-in key components. The format of the generated key string is namespace.partition.key.

  • createFullyQualifiedPartition(namespace, partition)

    Generates a fully qualified partition name from the passed-in namespace and partition. The format of the generated partition string is namespace.partition.

  • get(key)

    Returns the cached value corresponding to the specified key from the cache partition.

  • get(keys)

    Returns the cached values corresponding to the specified set of keys from the cache partition.

  • get(cacheBuilder, key)

    Returns the cached value corresponding to the specified key from the partition cache. Use this method if your cached value is a class that implements the CacheBuilder interface.

  • getAvgGetSize()

    Returns the average item size of all the keys fetched from the partition, in bytes.

  • getAvgGetTime()

    Returns the average time taken to get a key from the partition, in nanoseconds.

  • getAvgValueSize()

    Deprecated and available only in API versions 49.0 and earlier. Returns the average item size for keys in the partition, in bytes.

  • getCapacity()

    Returns the percentage of cache used of the total capacity for this partition.

  • getKeys()

    Returns a set of all keys that are stored in the cache partition and visible to the invoking namespace.

  • getMaxGetSize()

    Returns the maximum item size of all the keys fetched from the partition, in bytes.

  • getMaxGetTime()

    Returns the maximum time taken to get a key from the partition, in nanoseconds.

  • getMaxValueSize()

    Deprecated and available only in API versions 49.0 and earlier. Returns the maximum item size for keys in the partition, in bytes.

  • getMissRate()

    Returns the miss rate in the partition.

  • getName()

    Returns the name of this cache partition.

  • getNumKeys()

    Returns the total number of keys in the partition.

  • isAvailable()

    Returns true if the Salesforce session is available. Only applies to Cache.SessionPartition. The session cache isn’t available when an active session isn’t present, such as in asynchronous Apex or code called by asynchronous Apex. For example, if batch Apex causes an Apex trigger to execute, the session cache isn’t available in the trigger because the trigger runs in asynchronous context.

  • put(key, value)

    Stores the specified key/value pair as a cached entry in the cache partition. The put method can write only to the cache in your org’s namespace.

  • put(key, value, visibility)

    Stores the specified key/value pair as a cached entry in the cache partition and sets the cached value’s visibility.

  • put(key, value, ttlSecs)

    Stores the specified key/value pair as a cached entry in the cache partition and sets the cached value’s lifetime.

  • put(key, value, ttlSecs, visibility, immutable)

    Stores the specified key/value pair as a cached entry in the cache partition. This method also sets the cached value’s lifetime, visibility, and whether it can be overwritten by another namespace.

  • remove(key)

    Deletes the cached value corresponding to the specified key from this cache partition.

  • remove(cacheBuilder, key)

    Deletes the cached value corresponding to the specified key from the partition cache. Use this method if your cached value is a class that implements the CacheBuilder interface.

  • validateCacheBuilder(cacheBuilder)

    Validates that the specified class implements the CacheBuilder interface.

  • validateKey(isDefault, key)

    Validates a cache key. This method throws a Cache.InvalidParamException if the key is not valid. A valid key is not null and contains alphanumeric characters.

  • validateKeyValue(isDefault, key, value)

    Validates a cache key and ensures that the cache value is non-null. This method throws a Cache.InvalidParamException if the key or value is not valid. A valid key is not null and contains alphanumeric characters.

  • validateKeys(isDefault, keys)

    Validates the specified cache keys. This method throws a Cache.InvalidParamException if the key is not valid. A valid key is not null and contains alphanumeric characters.

  • validatePartitionName(name)

    Validates the partition name — for example, that it is not null.

Returns true if the cache partition contains a cached value corresponding to the specified key.

public Boolean contains(String key)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

Type: Boolean

true if a cache entry is found. Othewise, false.

Returns true if the cache partition contains values for a specified set of keys.

public Map <String, Boolean> contains (Set<String> keys)

  • setOfKeys:

    Type: Set <String>

    A set of keys that uniquely identifies cached values. For information about the format of the key name, see Usage.

Type: Map <String, Boolean>

Returns the cache key and corresponding Boolean value indicating that the key entry exists. The Boolean value is false if the key entry doesn’t exist.

The number of input keys cannot exceed the maximum limit of 10.

In this example, the code checks for the presence of multiple keys on a partition. It fetches the cache key and the corresponding Boolean value for the key entry from the org cache of the partition.

In this example, the code checks for the presence of multiple keys on a partition. It fetches the cache key and the corresponding Boolean value for the key entry from the session cache of the partition.

Generates a fully qualified key from the passed-in key components. The format of the generated key string is namespace.partition.key.

public static String createFullyQualifiedKey(String namespace, String partition, String key)

  • namespace:

    Type: String

    The namespace of the cache key.

  • partition:

    Type: String

    The partition of the cache key.

  • key:

    Type: String

    The name of the cache key.

Type: String

Generates a fully qualified partition name from the passed-in namespace and partition. The format of the generated partition string is namespace.partition.

public static String createFullyQualifiedPartition(String namespace, String partition)

  • namespace:

    Type: String

    The namespace of the cache key.

  • partition:

    Type: String

    The partition of the cache key.

Type: String

Returns the cached value corresponding to the specified key from the cache partition.

public Object get(String key)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

Type:

Object The cached value as a generic object type. Cast the returned value to the appropriate type.

Returns the cached values corresponding to the specified set of keys from the cache partition.

public Map <String, Object> get (Set <String> keys)

  • keys:

    Type: Set <String>

    A set of keys that uniquely identify cached values. For information about the format of the key name, see Usage.

Type: Map <String, Object>

Returns the cache key and corresponding value. Returns null when no corresponding value is found for an input key.

The number of input keys cannot exceed the maximum limit of 10.

Fetch multiple keys from the org cache of a partition.

Fetch multiple keys from the session cache of a partition.

Returns the cached value corresponding to the specified key from the partition cache. Use this method if your cached value is a class that implements the CacheBuilder interface.

public Object get(System.Type cacheBuilder, String key)

  • cacheBuilder:

    Type: System.Type

    The Apex class that implements the CacheBuilder interface.

  • key:

    Type: String

    A case-sensitive string value that, combined with the class name corresponding to the cacheBuilder parameter, uniquely identifies a cached value.

Type:

Object The cached value as a generic object type. Cast the returned value to the appropriate type.

Returns the average item size of all the keys fetched from the partition, in bytes.

public Long getAvgGetSize()

Type: Long

Returns the average time taken to get a key from the partition, in nanoseconds.

public Long getAvgGetTime()

Type: Long

Deprecated and available only in API versions 49.0 and earlier. Returns the average item size for keys in the partition, in bytes.

public Long getAvgValueSize()

Type: Long

Returns the percentage of cache used of the total capacity for this partition.

public Double getCapacity()

Type: Double

Used partition cache as a percentage number.

Returns a set of all keys that are stored in the cache partition and visible to the invoking namespace.

public Set<String> getKeys()

Type: Set<String>

A set containing all cache keys.

Returns the maximum item size of all the keys fetched from the partition, in bytes.

public Long getMaxGetSize()

Type: Long

Returns the maximum time taken to get a key from the partition, in nanoseconds.

public Long getMaxGetTime()

Type: Long

Deprecated and available only in API versions 49.0 and earlier. Returns the maximum item size for keys in the partition, in bytes.

public Long getMaxValueSize()

Type: Long

Returns the miss rate in the partition.

public Double getMissRate()

Type: Double

Returns the name of this cache partition.

public String getName()

Type: String

The name of this cache partition.

Returns the total number of keys in the partition.

public Long getNumKeys()

Type: Long

Returns true if the Salesforce session is available. Only applies to Cache.SessionPartition. The session cache isn’t available when an active session isn’t present, such as in asynchronous Apex or code called by asynchronous Apex. For example, if batch Apex causes an Apex trigger to execute, the session cache isn’t available in the trigger because the trigger runs in asynchronous context.

public Boolean isAvailable()

Type: Boolean

Stores the specified key/value pair as a cached entry in the cache partition. The put method can write only to the cache in your org’s namespace.

public void put(String key, Object value)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

  • value:

    Type:

    Object The value to store in the cache. The cached value must be serializable.

Type: void

Stores the specified key/value pair as a cached entry in the cache partition and sets the cached value’s visibility.

public void put(String key, Object value, cache.Visibility visibility)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

  • value:

    Type:

    Object The value to store in the cache. The cached value must be serializable.

  • visibility:

    Type: Cache.Visibility

    Indicates whether the cached value is available only to Apex code that is executing in the same namespace or to Apex code executing from any namespace.

Type: void

Stores the specified key/value pair as a cached entry in the cache partition and sets the cached value’s lifetime.

public void put(String key, Object value, Integer ttlSecs)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

  • value:

    Type:

    Object The value to store in the cache. The cached value must be serializable.

  • ttlSecs:

    Type: Integer

    The amount of time, in seconds, to keep the cached value in the cache.

Type: void

Stores the specified key/value pair as a cached entry in the cache partition. This method also sets the cached value’s lifetime, visibility, and whether it can be overwritten by another namespace.

public void put(String key, Object value, Integer ttlSecs, cache.Visibility visibility, Boolean immutable)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

  • value:

    Type: Object The value to store in the cache. The cached value must be serializable.

  • ttlSecs:

    Type: Integer The amount of time, in seconds, to keep the cached value in the cache.

  • visibility:

    Type: Cache.Visibility Indicates whether the cached value is available only to Apex code that is executing in the same namespace or to Apex code executing from any namespace.

  • immutable:

    Type: Boolean

    Indicates whether the cached value can be overwritten by another namespace (false) or not (true).

Type: void

Deletes the cached value corresponding to the specified key from this cache partition.

public Boolean remove(String key)

  • key:

    Type: String

    A case-sensitive string value that uniquely identifies a cached value.

Type: Boolean

true if the cache value was successfully removed. Otherwise, false.

Deletes the cached value corresponding to the specified key from the partition cache. Use this method if your cached value is a class that implements the CacheBuilder interface.

public Boolean remove(System.Type cacheBuilder, String key)

  • cacheBuilder:

    Type: System.Type

    The Apex class that implements the CacheBuilder interface.

  • key:

    Type: String

    A case-sensitive string value that, combined with the class name corresponding to the cacheBuilder parameter, uniquely identifies a cached value.

Type: Boolean

true if the cache value was successfully removed. Otherwise, false.

Validates that the specified class implements the CacheBuilder interface.

public static void validateCacheBuilder(System.Type cacheBuilder)

  • cacheBuilder:

    Type: System.Type

    The class to validate.

Type: void

Validates a cache key. This method throws a Cache.InvalidParamException if the key is not valid. A valid key is not null and contains alphanumeric characters.

public static void validateKey(Boolean isDefault, String key)

  • isDefault:

    Type: Boolean

    Set to true if the key references a default partition. Otherwise, set to false.

  • key:

    Type: String

    The key to validate.

Type: void

Validates a cache key and ensures that the cache value is non-null. This method throws a Cache.InvalidParamException if the key or value is not valid. A valid key is not null and contains alphanumeric characters.

public static void validateKeyValue(Boolean isDefault, String key, Object value)

  • isDefault:

    Type: Boolean Set to true

    if the key references a default partition. Otherwise, set to false.

  • key:

    Type: String

    The key to validate.

  • value:

    Type:

    Object The cache value to validate.

Type: void

Validates the specified cache keys. This method throws a Cache.InvalidParamException if the key is not valid. A valid key is not null and contains alphanumeric characters.

public static void validateKeys(Boolean isDefault, Set<String> keys)

  • isDefault:

    Type: Boolean Set to true

    if the key references a default partition. Otherwise, set to false.

  • keys:

    Type: Set<String>

    A set of key string values to validate.

Type: void

Validates the partition name — for example, that it is not null.

public static void validatePartitionName(String name)

  • name:

    Type: String

    The name of the partition to validate.

Type: void