From 2da67cc5bf2670122892aade13a6546263bf6e7d Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 19 Jun 2018 15:09:08 -0700 Subject: [PATCH] (dev/core#174) CRM_Utils_Cache_Interface::get() should match PSR-16 Comparing `CRM_Utils_Cache_Interface::get()` and `Psr\SimpleCache\CacheInterface::get()`, they agree on key details: 1. They return values are `mixed` (strings, arrays, numbers,, etc) 2. The default representation of a cache-miss is NULL. They differ in that PSR-16 allows the caller to optionally specify a `$default`. Since no existing callers actually use this, we can (for moment) throw an error if someone tries to pass `$default` to a driver that doesn't support it. See also: https://www.php-fig.org/psr/psr-16/ --- CRM/Utils/Cache/APCcache.php | 6 +++++- CRM/Utils/Cache/ArrayCache.php | 5 +++-- CRM/Utils/Cache/Interface.php | 5 +++-- CRM/Utils/Cache/Memcache.php | 6 +++++- CRM/Utils/Cache/Memcached.php | 6 +++++- CRM/Utils/Cache/NoCache.php | 5 +++-- CRM/Utils/Cache/Redis.php | 5 +++-- CRM/Utils/Cache/SerializeCache.php | 7 ++++++- CRM/Utils/Cache/SqlGroup.php | 6 +++++- 9 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CRM/Utils/Cache/APCcache.php b/CRM/Utils/Cache/APCcache.php index e696aa19c3..64c1de94b6 100644 --- a/CRM/Utils/Cache/APCcache.php +++ b/CRM/Utils/Cache/APCcache.php @@ -84,10 +84,14 @@ class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface { /** * @param $key + * @param mixed $default * * @return mixed */ - public function get($key) { + public function get($key, $default = NULL) { + if ($default !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + } return apc_fetch($this->_prefix . $key); } diff --git a/CRM/Utils/Cache/ArrayCache.php b/CRM/Utils/Cache/ArrayCache.php index b2272fa1eb..1c1b2b2979 100644 --- a/CRM/Utils/Cache/ArrayCache.php +++ b/CRM/Utils/Cache/ArrayCache.php @@ -63,11 +63,12 @@ class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { /** * @param string $key + * @param mixed $default * * @return mixed */ - public function get($key) { - return CRM_Utils_Array::value($key, $this->_cache); + public function get($key, $default = NULL) { + return CRM_Utils_Array::value($key, $this->_cache, $default); } /** diff --git a/CRM/Utils/Cache/Interface.php b/CRM/Utils/Cache/Interface.php index f11327cb59..d1d1576a3b 100644 --- a/CRM/Utils/Cache/Interface.php +++ b/CRM/Utils/Cache/Interface.php @@ -69,10 +69,11 @@ interface CRM_Utils_Cache_Interface { * Get a value from the cache. * * @param string $key + * @param mixed $default * @return mixed - * NULL if $key has not been previously set + * The previously set value value, or $default (NULL). */ - public function get($key); + public function get($key, $default = NULL); /** * Delete a value from the cache. diff --git a/CRM/Utils/Cache/Memcache.php b/CRM/Utils/Cache/Memcache.php index 630589e3d8..85a1376500 100644 --- a/CRM/Utils/Cache/Memcache.php +++ b/CRM/Utils/Cache/Memcache.php @@ -121,10 +121,14 @@ class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface { /** * @param $key + * @param mixed $default * * @return mixed */ - public function &get($key) { + public function get($key, $default = NULL) { + if ($default !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + } $result = $this->_cache->get($this->_prefix . $key); return $result; } diff --git a/CRM/Utils/Cache/Memcached.php b/CRM/Utils/Cache/Memcached.php index 07315e53ce..c653154483 100644 --- a/CRM/Utils/Cache/Memcached.php +++ b/CRM/Utils/Cache/Memcached.php @@ -126,10 +126,14 @@ class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface { /** * @param $key + * @param mixed $default * * @return mixed */ - public function &get($key) { + public function get($key, $default = NULL) { + if ($default !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + } $key = $this->cleanKey($key); $result = $this->_cache->get($key); return $result; diff --git a/CRM/Utils/Cache/NoCache.php b/CRM/Utils/Cache/NoCache.php index 66c6b74cff..a7adecbe73 100644 --- a/CRM/Utils/Cache/NoCache.php +++ b/CRM/Utils/Cache/NoCache.php @@ -63,11 +63,12 @@ class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface { /** * @param string $key + * @param mixed $default * * @return null */ - public function get($key) { - return NULL; + public function get($key, $default = NULL) { + return $default; } /** diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php index 4988b8beef..27fcb159a1 100644 --- a/CRM/Utils/Cache/Redis.php +++ b/CRM/Utils/Cache/Redis.php @@ -133,12 +133,13 @@ class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { /** * @param $key + * @param mixed $default * * @return mixed */ - public function get($key) { + public function get($key, $default = NULL) { $result = $this->_cache->get($this->_prefix . $key); - return ($result === FALSE) ? NULL : unserialize($result); + return ($result === FALSE) ? $default : unserialize($result); } /** diff --git a/CRM/Utils/Cache/SerializeCache.php b/CRM/Utils/Cache/SerializeCache.php index 6164694c3e..7ee639bd60 100644 --- a/CRM/Utils/Cache/SerializeCache.php +++ b/CRM/Utils/Cache/SerializeCache.php @@ -67,10 +67,15 @@ class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface { /** * @param string $key + * @param mixed $default * * @return mixed */ - public function get($key) { + public function get($key, $default = NULL) { + if ($default !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + } + if (array_key_exists($key, $this->_cache)) { return $this->_cache[$key]; } diff --git a/CRM/Utils/Cache/SqlGroup.php b/CRM/Utils/Cache/SqlGroup.php index 0bb8e98e27..402614b8f8 100644 --- a/CRM/Utils/Cache/SqlGroup.php +++ b/CRM/Utils/Cache/SqlGroup.php @@ -97,10 +97,14 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { /** * @param string $key + * @param mixed $default * * @return mixed */ - public function get($key) { + public function get($key, $default = NULL) { + if ($default !== NULL) { + throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + } if (!array_key_exists($key, $this->frontCache)) { $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID); } -- 2.25.1