1. 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?

Discussion in 'XenForo Development Discussions' started by cclaerhout, Apr 24, 2012.

  1. cclaerhout

    cclaerhout Well-Known Member

    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?
     
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    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.
     
    Eagle, Marcus, bousaid and 5 others like this.
  3. cclaerhout

    cclaerhout Well-Known Member

    Oh........... it's working great !!! So many hours spent to find a solution and after your help 2 minutes to make it work; Can I kiss you? ^^
    THANK YOU!
     
    HeadHodge likes this.
  4. luutruong

    luutruong Active Member

    I give a array to simpleCache but i cant call data from it.
    PHP:
    $attachmentTotals XenForo_Application::getSimpleCacheData('attachmentTotals');
    Return nothing
     
  5. cclaerhout

    cclaerhout Well-Known Member

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

    XenForo_Application::setSimpleCacheData('attachmentTotals', $yourArray);
     
  6. luutruong

    luutruong Active Member

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

    cclaerhout Well-Known Member

    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?
     
  8. luutruong

    luutruong Active Member

    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;
        }
     
    }
     
  9. cclaerhout

    cclaerhout Well-Known Member

    Sorry if my question seems silly but are you sure the setSimpleCacheData function has been executed before?
     
  10. luutruong

    luutruong Active Member

    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)
     
  11. cclaerhout

    cclaerhout Well-Known Member

    ... 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.
     
    luutruong likes this.
  12. luutruong

    luutruong Active Member

    Still not working... Im just send for you a PC (Full code...)
     
  13. MattW

    MattW Well-Known Member

    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.
     
    bousaid and luutruong like this.
  14. luutruong

    luutruong Active Member

    Dont need.. But im learning give data to cache... but i dont understand why not work?
     
  15. cclaerhout

    cclaerhout Well-Known Member

    I've checked and didn't find anything wrong... May be you should put the link here so someone else can check.
     
    luutruong likes this.
  16. luutruong

    luutruong Active Member

    Now..it working... Im rebuilt again and work ;)
     
  17. bousaid

    bousaid Well-Known Member

    working great .. Thank you cclaerhout, luutruong :D
     
  18. cclaerhout

    cclaerhout Well-Known Member

    I didn't see your message. What was the problem after all ?
    You should thank Jake, he's the one who has explained the method(s) to cache datas.
     
    bousaid likes this.
  19. luutruong

    luutruong Active Member

    I dont know. Maybe i repair too much cache or code in that Addon. Then i remove that addon and rebuild again (same code) and run... ;)
     
  20. W1zzard

    W1zzard Well-Known Member

    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).
     

Share This Page