Newer Version Available
SessionPartition Class
Namespace
Usage
This class extends Cache.Partition and inherits all of its non-static methods. Utility methods for creating and validating keys are not supported and can be called only from the Cache.Partition parent class. For a list of Cache.Partition methods, see Partition Methods.
To get a session partition, call Cache.Session.getPartition and pass in a fully qualified partition name, as follows.
1Cache.SessionPartition sessionPartition = Cache.Session.getPartition('namespace.myPartition');Example
This class is the controller for a sample Visualforce page (shown in the subsequent code sample). The controller shows how to use the methods of Cache.SessionPartition to manage a cache value on a particular partition. The controller takes inputs from the Visualforce page for the partition name, key name for a counter, and initial counter value. The controller contains default values for these inputs. When you click Rerender on the Visualforce page, the go() method is invoked and increases the counter by one. When you click Remove Key, the counter key is removed from the cache. The counter value gets reset to its initial value when it’s re-added to the cache.
1public class SessionPartitionController {
2
3 // Name of a partition in the local namespace
4 String partitionInput = 'local.myPartition';
5 // Name of the key
6 String counterKeyInput = 'counter';
7 // Key initial value
8 Integer counterInitValue = 0;
9 // Session partition object
10 Cache.SessionPartition sessionPartition;
11
12 // Constructor of the controller for the Visualforce page.
13 public SessionPartitionController() {
14 }
15
16 // Adds counter value to the cache.
17 // This method is called when the Visualforce page loads.
18 public void init() {
19 // Create the partition instance based on the partition name
20 sessionPartition = getPartition();
21
22 // Add counter to the cache with an initial value
23 // or increment it if it's already there.
24 if (!sessionPartition.contains(counterKeyInput)) {
25 sessionPartition.put(counterKeyInput, counterInitValue);
26 } else {
27 sessionPartition.put(counterKeyInput, getCounter() + 1);
28 }
29 }
30
31 // Returns the session partition based on the partition name
32 // given in the Visualforce page or the default value.
33 private Cache.SessionPartition getPartition() {
34 if (sessionPartition == null) {
35 sessionPartition = Cache.Session.getPartition(partitionInput);
36 }
37
38 return sessionPartition;
39 }
40
41 // Return counter from the cache.
42 public Integer getCounter() {
43 return (Integer)getPartition().get(counterKeyInput);
44 }
45
46 // Invoked by the Submit button to save input values
47 // supplied by the user.
48 public PageReference save() {
49 // Reset the initial key value in the cache
50 getPartition().put(counterKeyInput, counterInitValue);
51
52 return null;
53 }
54
55 // Method invoked by the Rerender button on the Visualforce page.
56 // Updates the values of various cached values.
57 // Increases the values of counter and the MyData counter if those
58 // cache values are still in the cache.
59 public PageReference go() {
60 // Get the partition object
61 sessionPartition = getPartition();
62 // Increase the cached counter value or set it to 0
63 // if it's not cached.
64 if (sessionPartition.contains(counterKeyInput)) {
65 sessionPartition.put(counterKeyInput, getCounter() + 1);
66 } else {
67 sessionPartition.put(counterKeyInput, counterInitValue);
68 }
69
70 return null;
71 }
72
73 // Method invoked by the Remove button on the Visualforce page.
74 // Removes the datetime cached value from the session cache.
75 public PageReference remove() {
76 getPartition().remove(counterKeyInput);
77
78 return null;
79 }
80
81 // Get and set methods for accessing variables
82 // that correspond to the input text fields on
83 // the Visualforce page.
84 public String getPartitionInput() {
85 return partitionInput;
86 }
87
88 public String getCounterKeyInput() {
89 return counterKeyInput;
90 }
91
92 public Integer getCounterInitValue() {
93 return counterInitValue;
94 }
95
96 public void setPartitionInput(String partition) {
97 this.partitionInput = partition;
98 }
99
100 public void setCounterKeyInput(String keyName) {
101 this.counterKeyInput = keyName;
102 }
103
104 public void setCounterInitValue(Integer counterValue) {
105 this.counterInitValue = counterValue;
106 }
107}This is the Visualforce page that corresponds to the SessionPartitionController class.
1<apex:page controller="SessionPartitionController" action="{!init}">
2
3 <apex:form >
4 <br/>Partition with Namespace Prefix: <apex:inputText value="{!partitionInput}"/>
5 <br/>Counter Key Name: <apex:inputText value="{!counterKeyInput}"/>
6 <br/>Counter Initial Value: <apex:inputText value="{!counterInitValue}"/>
7 <apex:commandButton action="{!save}" value="Save Key Input Values"/>
8 </apex:form>
9
10 <apex:outputPanel id="output">
11 <br/>Cached Counter: <apex:outputText value="{!counter}"/>
12 </apex:outputPanel>
13
14 <br/>
15 <apex:form >
16 <apex:commandButton id="go" action="{!go}" value="Rerender" rerender="output"/>
17 <apex:commandButton id="remove" action="{!remove}" value="Remove Key" rerender="output"/>
18 </apex:form>
19
20</apex:page>