Newer Version Available
OrgPartition Class
Namespace
Usage
This class extends Cache.Partition and inherits all its non-static methods. Utility methods for creating and validating keys aren’t supported and can be called only from the Cache.Partition parent class. For a list of Cache.Partition methods, see Partition Methods.
To get an org partition, call Cache.Org.getPartition and pass in a fully qualified partition name, as follows.
1Cache.OrgPartition orgPartition = Cache.Org.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.OrgPartition 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 OrgPartitionController {
2
3 // Name of a partition
4 String partitionInput = 'local.myPartition';
5 // Name of the key
6 String counterKeyInput = 'counter';
7 // Key initial value
8 Integer counterInitValue = 0;
9 // Org partition object
10 Cache.OrgPartition orgPartition;
11
12 // Constructor of the controller for the Visualforce page.
13 public OrgPartitionController() {
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 orgPartition = getPartition();
21
22 // Create the partition instance based on the partition name
23 // given in the Visualforce page or the default value.
24 orgPartition = Cache.Org.getPartition(partitionInput);
25
26 // Add counter to the cache with an initial value
27 // or increment it if it's already there.
28 if (!orgPartition.contains(counterKeyInput)) {
29 orgPartition.put(counterKeyInput, counterInitValue);
30 } else {
31 orgPartition.put(counterKeyInput, getCounter() + 1);
32 }
33 }
34
35 // Returns the org partition based on the partition name
36 // given in the Visualforce page or the default value.
37 private Cache.OrgPartition getPartition() {
38 if (orgPartition == null) {
39 orgPartition = Cache.Org.getPartition(partitionInput);
40 }
41
42 return orgPartition;
43 }
44
45
46 // Return counter from the cache.
47 public Integer getCounter() {
48 return (Integer)getPartition().get(counterKeyInput);
49 }
50
51 // Invoked by the Submit button to save input values
52 // supplied by the user.
53 public PageReference save() {
54 // Reset the initial key value in the cache
55 getPartition().put(counterKeyInput, counterInitValue);
56
57 return null;
58 }
59
60 // Method invoked by the Rerender button on the Visualforce page.
61 // Updates the values of various cached values.
62 // Increases the values of counter and the MyData counter if those
63 // cache values are still in the cache.
64 public PageReference go() {
65 // Get the org partition object
66 orgPartition = getPartition();
67 // Increase the cached counter value or set it to 0
68 // if it's not cached.
69 if (orgPartition.contains(counterKeyInput)) {
70 orgPartition.put(counterKeyInput, getCounter() + 1);
71 } else {
72 orgPartition.put(counterKeyInput, counterInitValue);
73 }
74
75 return null;
76 }
77
78 // Method invoked by the Remove button on the Visualforce page.
79 // Removes the datetime cached value from the org cache.
80 public PageReference remove() {
81 getPartition().remove(counterKeyInput);
82
83 return null;
84 }
85
86 // Get and set methods for accessing variables
87 // that correspond to the input text fields on
88 // the Visualforce page.
89 public String getPartitionInput() {
90 return partitionInput;
91 }
92
93 public String getCounterKeyInput() {
94 return counterKeyInput;
95 }
96
97 public Integer getCounterInitValue() {
98 return counterInitValue;
99 }
100
101 public void setPartitionInput(String partition) {
102 this.partitionInput = partition;
103 }
104
105 public void setCounterKeyInput(String keyName) {
106 this.counterKeyInput = keyName;
107 }
108
109 public void setCounterInitValue(Integer counterValue) {
110 this.counterInitValue = counterValue;
111 }
112}This is the Visualforce page that corresponds to the OrgPartitionController class.
1<apex:page controller="OrgPartitionController" 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>