SysGlobalObjectCache class in AX 2012 [x++]

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
class SR_CurrentWorkerDetails
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.
    SysGlobalObjectCache    sgoc;
    container               result;
    userId                  currentUserId = curUserId(); // Key
    recId                   workerId;
    HcmWorker               hcmWorker;
    DirPersonUser           dirPersonUser;
    // scope

    #DEFINE.Values(workerId) // Caching only Worker Id

    //Try put pull from cache first
    if (classfactory)
        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
        // singleton
        sgoc =  new SysGlobalObjectCache();
    result = sgoc.find(#CURRENT_WORKER_ID, [currentUserId]); // use scope and key to find the value cached
    if(result != conNull())
        [#Values] = result;
        return workerId;

    //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]);

    return workerId;

PriceDisc class uses the same concept – to find the prices and discounts in AX 2012. Refer to priceDisc class for more details.
Happy Dax6ng,

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.