• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

cclaerhout

Well-known member
#1
The easiest way is of course to create options in the xen options section, but how can I do it when I create options outside this section? In others words, my goal is to get my datas by using the following command:
Code:
$options = XenForo_Application::get('options');
In my addon, I've got my model and my controlleradmin files.
In my model, I've added a regenerate cache options like this one:
Code:
    public function rebuildMyAddonCache()
    {
        $xenoptions =  $this->_getDataRegistryModel()->get('options');
        if (!is_array($xenoptions))
        {
            $xenoptions =  $this->rebuildOptionCache();
        }
 
        $myaddonoptions['Addon'] = $this->getAllMyAddonDatas();
        $mergedoptions = array_merge($xenoptions, $markitupoptions);
       
        $this->_getDataRegistryModel()->set('options', $mergedoptions);
 
        return $myaddonoptions;
    }
And in my controller admin save function, I've added a callback to the above rebuildMyAddonCache function. It's working but of course if I go back to the XenForo Options section and save one option, my datas won't be there. I would need to modify the XenForo_Model_Option::rebuildOptionCache(), but it's a model, so no listener there.

I might have this solution too, try to get my datas doing this:
Code:
$options = XenForo_Application::get('customized');
Which means to create a new application and implies to modify XenForo_Application::loadDefaultData() which may be, I'm not sure, can be listened with the init_dependencies listener.

Does someone have a solution? Is there an easy way to extend cached datas in options or do I have to do it another way?
 

Jake Bunce

XenForo moderator
Staff member
#2
I don't know the nature of the data you need to store, but the simpleCache might be good for you:

Code:
// STORE DATA IN THE SIMPLE CACHE
XenForo_Application::setSimpleCacheData('mystuff', $mydata);

// RETRIEVE DATA FROM THE SIMPLE CACHE
$mydata = XenForo_Application::getSimpleCacheData('mystuff');

// DELETE DATA FROM THE SIMPLE CACHE (good for uninstall routines)
XenForo_Application::setSimpleCacheData('mystuff', false);
The simpleCache is stored in the registry and is automatically available on every execution. It is good for storing information that you need on every page load.
 

luutruong

Active member
#4
I give a array to simpleCache but i cant call data from it.
PHP:
$attachmentTotals = XenForo_Application::getSimpleCacheData('attachmentTotals');
Return nothing
 

cclaerhout

Well-known member
#5
I give a array to simpleCache but i cant call data from it.
PHP:
$attachmentTotals = XenForo_Application::getSimpleCacheData('attachmentTotals');
Return nothing
Are you sure you use the same "code" (attachementTotals) to set the simple cache before ?

XenForo_Application::setSimpleCacheData('attachmentTotals', $yourArray);
 

luutruong

Active member
#6
Are you sure you use the same "code" (attachementTotals) to set the simple cache before ?

XenForo_Application::setSimpleCacheData('attachmentTotals', $yourArray);
Sure !!
PHP:
$counter = array(
            'total' => $attachmentModel->countAttachments(),
            'document' => $attachmentModel->getTotalDocument(),
            'size' => $attachmentModel->getAttachmentSize(),
            'download' => $attachmentModel->getTotalDownload()
        );
       
        XenForo_Application::setSimpleCacheData('attachmentTotals', $counter);
What is wrong?
 

cclaerhout

Well-known member
#7
Sure !!
PHP:
$counter = array(
            'total' => $attachmentModel->countAttachments(),
            'document' => $attachmentModel->getTotalDocument(),
            'size' => $attachmentModel->getAttachmentSize(),
            'download' => $attachmentModel->getTotalDownload()
        );
     
        XenForo_Application::setSimpleCacheData('attachmentTotals', $counter);
What is wrong?
In your code, nothing. And if you use this command just after:
PHP:
$attachmentTotals = XenForo_Application::getSimpleCacheData('attachmentTotals');
You got nothing ? Even not the array with its keys?
 

luutruong

Active member
#8
In your code, nothing. And if you use this command just after:
PHP:
$attachmentTotals = XenForo_Application::getSimpleCacheData('attachmentTotals');
You got nothing ? Even not the array with its keys?
yes. I cant re-call that.
PHP:
<?php
 
class Nobita_AttachmentStatistics_ControllerPublic_AttachStats extends XFCP_Nobita_AttachmentStatistics_ControllerPublic_AttachStats
{
    public function actionIndex()
    {
        $response = parent::actionIndex();
     
        $attachmentTotals = XenForo_Application::getSimpleCacheData('attachmentTotals');
     
        //var_dump($attachmentTotals);die();
        $response->params['attachmentTotals'] = $attachmentTotals
     
        var_dump($response->params['attachmentTotals']);die();
        return $response;
    }
 
}
 
#10
Sorry if my question seems silly but are you sure the setSimpleCacheData function has been executed before?
Yes. I manual executed it (Run cronEntry)
Export xf_data_registry i got (Row: simpleCache)
Code:
s:16:"attachmentTotals";a:4:{s:5:"total";a:1:{s:5:"total";i:14;}s:8:"document";a:1:{s:8:"document";i:1;}s:4:"size";s:6:"736797";s:8:"download";s:2:"94";
Sorry my english very bad :( (Hope you understand)
 

cclaerhout

Well-known member
#11
... your English is good and probably better than mine.
I've just thought about something. May be, and I said may be, the problem comes from the capital letter. I think I had the same problem before with a private addon. So instead of attachmentTotals, use attachment_totals and check if it's working.
 
#12
... your English is good and probably better than mine.
I've just thought about something. May be, and I said may be, the problem comes from the capital letter. I think I had the same problem before with a private addon. So instead of attachmentTotals, use attachment_totals and check if it's working.
Still not working... Im just send for you a PC (Full code...)
 

MattW

Well-known member
#13
Does the data need to be available on every page? Would it be better to put the results in the data registry? I've recently done an attachment stats add-on, and I'm storing the results in the data registry.
 
#14
Does the data need to be available on every page? Would it be better to put the results in the data registry? I've recently done an attachment stats add-on, and I'm storing the results in the data registry.
Dont need.. But im learning give data to cache... but i dont understand why not work?
 

W1zzard

Well-known member
#20
Sorry for reviving this thread.

Is there a way for addons to store data, not in the database, with an expire timestamp? (I'm thinking memcached).