Org クラス
名前空間
使用方法
キャッシュキー形式
次の表は、put、get、contains など、このクラスの一部のメソッドで取るキーパラメータの形式の一覧です。
| キー形式 | 説明 |
|---|---|
| namespace.partition.key | 完全修飾されたキー名。 |
| key | namespace.partition プレフィックスが省略された場合、デフォルトとしてマークされたパーティションを参照します。 |
| local.partition.key | 組織に名前空間が定義されていない場合、local プレフィックスを使用して組織の名前空間を参照します。組織に名前空間が定義されている場合も、local プレフィックスはその組織の名前空間を参照します。 |
例
このクラスは、サンプル Visualforce ページのコントローラです (後続のコードサンプルを参照)。Visualforce ページが action 属性によって読み込まれたときに呼び出す init() メソッドによって、キャッシュ値は最初にキャッシュに追加されます。キャッシュキーには、namespace.partition プレフィックスは含まれません。キーはすべて組織のデフォルトパーティションを参照します。このサンプルを実行するには、パーティションを作成してデフォルトとしてマークします。
Visualforce ページには 4 つの出力コンポーネントが含まれます。これらのコンポーネントは、コントローラの get メソッドをコールし、このメソッドがキャッシュから日付、MyData 内部クラスに基づくデータ、カウンタ、テキスト値、リストの各値を返します。リストのサイズも返します。
Visualforce ページには 2 つのボタンも含まれます。[Rerender (再表示)] ボタンはコントローラの go() メソッドを呼び出します。このメソッドは、キャッシュのカウンタとカスタムデータの値を増やします。[Rerender (再表示)] をクリックすると、毎回 2 つのカウンタが 1 ずつ増えます。go() メソッドは、キャッシュからこれらのカウンタの値を取得し、その値を 1 ずつ増やしてキャッシュに再度保存します。
[Remove datetime Key (日時キーの削除)] ボタンは、キャッシュから日時値を (キー datetime で) 削除します。その結果、ページ上の [Cached datetime: (キャッシュ日時:)] の横にある値がクリアされます。
1public class OrgCacheController {
2
3 // Inner class.
4 // Used as the data type of a cache value.
5 class MyData {
6 public String value { get; set; }
7 public Integer counter { get; set; }
8
9 public MyData(String value) {
10 this.value = value;
11 this.counter = 0;
12 }
13
14 public void inc() {
15 counter++;
16 }
17
18 override public String toString() {
19 return this.value + ':' + this.counter;
20 }
21 }
22
23 // Apex List.
24 // Used as the data type of a cached value.
25 private List<String> numbers =
26 new List<String> { 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE' };
27
28 // Constructor of the controller for the Visualforce page.
29 public OrgCacheController() {
30 }
31
32 // Adds various values to the cache.
33 // This method is called when the Visualforce page loads.
34 public void init() {
35 // All key values are not qualified by the namespace.partition
36 // prefix because they use the default partition.
37
38 // Add counter to the cache with initial value of 0
39 // or increment it if it's already there.
40 if (!Cache.Org.contains('counter')) {
41 Cache.Org.put('counter', 0);
42 } else {
43 Cache.Org.put('counter', getCounter() + 1);
44 }
45
46 // Add the datetime value to the cache only if it's not already there.
47 if (!Cache.Org.contains('datetime')) {
48 DateTime dt = DateTime.now();
49 Cache.Org.put('datetime', dt);
50 }
51
52 // Add the custom data to the cache only if it's not already there.
53 if (!Cache.Org.contains('data')) {
54 Cache.Org.put('data', new MyData('Some custom value'));
55 }
56
57 // Add a list of number to the cache if not already there.
58 if (!Cache.Org.contains('list')) {
59 Cache.Org.put('list', numbers);
60 }
61
62 // Add a string value to the cache if not already there.
63 if (!Cache.Org.contains('output')) {
64 Cache.Org.put('output', 'Cached text value');
65 }
66 }
67
68 // Return counter from the cache.
69 public Integer getCounter() {
70 return (Integer)Cache.Org.get('counter');
71 }
72
73 // Return datetime value from the cache.
74 public String getCachedDatetime() {
75 DateTime dt = (DateTime)Cache.Org.get('datetime');
76 return dt != null ? dt.format() : null;
77 }
78
79 // Return cached value whose type is the inner class MyData.
80 public String getCachedData() {
81 MyData mydata = (MyData)Cache.Org.get('data');
82 return mydata != null ? mydata.toString() : null;
83 }
84
85 // Return output from the cache.
86 public String getOutput() {
87 return (String)Cache.Org.get('output');
88 }
89
90 // Return list from the cache.
91 public List<String> getList() {
92 return (List<String>)Cache.Org.get('list');
93 }
94
95 // Method invoked by the Rerender button on the Visualforce page.
96 // Updates the values of various cached values.
97 // Increases the values of counter and the MyData counter if those
98 // cache values are still in the cache.
99 public PageReference go() {
100 // Increase the cached counter value or set it to 0
101 // if it's not cached.
102 if (Cache.Org.contains('counter')) {
103 Cache.Org.put('counter', getCounter() + 1);
104 } else {
105 Cache.Org.put('counter', 0);
106 }
107
108 // Get the custom data value from the cache.
109 MyData d = (MyData)Cache.Org.get('data');
110 // Only if the data is already in the cache, update it.
111 if (Cache.Org.contains('data')) {
112 d.inc();
113 Cache.Org.put('data', d);
114 }
115
116 return null;
117 }
118
119 // Method invoked by the Remove button on the Visualforce page.
120 // Removes the datetime cached value from the org cache.
121 public PageReference remove() {
122 Cache.Org.remove('datetime');
123
124 return null;
125 }
126}これは、OrgCacheController クラスに対応する Visualforce ページです。
1<apex:page controller="OrgCacheController" action="{!init}">
2
3 <apex:outputPanel id="output">
4 <br/>Cached datetime: <apex:outputText value="{!cachedDatetime}"/>
5 <br/>Cached data: <apex:outputText value="{!cachedData}"/>
6 <br/>Cached counter: <apex:outputText value="{!counter}"/>
7 <br/>Output: <apex:outputText value="{!output}"/>
8 <br/>Repeat: <apex:repeat var="item" value="{!list}">
9 <apex:outputText value="{!item}"/>
10 </apex:repeat>
11 <br/>List size: <apex:outputText value="{!list.size}"/>
12 </apex:outputPanel>
13
14 <br/><br/>
15 <apex:form >
16 <apex:commandButton id="go" action="{!go}" value="Rerender" rerender="output"/>
17 <apex:commandButton id="remove" action="{!remove}" value="Remove datetime Key" rerender="output"/>
18 </apex:form>
19
20</apex:page>これは [Rerender (再表示)] ボタンを 2 回クリックした後のページの出力です。このサンプルを実行する前に counter という名前のキーがすでにキャッシュにある場合は、カウンタ値が異なる可能性があります。
1Cached datetime:8/11/2015 1:58 PM
2Cached data:Some custom value:2
3Cached counter:2
4Output:Cached text value
5Repeat:ONE TWO THREE FOUR FIVE
6List size:5Org メソッド
Org のメソッドは次のとおりです。すべてのメソッドが静的です。
get(key)
署名
public static Object get(String key)
戻り値
型: Object
キャッシュ値が汎用オブジェクト種別として返されます。戻り値を適切な型にキャストしてください。
使用方法
Cache.Org.get() はオブジェクトを返すため、戻り値を使いやすいように特定の型にキャストしてください。
1// Get a cached value
2Object obj = Cache.Org.get('ns1.partition1.orderDate');
3// Cast return value to a specific data type
4DateTime dt2 = (DateTime)obj;Cache.Org.get() コールで参照されたキーが見つからない場合、null が返されます。
get(cacheBuilder, key)
署名
public static Object get(System.Type cacheBuilder, String key)
パラメータ
- cacheBuilder
- 型: System.Type
- CacheBuilder インターフェースを実装する Apex クラス。
- 鍵
- 型: String
- cacheBuilder パラメータに対応するクラス名と組み合せてキャッシュ値を一意に識別する文字列値 (大文字と小文字を区別)。
戻り値
型: Object
キャッシュ値が汎用オブジェクト種別として返されます。戻り値を適切な型にキャストしてください。
使用方法
Cache.Org.get(cacheBuilder, key) はオブジェクトを返すため、戻り値を使いやすいように特定の型にキャストしてください。
1return ((DateTime)Cache.Org.get(DateCache.class, 'datetime')).format();getPartition(partitionName)
署名
public static cache.OrgPartition getPartition(String partitionName)
パラメータ
- partitionName
- 型: String
- 名前空間で修飾されたパーティション名 (namespace.partition など)。
戻り値
例
組織のパーティションを取得したら、パーティションのキャッシュ値を追加および取得できます。
1// Get partition
2Cache.OrgPartition orgPart = Cache.Org.getPartition('myNs.myPartition');
3// Retrieve cache value from the partition
4if (orgPart.contains('BookTitle')) {
5 String cachedTitle = (String)orgPart.get('BookTitle');
6}
7
8// Add cache value to the partition
9orgPart.put('OrderDate', Date.today());
10
11// Or use dot notation to call partition methods
12String cachedAuthor = (String)Cache.Org.getPartition('myNs.myPartition').get('BookAuthor');put(key, value, visibility)
署名
public static void put(String key, Object value, Cache.Visibility visibility)
パラメータ
- key
- 型: String
- キャッシュ値を一意に識別する文字列値 (大文字と小文字を区別)。キー名の形式についての詳細は、「使用方法」を参照してください。
- value
- 型: Object
- キャッシュに保存する値。キャッシュ値は逐次化可能にする必要があります。
- visibility
- 型: Cache.Visibility
- キャッシュ値を使用できるのが、同じ名前空間内で���行される Apex コードのみか、任意の名前空間から実行される Apex コードかを示します。
戻り値
型: void
put(key, value, ttlSecs)
署名
public static void put(String key, Object value, Integer ttlSecs)
パラメータ
戻り値
型: void
put(key, value, ttlSecs, visibility, immutable)
署名
public static void put(String key, Object value, Integer ttlSecs, cache.Visibility visibility, Boolean immutable)
パラメータ
- key
- 型: String
- キャッシュ値を一意に識別する文字列値 (大文字と小文字を区別)。キー名の形式についての詳細は、「使用方法」を参照してください。
- value
- 型: Object
- キャッシュに保存する値。キャッシュ値は逐次化可能にする必要があります。
- ttlSecs
- 型: Integer
- キャッシュ値を組織キャッシュに保持しておく時間 (秒数)。最大値は 172,800 秒 (48 時間) です。最小値は 300 秒 (5 分) です。デフォルト値は 86,400 秒 (24 時間) です。
- visibility
- 型: Cache.Visibility
- キャッシュ値を使用できるのが、同じ名前空間内で実行される Apex コードのみか、任意の名前空間から実行される Apex コードかを示します。
- immutable
- 型: Boolean
- キャッシュ値を別の名前空間によって上書きできるか (false)、否か (true) を示します。
戻り値
型: void
remove(cacheBuilder, key)
署名
public static Boolean remove(System.Type cacheBuilder, String key)
パラメータ
- cacheBuilder
- 型: System.Type
- CacheBuilder インターフェースを実装する Apex クラス。
- 鍵
- 型: String
- cacheBuilder パラメータに対応するクラス名と組み合せてキャッシュ値を一意に識別する文字列値 (大文字と小文字を区別)。