Caching contents of a sidebar block/widget

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?
 

Chris D

XenForo developer
Staff member
Just use the XF simple cache system.

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

XenForo_Application::getSimpleCacheData('key');
 

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

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

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.
 

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.
 

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

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.
 

Chris D

XenForo developer
Staff member
Pretty much similar.

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

XenForo_Application::getSession()->get('key');
 
Top