getModelFromCache from external file

LPH

Well-known member
What is the best way to call getModelFromCache from an external file? The file is connected via a bridge.

Right now I plugged in the getModelFromCache method (copied from XenForo_Model.php) into the connector between WordPress and XenForo. I

PHP:
public function getModelFromCache($class)
{
   if (!isset($this->_modelCache[$class]))
   {
      $this->_modelCache[$class] = XenForo_Model::create($class);
   }

   return $this->_modelCache[$class];
}

And call from a $xenword variable.

PHP:
$xenword_options = get_option( 'xenword_options' );

      $xenword = new XenWord ( $xenword_options );

      /** @var  $sessionModel XenForo_Model_Session */
      $sessionModel = $xenword->getModelFromCache('XenForo_Model_Session');

However, I'm not sure to even tell whether or not this makes any difference from simply using create and the class separately each time.
 
It likely doesn't make a massive amount of difference, but to be fair, what you're doing does the job and should be fine.
 
Thank you.

1. Is there a way to tell if the cache is being used versus create ? Should I comment out the create and see what happens?

2. Is there a proper way to make sure I'm not calling a new XenWord class when that isn't necessary?

Sorry. Im uneasy since I'm not sure how to even test the situation.
 
1. Is there a way to tell if the cache is being used versus create ? Should I comment out the create and see what happens?
It is only cached for the length of the current request hence my comment about it not making a massive amount of difference. Your code does work, though, which I can tell just on the basis of reading the code.

2. Is there a proper way to make sure I'm not calling a new XenWord class when that isn't necessary?
You'd perhaps have a similar method, and it would look something like this:

PHP:
protected $xenword;

public function getXenword()
{
    if (!$this->xenword)
    {
        $xenword_options = get_option( 'xenword_options' );
        $this->xenword = new XenWord ( $xenword_options );
    }
    return $this->xenword;
}
 
  • Like
Reactions: LPH
Top Bottom