Caching a per-user field - how do you do it?

tyteen4a03

Well-known member
(Have I asked this before? The month-long vacation totally caused some memory loss...)

So I have things I need to cache. It's a small piece of serialized array that is generated for every user. Pages use the user's cache entry and other users' cache entry.

How would you cache them efficiently?

My ideas are:
  1. Extra field in user table - I'm not quite sure how to make every user fetching query automatically fetch my extra field, so there's a little bit of problem.
  2. (Ab)use the DataRegistry to store one row per user (with A Dirty Hack in place for my Usergroup Ranks addon) - It Just Isn't Right, and kind of inefficient as well since this practically violates 2NF(?)
  3. Use the DataRegistry and store the caches in reasonably-sized chunks - it seems to be the way to go, but I have yet to write code that does this.
What do you do?
 
Last edited:
Number 1.

A column in the xf_user table will always be apart of the $visitor array in the template or XenForo_Visitor::getInstance() in PHP. Additionally you could unserialize it in the visitor_setup event to save you from having to do that every time you need it.

Alternatively, you could write it to the session, but unless there is a particular reason for that, number 1 seems the most simple.
 
Number 1.

A column in the xf_user table will always be apart of the $visitor array in the template or XenForo_Visitor::getInstance() in PHP. Additionally you could unserialize it in the visitor_setup event to save you from having to do that every time you need it.

Alternatively, you could write it to the session, but unless there is a particular reason for that, number 1 seems the most simple.
I forgot to mention; the page will fetch other users' cache as well.
 
If it's in the xf_user table, most default getUserByXXXX or getUsers function will get that field. Because pretty much all user queries select all fields from the xf_user table.
 
If it's in the xf_user table, most default getUserByXXXX or getUsers function will get that field. Because pretty much all user queries select all fields from the xf_user table.
From my experience (month-long vacation so excuse me for any memory losses) the ones that are solely used to fetch usernames don't. I think they just use $db->query().
 
Top Bottom