From 0d64c8fa3bce8e62d8f62d22bacdc00a1161d6df Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 19 Jun 2018 15:53:46 -0700 Subject: [PATCH] (dev/core#174) CRM_Utils_Cache_Interface - Add getMultiple/setMultiple (PSR-16) --- CRM/Utils/Cache/APCcache.php | 3 + CRM/Utils/Cache/ArrayCache.php | 2 + CRM/Utils/Cache/Memcache.php | 3 + CRM/Utils/Cache/Memcached.php | 3 + CRM/Utils/Cache/NaiveMultipleTrait.php | 102 +++++++++++++++++++++++++ CRM/Utils/Cache/NoCache.php | 2 + CRM/Utils/Cache/Redis.php | 3 + CRM/Utils/Cache/SerializeCache.php | 2 + CRM/Utils/Cache/SqlGroup.php | 2 + 9 files changed, 122 insertions(+) create mode 100644 CRM/Utils/Cache/NaiveMultipleTrait.php diff --git a/CRM/Utils/Cache/APCcache.php b/CRM/Utils/Cache/APCcache.php index cb08b7cd16..8c36400dcd 100644 --- a/CRM/Utils/Cache/APCcache.php +++ b/CRM/Utils/Cache/APCcache.php @@ -31,6 +31,9 @@ * @copyright CiviCRM LLC (c) 2004-2018 */ class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface { + + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + const DEFAULT_TIMEOUT = 3600; const DEFAULT_PREFIX = ''; diff --git a/CRM/Utils/Cache/ArrayCache.php b/CRM/Utils/Cache/ArrayCache.php index 5d3fc7c050..d4bb268fbf 100644 --- a/CRM/Utils/Cache/ArrayCache.php +++ b/CRM/Utils/Cache/ArrayCache.php @@ -36,6 +36,8 @@ */ class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { + use CRM_Utils_Cache_NaiveMultipleTrait; + /** * The cache storage container, an in memory array by default */ diff --git a/CRM/Utils/Cache/Memcache.php b/CRM/Utils/Cache/Memcache.php index dcab723283..c85b85559b 100644 --- a/CRM/Utils/Cache/Memcache.php +++ b/CRM/Utils/Cache/Memcache.php @@ -31,6 +31,9 @@ * @copyright CiviCRM LLC (c) 2004-2018 */ class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface { + + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 11211; const DEFAULT_TIMEOUT = 3600; diff --git a/CRM/Utils/Cache/Memcached.php b/CRM/Utils/Cache/Memcached.php index 82910da114..876f6961e6 100644 --- a/CRM/Utils/Cache/Memcached.php +++ b/CRM/Utils/Cache/Memcached.php @@ -31,6 +31,9 @@ * @copyright CiviCRM LLC (c) 2004-2018 */ class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface { + + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 11211; const DEFAULT_TIMEOUT = 3600; diff --git a/CRM/Utils/Cache/NaiveMultipleTrait.php b/CRM/Utils/Cache/NaiveMultipleTrait.php new file mode 100644 index 0000000000..b1f37a4490 --- /dev/null +++ b/CRM/Utils/Cache/NaiveMultipleTrait.php @@ -0,0 +1,102 @@ + value pairs. Cache keys that do not exist or are stale will have $default as value. + * + * @throws \Psr\SimpleCache\InvalidArgumentException + * MUST be thrown if $keys is neither an array nor a Traversable, + * or if any of the $keys are not a legal value. + */ + public function getMultiple($keys, $default = NULL) { + $result = []; + foreach ($keys as $key) { + $result[$key] = $this->get($key, $default); + } + return $result; + } + + /** + * Persists a set of key => value pairs in the cache, with an optional TTL. + * + * @param iterable $values A list of key => value pairs for a multiple-set operation. + * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and + * the driver supports TTL then the library may set a default value + * for it or let the driver take care of that. + * + * @return bool True on success and false on failure. + * + * @throws \Psr\SimpleCache\InvalidArgumentException + * MUST be thrown if $values is neither an array nor a Traversable, + * or if any of the $values are not a legal value. + */ + public function setMultiple($values, $ttl = NULL) { + $result = TRUE; + foreach ($values as $key => $value) { + $result = $this->set($key, $value, $ttl) || $result; + } + return $result; + } + + /** + * Deletes multiple cache items in a single operation. + * + * @param iterable $keys A list of string-based keys to be deleted. + * + * @return bool True if the items were successfully removed. False if there was an error. + * + * @throws \Psr\SimpleCache\InvalidArgumentException + * MUST be thrown if $keys is neither an array nor a Traversable, + * or if any of the $keys are not a legal value. + */ + public function deleteMultiple($keys) { + $result = TRUE; + foreach ($keys as $key) { + $result = $this->delete($key) || $result; + } + return $result; + } + +} diff --git a/CRM/Utils/Cache/NoCache.php b/CRM/Utils/Cache/NoCache.php index 119f646155..c0d7523c3a 100644 --- a/CRM/Utils/Cache/NoCache.php +++ b/CRM/Utils/Cache/NoCache.php @@ -32,6 +32,8 @@ */ class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface { + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + /** * We only need one instance of this object. So we use the singleton * pattern and cache the instance in this variable diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php index 664a2f2cb5..f0ee3f3792 100644 --- a/CRM/Utils/Cache/Redis.php +++ b/CRM/Utils/Cache/Redis.php @@ -33,6 +33,9 @@ * */ class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { + + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 6379; const DEFAULT_TIMEOUT = 3600; diff --git a/CRM/Utils/Cache/SerializeCache.php b/CRM/Utils/Cache/SerializeCache.php index 5e21c31726..7f6f63246f 100644 --- a/CRM/Utils/Cache/SerializeCache.php +++ b/CRM/Utils/Cache/SerializeCache.php @@ -36,6 +36,8 @@ */ class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface { + use CRM_Utils_Cache_NaiveMultipleTrait; + /** * The cache storage container, an array by default, stored in a file under templates */ diff --git a/CRM/Utils/Cache/SqlGroup.php b/CRM/Utils/Cache/SqlGroup.php index e681639ea2..01a7637737 100644 --- a/CRM/Utils/Cache/SqlGroup.php +++ b/CRM/Utils/Cache/SqlGroup.php @@ -38,6 +38,8 @@ */ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { + use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + /** * The host name of the memcached server. * -- 2.25.1