Caching data in add-ons

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?

Rich (BB code):
SELECT
user.username,​
user.user_id,​
user_profile.view_count​
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!
 
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.
 
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.
Mr. Zero Queries, that sounds like a great idea. (y)
 
On a similar note, how much data can be stored in the data registry? I assume we shouldn't put too much in there. Just wondering if there were any hard limits or at least advisable limits?

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

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

EDIT:

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

Rich (BB code):
/**
* 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.
 
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:

XenForo_Model::create('XenForo_Model_DataRegistry')->delete('mostViewed');
 
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?
 
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. :)
 
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.
 
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.
 
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.
 
Back
Top Bottom