Org Class
Use the Cache.Org class to add, retrieve, and manage values in the org cache. Unlike the session cache, the org cache is not tied to any session and is available to the organization across requests and to all users.
Cache Key Format
This table lists the format of the key parameter that some methods in this class take, such as put, get, and contains.
| Key Format | Description |
|---|---|
| namespace.partition.key | Fully qualified key name. |
| key | Refers to a partition marked as default when the namespace.partition prefix is omitted. |
local.partition.key | Use the local prefix to refer to the org’s namespace when the org doesn’t have a namespace defined. If the org has a namespace defined, the local prefix also refers to that org’s namespace. |
- If no default partition is specified in the org, calling a cache method without fully qualifying the key name causes a
Cache.Org.OrgCacheExceptionto be thrown. - The
localprefix in an installed managed package refers to the namespace of the subscriber org and not the package’s namespace. The cacheputcalls aren’t allowed in a partition that the invoking class doesn’t own.
This class is the controller for a sample Visualforce page (shown in the subsequent code sample). The cached values are initially added to the cache by the init() method, which the Visualforce page invokes when it loads through the action attribute. The cache keys don’t contain the namespace.partition prefix. They all refer to the default partition in your org. To run this sample, create a partition and mark it as default.
The Visualforce page contains four output components. These components call get methods on the controller that returns the following values from the cache: a date, data based on the MyData inner class, a counter, a text value, and a list. The size of the list is also returned.
The Visualforce page also contains two buttons. The Rerender button invokes the go() method on the controller. This method increases the values of the counter and the custom data in the cache. When you click Rerender, the two counters increase by one each time. The go() method retrieves the values of these counters from the cache, increments their values by one, and stores them again in the cache.
The Remove datetime Key button deletes the date-time value (with key datetime) from the cache. As a result, the value next to Cached datetime: is cleared on the page.
If another user logs in and runs this sample, this user gets the cache values that were last added or updated by the previous user. For example, if the counter value was five, the next user sees the counter value as increased to six.
This is the Visualforce page that corresponds to the OrgCacheController class.
This is the output of the page after clicking the Rerender button twice. The counter value could differ in your case if a key named counter was already in the cache before running this sample.
-
The Org class provides a constant that you can use when setting the time-to-live (TTL) value.
See Also
The Org class provides a constant that you can use when setting the time-to-live (TTL) value.
| Constant | Description |
|---|---|
MAX_TTL_SECS | Represents the maximum amount of time, in seconds, to keep the cached value in the org cache. |
The following are methods for Org. All methods are static.
-
Returns
trueif the org cache contains a cached value corresponding to the specified key. -
Returns
trueif the org cache contains values for the specified key entries. -
Returns
trueif the org cache contains values for a specified set of keys. -
Returns the cached value corresponding to the specified key from the org cache.
-
Returns the cached value corresponding to the specified key from the org cache. Use this method if your cached value is a class that implements the
CacheBuilderinterface. -
Returns the cached values corresponding to the specified set of keys from the org cache.
-
Returns the average item size of all the keys fetched from the org cache, in bytes.
-
Returns the average time taken to get a key from the org cache, in nanoseconds.
-
Deprecated and available only in API versions 49.0 and earlier. Returns the average item size for keys in the org cache, in bytes.
-
Returns the percentage of org cache capacity that has been used.
-
Returns a set of all keys that are stored in the org cache and visible to the invoking namespace.
-
Returns the maximum item size of all the keys fetched from the org cache, in bytes.
-
Returns the maximum time taken to get a key from the org cache, in nanoseconds.
-
Deprecated and available only in API versions 49.0 and earlier. Returns the maximum item size for keys in the org cache, in bytes.
-
Returns the miss rate in the org cache.
-
Returns the name of the default cache partition.
-
Returns the total number of keys in the org cache.
-
Returns a partition from the org cache that corresponds to the specified partition name.
-
Stores the specified key/value pair as a cached entry in the org cache. The
putmethod can write only to the cache in your org’s namespace. -
Stores the specified key/value pair as a cached entry in the org cache and sets the cached value’s visibility.
-
Stores the specified key/value pair as a cached entry in the org cache 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 org cache. This method also sets the cached value’s lifetime, visibility, and whether it can be overwritten by another namespace.
-
Deletes the cached value corresponding to the specified key from the org cache.
-
Deletes the cached value corresponding to the specified key from the org cache. Use this method if your cached value is a class that implements the
CacheBuilderinterface.
Returns true if the org cache contains a cached value corresponding to the specified key.
public static Boolean contains(String key)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
Type: Boolean
true if a cache entry is found. Othewise, false.
Returns true if the org cache contains values for the specified key entries.
public static List<Boolean> contains(List<String> keys)
-
keys:
Type: List<String>
A list of keys that identifies cached values. For information about the format of the key name, see Usage.
Type: List<Boolean>
true if the key entries are found. Othewise, false.
Returns true if the org cache contains values for a specified set of keys.
public static 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 the default partition. It fetches the cache key and the corresponding Boolean value for the key entry from the org cache of the default partition.
In this example, the code checks for the presence of multiple keys on different partitions. It fetches the cache key and the corresponding Boolean value for the key entry from the org cache of different partitions.
Returns the cached value corresponding to the specified key from the org cache.
public static Object get(String key)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
Type:
Object The cached value as a generic object type. Cast the returned value to the appropriate type.
Because Cache.Org.get() returns an object, cast the returned value to a specific type to facilitate use of the returned value.
If a Cache.Org.get() call doesn’t find the referenced key, it returns null.
Returns the cached value corresponding to the specified key from the org cache. Use this method if your cached value is a class that implements the CacheBuilder interface.
public static Object get(System.Type cacheBuilder, String key)
-
cacheBuilder:
Type: System.Type
The Apex class that implements the
CacheBuilderinterface. -
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.
Because Cache.Org.get(cacheBuilder, key) returns an object, cast the returned value to a specific type to facilitate use of the returned value.
Returns the cached values corresponding to the specified set of keys from the org cache.
public static 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 the default partition.
Fetch multiple keys from the org cache of different partitions.
Returns the average item size of all the keys fetched from the org cache, in bytes.
public static Long getAvgGetSize()
Type: Long
In this example the following keys and their corresponding value sizes are inserted. The code then fetches the keys: key 1, key 2, key 3 and key 4 and returns the average item size of the fetched keys.
| Key | Key Value Size |
|---|---|
| key 1 | 42 |
| key 2 | 42 |
| key 3 | 58 |
| key 4 | 36 |
| key 5 | 36 |
Returns the average time taken to get a key from the org cache, in nanoseconds.
public static Long getAvgGetTime()
Type: Long
Deprecated and available only in API versions 49.0 and earlier. Returns the average item size for keys in the org cache, in bytes.
public static Long getAvgValueSize()
Type: Long
Returns the percentage of org cache capacity that has been used.
public static Double getCapacity()
Type: Double
Used cache as a percentage number.
Returns a set of all keys that are stored in the org cache and visible to the invoking namespace.
public static Set<String> getKeys()
Type: Set<String>
A set containing all cache keys.
Returns the maximum item size of all the keys fetched from the org cache, in bytes.
public static Long getMaxGetSize()
Type: Long
In this example the following keys and their corresponding value sizes are inserted. The code fetches the keys: key 1, key 2 and key 4 and returns the maximum key value size from the fetched keys.
| Key | Key Value Size |
|---|---|
| key 1 | 42 |
| key 2 | 42 |
| key 3 | 58 |
| key 4 | 36 |
| key 5 | 36 |
Returns the maximum time taken to get a key from the org cache, in nanoseconds.
public static Long getMaxGetTime()
Type: Long
Deprecated and available only in API versions 49.0 and earlier. Returns the maximum item size for keys in the org cache, in bytes.
public static Long getMaxValueSize()
Type: Long
Returns the miss rate in the org cache.
public static Double getMissRate()
Type: Double
Returns the name of the default cache partition.
public String getName()
Type: String
The name of the default cache partition.
Returns the total number of keys in the org cache.
public static Long getNumKeys()
Type: Long
Returns a partition from the org cache that corresponds to the specified partition name.
public static cache.OrgPartition getPartition(String partitionName)
-
partitionName:
Type: String
A partition name that is qualified by the namespace, for example, namespace.partition.
Type: Cache.OrgPartition
After you get the org partition, you can add and retrieve the partition’s cache values.
Stores the specified key/value pair as a cached entry in the org cache. The put method can write only to the cache in your org’s namespace.
public static void put(String key, Object value)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
-
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 org cache and sets the cached value’s visibility.
public static void put(String key, Object value, Cache.Visibility visibility)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
-
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 org cache and sets the cached value’s lifetime.
public static void put(String key, Object value, Integer ttlSecs)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
-
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 org cache. The maximum is 172,800 seconds (48 hours). The minimum value is 300 seconds or 5 minutes. The default value is 86,400 seconds (24 hours).
Type: void
Stores the specified key/value pair as a cached entry in the org cache. This method also sets the cached value’s lifetime, visibility, and whether it can be overwritten by another namespace.
public static 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. For information about the format of the key name, see Usage.
-
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 org cache. The maximum is 172,800 seconds (48 hours). The minimum value is 300 seconds or 5 minutes. The default value is 86,400 seconds (24 hours).
-
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 the org cache.
public static Boolean remove(String key)
-
key:
Type: String
A case-sensitive string value that uniquely identifies a cached value. For information about the format of the key name, see Usage.
Type: Boolean
true if the cache value was successfully removed. Otherwise, false.
Deletes the cached value corresponding to the specified key from the org cache. Use this method if your cached value is a class that implements the CacheBuilder interface.
public static Boolean remove(System.Type cacheBuilder, String key)
-
cacheBuilder:
Type: System.Type
The Apex class that implements the
CacheBuilderinterface. -
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.