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

Caching data in add-ons

Discussion in 'XenForo Development Discussions' started by Chris D, Sep 27, 2012.

  1. Chris D

    Chris D XenForo Developer Staff Member

    I have completed some work which essentially adds a view counter to profile pages.

    This is all working great.

    Part of the request is to display the top X most viewed profiles in a sidebar block.

    Now, I appreciate I could make use of the caching methods in XenPorta or Widget Framework, but I plan to just add the block to the sidebar using a template hook.

    So my question is this.

    If I have a query, like the following which generates the information required, what's the best way to cache it?

    FROM xf_user_profile AS user_profile INNER JOIN xf_user AS user ON
    (user.user_id = user_profile.user_id)​
    ORDER BY view_count DESC LIMIT 10
    So how do I execute that query into the cache? How do I then retrieve it from the cache? How do I handle the cache expiring and can I adjust the expiry time? How do I only execute the query once the cache has expired?

    Any advice gratefully receved!
  2. Lawrence

    Lawrence Well-Known Member

    I would create a cron job in XF that runs about once an hour that executes that query and places the results in the data registry. Pulling the data out of the registry on every page load does not add any queries.
    Chris D likes this.
  3. Chris D

    Chris D XenForo Developer Staff Member

    Mr. Zero Queries, that sounds like a great idea. (y)
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  5. Chris D

    Chris D XenForo Developer Staff Member

  6. Chris D

    Chris D XenForo Developer Staff Member

  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    It's a mediumblob field which maxes at 16777215 characters. The default max_allowed_packet on many servers is 1MB (1048576) so anything smaller than that should avoid errors. 10 user records won't even come close.
    Chris D likes this.
  8. Chris D

    Chris D XenForo Developer Staff Member

    Both, how long does data stay in the DataRegistry for? Or does it never expire?


    I think I have answered my own question, from XenForo_Model_DataRegistry. So, permanent, yes?:

    * Model that represents data in the data registry system. This system
    * is a generally permanent cache of data that can be read from the
    * cache system or out of the database.
    * Data stored here will be automatically serialized and unserialized
    * as it is retrieved.
    * @package XenForo_Core
    I also see that there is a method for deleting the data so I will use that in any uninstall routine to clear that data.
  9. Lawrence

    Lawrence Well-Known Member

    It is permanent, unless something unexpected happened.

    Set your registry key during the install:

    XenForo_Model::create('XenForo_Model_DataRegistry')->set('mostViewed', array());

    and remove it during uninstall:

    cclaerhout and Chris D like this.
  10. Chris D

    Chris D XenForo Developer Staff Member

    Great, thank you.

    I see another potentially useful tool is the simple cache, but adding data to that makes it available on every single page load automagically.

    Is that also permanent?
  11. Lawrence

    Lawrence Well-Known Member

    It's been a while since I played with the simpleCache, and do not know much about it except how to set and remove your own cache keys, so hopefully Jake can provide more details on using the simpleCache. :)
  12. Chris D

    Chris D XenForo Developer Staff Member

    Thanks Lawrence :)

    Pretty confident on how to use it, based on some posts I've already seen by Jake, but just wondered about how permanent it was.

    It seems that there is a simple cache row in the xf_data_registry table. I wonder if that's where it is permanently stored? Or if it's stored somewhere else, but recycled periodically?

    Thanks both for your help.
  13. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    simpleCache is permanent. If you need to access the data on every page then the simpleCache is good because it is already loaded on every page so it doesn't add any queries. I use the simpleCache for Nodes As Tabs to store the tab info.
    Chris D likes this.
  14. cclaerhout

    cclaerhout Well-Known Member

    Jake also advised me to use the simpleCache when I was writing the Markitup addon. It's working great and... it's so easy to use.
    Chris D likes this.

Share This Page