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

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

Discussion in 'XenForo Development Discussions' started by tyteen4a03, May 30, 2014.

  1. tyteen4a03

    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: May 30, 2014
  2. W1zzard

    W1zzard Well-Known Member

    Put it in a plain old profile field ?
     
  3. tyteen4a03

    tyteen4a03 Well-Known Member

    You can make every user fetching query ever fetch a profile field automatically?
     
  4. Chris D

    Chris D XenForo Developer Staff Member

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

    tyteen4a03 Well-Known Member

    I forgot to mention; the page will fetch other users' cache as well.
     
  6. W1zzard

    W1zzard Well-Known Member

    do you really need that field everywhere? fetch it when you need it, cache it in a static array indexed by userid for the duration of your script
     
  7. tyteen4a03

    tyteen4a03 Well-Known Member

    It is needed pretty much everywhere that involves displaying usernames.
     
  8. Chris D

    Chris D XenForo Developer Staff Member

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

    tyteen4a03 Well-Known Member

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

Share This Page