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

Caching Data in Addons

Discussion in 'XenForo Development Discussions' started by bambua, Oct 10, 2011.

  1. bambua

    bambua Well-Known Member

    Rather than reinventing the wheel, in an addon I'm updating I'm building in caching of profile posts. I searched through the code and found getSimpleCacheData and setSimpleCacheData, they are doing the job but I saw that they post and pull the data from the data_registry table. Yes it saves queries doing it this way as it only checks for new posts when the cache needs to be updated, but is there a better way? Another method built in to the software?

    Right now here's how i'm doing it:
    PHP:
                $data XenForo_Application::getSimpleCacheData("RCBDRecentStatus_comments_array");
                if (!
    $data) {
                    
    $commentsArray $db->fetchAll($db->limit("SELECT * FROM  xf_profile_post_comment WHERE profile_post_id in(" $matches ") ORDER BY profile_post_id DESC, comment_date"$numStatusShown));
                    
    XenForo_Application::setSimpleCacheData("RCBDRecentStatus_comments_array"$commentsArray);
                } else {
                    
    $commentsArray $data;
                }
    Thanks!
     
    Floris likes this.
  2. xfrocks

    xfrocks Well-Known Member

    This looks like you are going to store a lot data in the simple cache? I don't think that's a good idea because XenForo may use the simple cache more some point in the future and that may break your add-on. Sometime I use simple cache but for relatively small data only.

    If you have to cache your comments (I don't understand why you want to cache the comments? They are used that much?) you should use XenForo_Model_DataRegistry directly.
     
  3. bambua

    bambua Well-Known Member

    Actually the most it's ever going to be stored is about 10 array elements. The issue I was getting around was it seemed silly to load the queries everytime the forum home is loaded if the data doesn't change that often. It was literally a savings of like 6 queries per home page load...multiple that out times quite a few users and that's a lot of queries.

    I'll take a look at XenForo_Model_DataRegistry, I didn't hunt through there yet.
     

Share This Page