From: Tim Otten Date: Wed, 19 Aug 2015 13:26:57 +0000 (-0700) Subject: \Civi::cache() - Add shorthand for accessing cache. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7b5937feec104d6843290f2c4822799b7c3d9fe2;p=civicrm-core.git \Civi::cache() - Add shorthand for accessing cache. --- diff --git a/CRM/Utils/Cache/Interface.php b/CRM/Utils/Cache/Interface.php index 4363d51f6f..f702114bf8 100644 --- a/CRM/Utils/Cache/Interface.php +++ b/CRM/Utils/Cache/Interface.php @@ -29,6 +29,31 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 + * + * CRM_Utils_Cache_Interface + * + * PHP-FIG has been developing a draft standard for caching, + * PSR-6. The standard has not been ratified yet. When + * making changes to this interface, please take care to + * avoid *conflicst* with PSR-6's CacheItemPoolInterface. At + * time of writing, they do not conflict. Avoiding conflicts + * will enable more transition paths where Civi + * simultaneously supports both interfaces in the same + * implementation. + * + * For example, the current interface defines: + * + * function get($key) => mixed $value + * + * and PSR-6 defines: + * + * function getItem($key) => ItemInterface $item + * + * These are different styles (e.g. "weak item" vs "strong item"), + * but the two methods do not *conflict*. They can coexist, + * and you can trivially write adapters between the two. + * + * @see https://github.com/php-fig/fig-standards/blob/master/proposed/cache.md */ interface CRM_Utils_Cache_Interface { diff --git a/Civi.php b/Civi.php index 0d899a65f4..d4fe840ce6 100644 --- a/Civi.php +++ b/Civi.php @@ -25,6 +25,26 @@ class Civi { */ public static $statics = array(); + /** + * EXPERIMENTAL. Retrieve a named cache instance. + * + * This interface is flagged as experimental due to political + * ambiguity in PHP community -- PHP-FIG has an open but + * somewhat controversial draft standard for caching. Based on + * the current draft, it's expected that this function could + * simultaneously support both CRM_Utils_Cache_Interface and + * PSR-6, but that depends on whether PSR-6 changes any more. + * + * @param string $name + * The name of the cache. The 'default' cache is biased toward + * high-performance caches (eg memcache/redis/apc) when + * available and falls back to single-request (static) caching. + * @return CRM_Utils_Cache_Interface + */ + public static function cache($name = 'default') { + return \Civi\Core\Container::singleton()->get('cache.' . $name); + } + /** * Get the service container. * diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 526ee9463f..7b4b611468 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -110,6 +110,7 @@ class Container { $singletons = array( 'resources' => 'CRM_Core_Resources', 'httpClient' => 'CRM_Utils_HttpClient', + 'cache.default' => 'CRM_Utils_Cache', // Maybe? 'config' => 'CRM_Core_Config', // Maybe? 'smarty' => 'CRM_Core_Smarty', );