Add task to "Rebuild User Caches"

Valhalla

Well-known member
I'm trying to add a task to the existing "Rebuild User Caches" process.

My thinking, and current setup, is to extend the User DataWriter, and specifically the "rebuildCustomFields" method to invoke my DB update.

This is roughly what I am thinking, but I would welcome some thoughts - thank you.

As you see, the existing core method "rebuildCustomFields" is being extended.

PHP:
class LikesGiven_DataWriter_User extends XFCP_LikesGiven_DataWriter_User
{
    public function rebuildCustomFields()
    {
        $response = parent::rebuildCustomFields();

        $this->_getUserModel()->rebuildUserLikeGivenCache($this->get('user_id'));

        return $response;
    }

    protected function _getUserModel()
    {
        return $this->getModelFromCache('XenForo_Model_User');
    }
}

PHP:
class LikesGiven_Model_User extends XFCP_LikesGiven_Model_User
{
    public function rebuildUserLikeGivenCache($userId)
    {
        $count = $this->_getLikeModel()->countLikesGivenByUser($userId);
       
        $db = $this->_getDb();
        $db->update('xf_user_like_given',
            array('like_given_count' => $count),
                'user_id = ' . $db->quote($userId)
        );
    }
}
 
Don't see anything obviously wrong with that.

Bear in mind the class you're extending, in this case the User DataWriter (which itself extends the base DataWriter class) may already have a function to get the User model. If it does, having one yourself is not ideal because you're then effectively overwriting it. Or indeed others may have done the same. Which, honestly it isn't likely to cause a big problem unless someone is doing something especially odd, but it's somewhat superfluous anyway; you only call it once so doing it via an additional function call doesn't really achieve anything.

There is one other point and that is that sometimes the custom fields cache is rebuilt independently of the user caches. So it just depends if that is something which is ok.
 
Back
Top Bottom