XLL+ Class Library

CXlOperCache Class

[C++]
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 command on the Function menu.

Code will be generated at the top of your function which inspects the cache and returns if a result is found. Just before your function exits, the cache will be updated automatically with the function's return value. This implementation makes use of the CXlCacheBinding template class.

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:

BOOL CAvgOptApp::OnXllOpenEx()
{
    // Calculate the full path of the cache data file.
    // This is the same as the add-in, with the extension "dat".
    m_strCacheFile = CXllApp::GetXllName();
    m_strCacheFile = m_strCacheFile.Left(m_strCacheFile.GetLength() - 3) + "dat";

    // Restore cache
    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 attribute: cacheresults