What is the best way to add datas into the cache?

Yes I'm aware of these, is there a "XenForo way" to use the caching mechanism that XF is configured to use?

Obviously addon users don't want to configure cache settings for every addon they are installing.
 
Yes I'm aware of these, is there a "XenForo way" to use the caching mechanism that XF is configured to use?

Obviously addon users don't want to configure cache settings for every addon they are installing.
Bumping this old thread...


The following code will use whatever cache is configured in the XF config file. We use it with memcache as our cache.

PHP:
// Saving data to the configured cache
if($cacheObject = XenForo_Application::getCache())
{
    $cacheObject->save($myData, $cacheId, array(), 7200); // look up the "save" method in the XF code to understand the various params
}

// Loading previously saved data
if($cacheObject = XenForo_Application::getCache())
{
    if($myData = $cacheObject->load($cacheId))
    {
        // $myData is what we saved earlier
    }
}

If you want to store php arrays in the cache, use the serialize php function around $myData when calling save to create a string representation of the array. Then use the unserialize php function on the $myData returned by load to convert the string representation back into a proper php array.
 
Last edited:
Top Bottom