/** * Factory method that creates a CacheHandler instance and instantiate a * BagOStuff object from available settings ($smwgCacheType) while * invoking additional parameters ($wgCachePrefix etc.) * * @par Example: * @code * $cache = new CacheHandler::newFromId()->setkey( new CachIdGenerator( 'Foo' ) ) * * $cache->set( 'CacheableObject' ) * $cache->get() returns 'CacheableObject' * $cache->delete() deletes 'CacheableObject' * @endcode * * @note If a BagOStuff instance is not available setCacheEnabled() is * disabled by default which prevents to run into unintended behaviour * while trying to access BagOStuff methods. * * @note This method is exposed to $wgObjectCaches, $wgCachePrefix globals * which can't and shouldn't be accessed otherwise. It is the task of this * method alone to invoke globals and avoid leakage into the object * life cycle. * * @note This method serves invoked instances from a static variable due to * the fact that the actual working object is being accessed through * getCache() and therefore not in direct conflict with its testability. * * @since 1.9 * * @param string $id Ids available in wgObjectCaches * * @return CacheHandler */ public static function newFromId($id = false) { $cacheType = $id ? $id : Settings::newFromGlobals()->get('smwgCacheType'); if (!isset(self::$instance[$cacheType])) { if ($cacheType && array_key_exists($cacheType, $GLOBALS['wgObjectCaches'])) { $cache = new self(ObjectCache::getInstance($cacheType)); } else { $cache = new self(); } $cache->setCacheEnabled(true); self::$instance[$cacheType] = $cache; } return self::$instance[$cacheType]; }