Skip to content

Commit

Permalink
\Civi::cache() - Add shorthand for accessing cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Sep 17, 2015
1 parent 9f49773 commit 7b5937f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CRM/Utils/Cache/Interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
20 changes: 20 additions & 0 deletions Civi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function createContainer() {
$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',
);
Expand Down

0 comments on commit 7b5937f

Please sign in to comment.