There is a new class SysGlobalObjectCache that has been introduced in AX 2012, which will help for Global object caching with the help of scope, Key and values.
Three important parameters are:
Scope: A string type that specifies the scope or the owner of the cached object.
Key : A container type that specifies the key to the cached object.
value: A container type that has the object to cache.
Let us work with a class example to help you better understand.
For this post, we will create a class by name SR_CurrentWorkerDetails with a simple public method that will get the current worker
Create a new public method getCurrentWorker() as shown below.
This method uses scope as “CurrentWorker”, Key as curUserId() and value as “WorkerId” to be cached.
Some important methods:
insert – insert the values in to cache if it’s not already cached/inserted
find – used find method by passing scope and key to get the cached values
remove : sysGlobalObjectCache.remove(scope, key);
In order to clear the cached values based on scope – use
classfactory.globalObjectCache().clear(#CURRENT_WORKER_ID); // scope to be passed as per below example
To clear all caches – we can use
SysGlobalObjectCache::clearAllCaches(); // You can call this method on client/server to clear caches on client or server
Refer to SysFlushAOD class methods to clear caches on client and server:
– clearGlobalObjectCaches() – client
– clearServerGlobalObjectCaches() – server
public HcmWorkerRecId getCurrentWorker()
// We can easily get current worker from Global::currentWorker() method as well. Below logic is same.
userId currentUserId = curUserId(); // Key
#DEFINE.Values(workerId) // Caching only Worker Id
//Try put pull from cache first
sgoc = classfactory.globalObjectCache();
// Workaround for SysQueryRangeUtil usage under IL
// the class factory is not initialized in the interpreter
// when called from IL.This is OK as the global cache is a kernel
sgoc = new SysGlobalObjectCache();
result = sgoc.find(#CURRENT_WORKER_ID, [currentUserId]); // use scope and key to find the value cached
if(result != conNull())
[#Values] = result;
//Calculate current worker value
select firstonly RecId from hcmWorker
join PersonParty, User from dirPersonUser
where (hcmWorker.Person == dirPersonUser.PersonParty) &&
(dirPersonUser.User == currentUserId);
//Cache current worker value
workerId = hcmWorker.RecId;
sgoc.insert(#CURRENT_WORKER_ID, [currentUserId], [#Values]);
PriceDisc class uses the same concept – to find the prices and discounts in AX 2012. Refer to priceDisc class for more details.