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. |
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 the default partition in your org. To run this sample, create a partition and mark it as default.
The Visualforce page contains four output components. These components call get methods on the controller that returns the following values from the cache: a date, data based on the MyData inner class, a counter, a text value, and a 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. When 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 datetime Key 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.
public class OrgCacheController { // Inner class. // Used as the data type of a cache value. class MyData { public String value { get; set; } public Integer counter { get; set; } public MyData(String value) { this.value = value; this.counter = 0; } public void inc() { counter++; } override public String toString() { return this.value + ':' + this.counter; } } // Apex List. // Used as the data type of a cached value. private List<String> numbers = new List<String> { 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE' }; // Constructor of the controller for the Visualforce page. public OrgCacheController() { } // Adds various values to the cache. // This method is called when the Visualforce page loads. public void init() { // All key values are not qualified by the namespace.partition // prefix because they use the default partition. // Add counter to the cache with initial value of 0 // or increment it if it's already there. if (!Cache.Org.contains('counter')) { Cache.Org.put('counter', 0); } else { Cache.Org.put('counter', getCounter() + 1); } // Add the datetime value to the cache only if it's not already there. if (!Cache.Org.contains('datetime')) { DateTime dt = DateTime.now(); Cache.Org.put('datetime', dt); } // Add the custom data to the cache only if it's not already there. if (!Cache.Org.contains('data')) { Cache.Org.put('data', new MyData('Some custom value')); } // Add a list of number to the cache if not already there. if (!Cache.Org.contains('list')) { Cache.Org.put('list', numbers); } // Add a string value to the cache if not already there. if (!Cache.Org.contains('output')) { Cache.Org.put('output', 'Cached text value'); } } // Return counter from the cache. public Integer getCounter() { return (Integer)Cache.Org.get('counter'); } // Return datetime value from the cache. public String getCachedDatetime() { DateTime dt = (DateTime)Cache.Org.get('datetime'); return dt != null ? dt.format() : null; } // Return cached value whose type is the inner class MyData. public String getCachedData() { MyData mydata = (MyData)Cache.Org.get('data'); return mydata != null ? mydata.toString() : null; } // Return output from the cache. public String getOutput() { return (String)Cache.Org.get('output'); } // Return list from the cache. public List<String> getList() { return (List<String>)Cache.Org.get('list'); } // Method invoked by the Rerender button on the Visualforce page. // Updates the values of various cached values. // Increases the values of counter and the MyData counter if those // cache values are still in the cache. public PageReference go() { // Increase the cached counter value or set it to 0 // if it's not cached. if (Cache.Org.contains('counter')) { Cache.Org.put('counter', getCounter() + 1); } else { Cache.Org.put('counter', 0); } // Get the custom data value from the cache. MyData d = (MyData)Cache.Org.get('data'); // Only if the data is already in the cache, update it. if (Cache.Org.contains('data')) { d.inc(); Cache.Org.put('data', d); } return null; } // Method invoked by the Remove button on the Visualforce page. // Removes the datetime cached value from the org cache. public PageReference remove() { Cache.Org.remove('datetime'); return null; } }
This is the Visualforce page that corresponds to the OrgCacheController class.
<apex:page controller="OrgCacheController" action="{!init}"> <apex:outputPanel id="output"> <br/>Cached datetime: <apex:outputText value="{!cachedDatetime}"/> <br/>Cached data: <apex:outputText value="{!cachedData}"/> <br/>Cached counter: <apex:outputText value="{!counter}"/> <br/>Output: <apex:outputText value="{!output}"/> <br/>Repeat: <apex:repeat var="item" value="{!list}"> <apex:outputText value="{!item}"/> </apex:repeat> <br/>List size: <apex:outputText value="{!list.size}"/> </apex:outputPanel> <br/><br/> <apex:form > <apex:commandButton id="go" action="{!go}" value="Rerender" rerender="output"/> <apex:commandButton id="remove" action="{!remove}" value="Remove datetime Key" rerender="output"/> </apex:form> </apex:page>
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.
Cached datetime:8/11/2015 1:58 PM Cached data:Some custom value:2 Cached counter:2 Output:Cached text value Repeat:ONE TWO THREE FOUR FIVE List size:5
The following are methods for Org. All methods are static.
public static Object get(String key)
Type: Object
The cached value as a generic object type. Cast the returned value to the appropriate type.
Because Cache.Org.get() returns an object, cast the returned value to a specific type to facilitate use of the returned value.
// Get a cached value Object obj = Cache.Org.get('ns1.partition1.orderDate'); // Cast return value to a specific data type DateTime dt2 = (DateTime)obj;
If a Cache.Org.get() call doesn’t find the referenced key, it returns null.
public static Object get(System.Type cacheBuilder, String key)
Type: Object
The cached value as a generic object type. Cast the returned value to the appropriate type.
Because Cache.Org.get(cacheBuilder, key) returns an object, cast the returned value to a specific type to facilitate use of the returned value.
return ((DateTime)Cache.Org.get(DateCache.class, 'datetime')).format();
public static cache.OrgPartition getPartition(String partitionName)
Type: Cache.OrgPartition
After you get the org partition, you can add and retrieve the partition’s cache values.
// Get partition Cache.OrgPartition orgPart = Cache.Org.getPartition('myNs.myPartition'); // Retrieve cache value from the partition if (orgPart.contains('BookTitle')) { String cachedTitle = (String)orgPart.get('BookTitle'); } // Add cache value to the partition orgPart.put('OrderDate', Date.today()); // Or use dot notation to call partition methods String cachedAuthor = (String)Cache.Org.getPartition('myNs.myPartition').get('BookAuthor');
public static void put(String key, Object value, Cache.Visibility visibility)
Type: void
public static void put(String key, Object value, Integer ttlSecs)
Type: void
public static void put(String key, Object value, Integer ttlSecs, cache.Visibility visibility, Boolean immutable)
Type: void
public static Boolean remove(System.Type cacheBuilder, String key)