XF 2.0 XenForo_Application::get('config')->cache->enabled

AndyB

Well-known member
What would be the equivalent in XF2?

PHP:
$cacheEnabled = XenForo_Application::get('config')->cache->enabled;

Thank you.
 
You'd just call the cache() method on the app object (a local $this->app or $this->app(), XF::app() if you have to). It returns null if there's no cache enabled.
 
  • Like
Reactions: Xon
If I add the following to my config.php:

PHP:
$config['cache']['enabled'] = true;

I would like to be able to see in my add-on if this has been entered into the config.php. If in my add-on I add the following:

PHP:
$cacheEnabled = \XF::app()->cache->enabled;

I get this error message:

ErrorException: Undefined property: Doctrine\Common\Cache\VoidCache::$enabled in src/addons/Andy/InsertAttachmentAll/Pub/Controller/InsertAttachmentAll.php at line 20

So what would be the correct PHP code to see if the cache is enabled?
 
Just as a follow up, the existence of a cache object means that the cache is enabled. You don't check anything else.
 
  • Like
Reactions: Xon
Thank you, Fillip.

This worked perfect:

PHP:
$cacheObj = \XF::app()->cache();


Can you explain the purpose of this code.
PHP:
$cacheEnabled = $cacheObj ? $cacheObj->enabled : false;
 
Thank you, Fillip.

This worked perfect:

PHP:
$cacheObj = \XF::app()->cache();


Can you explain the purpose of this code.
PHP:
$cacheEnabled = $cacheObj ? $cacheObj->enabled : false;

It's checking if the $cachObj->enabled is true and $cacheObj is a valid value
 
Thank you, Jake.

So I suppose this would be equivalent:

PHP:
        $cacheObj = \XF::app()->cache();
        
        if ($cacheObj)
        {
            
        }
 
Back
Top Bottom