SessionPartition クラス
特定のパーティションのセッションキャッシュにあるキャッシュ値を管理するメソッドが含まれます。
名前空間
使用方法
このクラスは、Cache.Partition を拡張し、その非静的メソッドのすべてを継承します。キーを作成および検証するためのユーティリティメソッドはサポートされておらず、Cache.Partition 親クラスからのみコールできます。Cache.Partition メソッドの一覧は、「Partition メソッド」を参照してください。
セッションパーティションを取得するには、次のように Cache.Session.getPartition をコールし、完全修飾されたパーティション名を渡します。
1Cache.SessionPartition sessionPartition = Cache.Session.getPartition('namespace.myPartition');「パーティションメソッドのキャッシュキー形式」を参照してください。
例
このクラスは、サンプル Visualforce ページのコントローラーです (後続のコードサンプルを参照)。このコントローラーは、Cache.SessionPartition のメソッドを使用して特定のパーティションのキャッシュ値を管理する方法を示しています。コントローラーは Visualforce ページからパーティション名、カウンターのキー名、およびカウンターの初期値を入力します。コントローラーにはこれらの入力のデフォルト値が含まれています。Visualforce ページで [Rerender (再表示)] をクリックすると、go() メソッドが呼び出されてカウンターが 1 増加します。[Remove Key (キーを削除)] をクリックすると、カウンターキーがキャッシュから削除されます。カウンターがキャッシュに再度追加されると、カウンター値は初期値にリセットされます。
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}これは、SessionPartitionController クラスに対応する Visualforce ページです。
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>