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

Caching contents of a sidebar block/widget

Discussion in 'XenForo Development Discussions' started by SneakyDave, May 12, 2016.

  1. SneakyDave

    SneakyDave Well-Known Member

    I have a widget that I want to cache the contents of to save on db requests. Is there is good place to find out how to do that, or a good resource that helps explain any best practices to use?

    I was looking at Widget Framework, but that looks like it's more of an all-in-one cache solution just for it's own widgets, so I don't think I need it that complicated, but who knows?
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Is it user specific data or globally the same for all users?
     
  3. SneakyDave

    SneakyDave Well-Known Member

    Globally the same for all users.
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Just use the XF simple cache system.

    XenForo_Application::setSimpleCacheData('key', 'value');

    XenForo_Application::getSimpleCacheData('key');
     
    Snog and SneakyDave like this.
  5. SneakyDave

    SneakyDave Well-Known Member

    GET OUT! It cannot be that sample.

    Can the cache time limit/expiration be explicity set? Never mind, I'll take a look at the methods.
     
    Chris D likes this.
  6. Snog

    Snog Well-Known Member

    It is. :D

    I use it in a couple of add-ons where the data changes infrequently.
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    Once it's in the cache, it's in there permanently. So, you first of all need to ensure that you take care of cache invalidation yourself. Second of all you also should unset any data on add on uninstall (just set false as the value).
     
    SneakyDave likes this.
  8. Snog

    Snog Well-Known Member

    While I'm sure you wouldn't do it, I want to throw this out there. And @Chris D please correct me if I'm wrong.

    The simple cache is not an infinite cache and can run out of room, so don't store huge amounts of data in there. I think it's best suited to small portions of data that would require many database reads, or a larger amount of time to compile.
     
  9. SneakyDave

    SneakyDave Well-Known Member

    Yes, I understand, it's just a small amount of data that I don't want to get by using a database call on each page refresh. Thanks Snog.
     
    Snog likes this.
  10. SneakyDave

    SneakyDave Well-Known Member

    Wow, just wow, thought I was going to spend hours trying to get it figured out, and just took me 20 minutes to come up with it, including a configurable invalidation routine.
     
    Chris D and Snog like this.
  11. Chris D

    Chris D XenForo Developer Staff Member

    The cache is a value which is serialised into a field inside the xf_data_registry table. The field is a BLOB which is around 16MB IIRC.

    That's a considerable chunk of data. But yeah certainly not infinite by any means. That's why I said make sure you remove it on add on uninstall and certainly only cache what you need. It would take some effort to generate that amount of data.

    User specific data for example would be better cached in the user's session.
     
    SneakyDave and Snog like this.
  12. SneakyDave

    SneakyDave Well-Known Member

    Is the user cache just as easy to use, or is there a good place to see an example of it?
     
  13. Chris D

    Chris D XenForo Developer Staff Member

    Pretty much similar.

    XenForo_Application::getSession()->set('key', 'value');

    XenForo_Application::getSession()->get('key');
     
    SneakyDave likes this.

Share This Page