XLL+ Class Library (6.3)

CXlOperCache Class

Stores the results of any add-in function in a map keyed by the collection of all inputs to the add-in function.

class CXlOperCache

Overview

The CXlOperCache class can be used to store the results of any add-in function in a map keyed by the collection of all inputs to the add-in function.

This behavior can greatly improve add-in function performance, since functions are calculated only once for any given set of inputs, however many times they are requested. Since Excel often asks the same question many times, recalculating cells whose inuts have not changed, the impact of a cache on the performance of a relatively expensive add-in function can be considerable.

Using the cache

You can make your add-in function use the cache in one easy step with the XLL+ Function Wizard: when you create the function, check the Cache results feature on the Features tab.

Code will be generated in the wrapper functions which inspects the cache and returns if a result is found. Just before the function exits, the cache will be updated automatically with the function's return value.

Persistence

You can save the cache to disk when the XLL closes and restore it each time the XLL is opened.

You should implement the OnXllOpenEx() and OnXllClose() overrideable methods of the application class, to restore and save the cache.

The code below shows how to do this:

CopyC++
BOOL CAvgOptApp::OnXllOpenEx()
{
    // Restore cache 
    if (CXlOperCache::GetUserCacheFilePath(m_strCacheFile))
        m_cache.Restore(m_strCacheFile);

    return TRUE;
}

void CAvgOptApp::OnXllClose()
{
    m_cache.Save(m_strCacheFile);
}

Sample applications

You can find a sample using the CXlCacheBinding class below:

Requirements

Header: xlserialize.h

See Also

CXlOperCache Methods | xlserialize.h | CXlCacheBinding class<C> | CXlInputKey class | CXlSerialData class | Cache sample | Function property: CacheResults