Newer Version Available
Session Class
Namespace
Usage
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. |
Example
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 a default partition in your org. The Visualforce page expects a partition named myPartition. To run this sample, create a default partition in your org with the name myPartition.
The Visualforce page contains four output components. The first three components call get methods on the controller that return the following values from the cache: a date, data based on the MyData inner class, and a counter. The next output component uses the $Cache.Session global variable to get the cached string value for the key named output. Next, the $Cache.Session global variable is used again in the Visualforce page to iterate over the elements of a cached value of type 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. If 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 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.
This is the Visualforce page that corresponds to the SessionCacheController 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.
Session Methods
The following are methods for Session. All methods are static.
contains(setOfKeys)
Signature
public static Map <String, Boolean> contains (Set<String> keys)
Parameters
Return Value
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.
Usage
The number of input keys cannot exceed the maximum limit of 10.
Example
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 session 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 session cache of different partitions.
get(key)
Signature
public static Object get(String key)
Parameters
Return Value
Type: Object
The cached value as a generic object type. Cast the returned value to the appropriate type.
Usage
Because Cache.Session.get() returns an object, we recommend that you cast the returned value to a specific type to facilitate use of the returned value.
If a Cache.Session.get() call doesn’t find the referenced key, it returns null.
get(keys)
Signature
public static Map <String, Object> get (Set <String> keys)
Parameters
Return Value
Type: Map <String, Object>
Returns the cache key and corresponding value. Returns null when no corresponding value is found for an input key.
Usage
The number of input keys cannot exceed the maximum limit of 10.
Example
Fetch multiple keys from the session cache of the default partition.
Fetch multiple keys from the session cache of different partitions.
get(cacheBuilder, key)
Signature
public static Object get(System.Type cacheBuilder, String key)
Parameters
- 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.
Return Value
Type: Object
The cached value as a generic object type. Cast the returned value to the appropriate type.
Usage
Because Cache.Session.get(cacheBuilder, key) returns an object, cast the returned value to a specific type to facilitate use of the returned value.
getAvgValueSize()
Signature
public static Long getAvgValueSize()
Return Value
Type: Long
getMaxValueSize()
Signature
public static Long getMaxValueSize()
Return Value
Type: Long
getPartition(partitionName)
Signature
public static cache.SessionPartition getPartition(String partitionName)
Parameters
- partitionName
- Type: String
- A partition name that is qualified by the namespace, for example, namespace.partition.
Return Value
Type: Cache.SessionPartition
Example
After you get the session partition, you can add and retrieve the partition’s cache values.
isAvailable()
Signature
public static Boolean isAvailable()
put(key, value)
put(key, value, visibility)
Signature
public static void put(String key, Object value, Cache.Visibility visibility)
Parameters
- key
- Type: String
- A string that uniquely identifies the value to be cached. 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.
Return Value
Type: void
put(key, value, ttlSecs)
Signature
public static void put(String key, Object value, Integer ttlSecs)
Parameters
- key
- Type: String
- A string that uniquely identifies the value to be cached. 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 session cache. The cached values remain in the cache as long as the Salesforce session hasn’t expired. The maximum value is 28,800 seconds or eight hours. The minimum value is 300 seconds or five minutes.
Return Value
Type: void
put(key, value, ttlSecs, visibility, immutable)
Signature
public static void put(String key, Object value, Integer ttlSecs, cache.Visibility visibility, Boolean immutable)
Parameters
- key
- Type: String
- A string that uniquely identifies the value to be cached. 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 session cache. The cached values remain in the cache as long as the Salesforce session hasn’t expired. The maximum value is 28,800 seconds or eight hours. The minimum value is 300 seconds or five minutes.
- 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)
Signature
public static Boolean remove(String key)
Parameters
remove(cacheBuilder, key)
Signature
public static Boolean remove(System.Type cacheBuilder, String key)
Parameters
- 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.