Partition Class

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

Namespace

Cache

Cache Key Format for Partition Methods

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.

Partition Methods

The following are methods for Partition.

contains(key)

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

Signature

public Boolean contains(String key)

Parameters

key
Type: String
A case-sensitive string value that uniquely identifies a cached value.

Return Value

Type: Boolean

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

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.

Signature

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

Parameters

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.

Return Value

Type: String

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.

Signature

public static String createFullyQualifiedPartition(String namespace, String partition)

Parameters

namespace
Type: String
The namespace of the cache key.
partition
Type: String
The partition of the cache key.

Return Value

Type: String

get(key)

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

Signature

public Object get(String key)

Parameters

key
Type: String
A case-sensitive string value that uniquely identifies a cached value.

Return Value

Type: Object

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

getAvgGetTime()

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

Signature

public Long getAvgGetTime()

Return Value

Type: Long

getAvgValueSize()

Returns the average item size for keys in the partition, in bytes.

Signature

public Long getAvgValueSize()

Return Value

Type: Long

getCapacity()

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

Signature

public Double getCapacity()

Return Value

Type: Double

Used partition cache as a percentage number.

getKeys()

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

Signature

public Set<String> getKeys()

Return Value

Type: Set<String>

A set containing all cache keys.

getMaxGetTime()

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

Signature

public Long getMaxGetTime()

Return Value

Type: Long

getMaxValueSize()

Returns the maximum item size for keys in the partition, in bytes.

Signature

public Long getMaxValueSize()

Return Value

Type: Long

getMissRate()

Returns the miss rate in the partition.

Signature

public Double getMissRate()

Return Value

Type: Double

getName()

Returns the name of this cache partition.

Signature

public String getName()

Return Value

Type: String

The name of this cache partition.

getNumKeys()

Returns the total number of keys in the partition.

Signature

public Long getNumKeys()

Return Value

Type: Long

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.

Signature

public Boolean isAvailable()

Return Value

Type: Boolean

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.

Signature

public void put(String key, Object value)

Parameters

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.

Return Value

Type: void

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.

Signature

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

Parameters

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.

Return Value

Type: void

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.

Signature

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

Parameters

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.

Return Value

Type: void

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.

Signature

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

Parameters

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).

Return Value

Type: void

remove(key)

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

Signature

public Boolean remove(String key)

Parameters

key
Type: String
A case-sensitive string value that uniquely identifies a cached value.

Return Value

Type: Boolean

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

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.

Signature

public static void validateKey(Boolean isDefault, String key)

Parameters

isDefault
Type: Boolean
Set to true if the key references a default partition. Otherwise, set to false.
key
Type: String
The key to validate.

Return Value

Type: void

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.

Signature

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

Parameters

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.

Return Value

Type: void

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.

Signature

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

Parameters

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.

Return Value

Type: void

validatePartitionName(name)

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

Signature

public static void validatePartitionName(String name)

Parameters

name
Type: String
The name of the partition to validate.

Return Value

Type: void