How to store an object state using Global Cache

Occasionally, you have a class that will be called multiple times within the same process. In cases like that, would be a great alternative to use the Global cache framework to improve performance. Let’s consider the following scenario:

You have a class that inserts data on a temporary table, but you need to retrieve that data even after the disposal.

You might want to do this quick change in your construct method. Plain and simple, you just have to get your class object from the Global cache instead of instantiate a new one again, as you can see here:

public static void main(Args args)
{
    AxForceCustomerRanking     axForceCustomerRanking;

    if (appl.globalCache().isSet(classStr(AxForceCustomerRanking), sessionId()))
    {
        axForceCustomerRanking = appl.globalCache().get(classStr(AxForceCustomerRanking), sessionId());
        axForceCustomerRanking.showResult();
    }
    else
    {
        axForceCustomerRanking = AxForceCustomerRanking::construct();
        axForceCustomerRanking.run();
    }
}

You also need to put your object inside the Global cache as well:

public void run()
{
    this.getCustomers();
    
    this.showResult();

    appl.globalCache().set(classStr(axForceCustomerRanking), sessionId(), this);
}

Doing this, considering our example, the class will be able to show the inserted data of the temporary table without the need of executing the query and the other methods again:

result

Keep in mind that the class object will remain stored at the global cache until the user session is over.

I hope that it can be useful for you guys!