From 9f70b0e4bc774d25e5bcff3de677e35d2808b646 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 19 Jun 2018 16:43:21 -0700 Subject: [PATCH] (dev/core#174) CRM_Utils_Cache_Interface - Add `has()` (PSR-16) --- CRM/Utils/Cache/APCcache.php | 1 + CRM/Utils/Cache/ArrayCache.php | 1 + CRM/Utils/Cache/Interface.php | 14 +++++++++ CRM/Utils/Cache/Memcache.php | 1 + CRM/Utils/Cache/Memcached.php | 1 + CRM/Utils/Cache/NaiveHasTrait.php | 49 ++++++++++++++++++++++++++++++ CRM/Utils/Cache/NoCache.php | 1 + CRM/Utils/Cache/Redis.php | 1 + CRM/Utils/Cache/SerializeCache.php | 1 + CRM/Utils/Cache/SqlGroup.php | 1 + 10 files changed, 71 insertions(+) create mode 100644 CRM/Utils/Cache/NaiveHasTrait.php diff --git a/CRM/Utils/Cache/APCcache.php b/CRM/Utils/Cache/APCcache.php index d0047b857f..a8b4e5cd69 100644 --- a/CRM/Utils/Cache/APCcache.php +++ b/CRM/Utils/Cache/APCcache.php @@ -33,6 +33,7 @@ class CRM_Utils_Cache_APCcache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation const DEFAULT_TIMEOUT = 3600; const DEFAULT_PREFIX = ''; diff --git a/CRM/Utils/Cache/ArrayCache.php b/CRM/Utils/Cache/ArrayCache.php index 47ad9a082d..c74e1900f1 100644 --- a/CRM/Utils/Cache/ArrayCache.php +++ b/CRM/Utils/Cache/ArrayCache.php @@ -37,6 +37,7 @@ class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation /** * The cache storage container, an in memory array by default diff --git a/CRM/Utils/Cache/Interface.php b/CRM/Utils/Cache/Interface.php index 52e4f11412..51158363d4 100644 --- a/CRM/Utils/Cache/Interface.php +++ b/CRM/Utils/Cache/Interface.php @@ -108,4 +108,18 @@ interface CRM_Utils_Cache_Interface { */ public function clear(); + /** + * Determines whether an item is present in the cache. + * + * NOTE: It is recommended that has() is only to be used for cache warming type purposes + * and not to be used within your live applications operations for get/set, as this method + * is subject to a race condition where your has() will return true and immediately after, + * another script can remove it making the state of your app out of date. + * + * @param string $key The cache item key. + * + * @return bool + */ + public function has($key); + } diff --git a/CRM/Utils/Cache/Memcache.php b/CRM/Utils/Cache/Memcache.php index 23889f5fe7..8e6e01dc51 100644 --- a/CRM/Utils/Cache/Memcache.php +++ b/CRM/Utils/Cache/Memcache.php @@ -33,6 +33,7 @@ class CRM_Utils_Cache_Memcache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 11211; diff --git a/CRM/Utils/Cache/Memcached.php b/CRM/Utils/Cache/Memcached.php index 7b9bcfeaf5..c1ad9cb75a 100644 --- a/CRM/Utils/Cache/Memcached.php +++ b/CRM/Utils/Cache/Memcached.php @@ -33,6 +33,7 @@ class CRM_Utils_Cache_Memcached implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 11211; diff --git a/CRM/Utils/Cache/NaiveHasTrait.php b/CRM/Utils/Cache/NaiveHasTrait.php new file mode 100644 index 0000000000..78ecc67887 --- /dev/null +++ b/CRM/Utils/Cache/NaiveHasTrait.php @@ -0,0 +1,49 @@ +get($key, NULL) === NULL); + $hasDefaultB = ($this->get($key, 123) === 123); + return !($hasDefaultA && $hasDefaultB); + } + +} diff --git a/CRM/Utils/Cache/NoCache.php b/CRM/Utils/Cache/NoCache.php index 1fdaef4e99..3c1d9b9445 100644 --- a/CRM/Utils/Cache/NoCache.php +++ b/CRM/Utils/Cache/NoCache.php @@ -33,6 +33,7 @@ class CRM_Utils_Cache_NoCache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation /** * We only need one instance of this object. So we use the singleton diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php index 0cc979abcc..01fc538c93 100644 --- a/CRM/Utils/Cache/Redis.php +++ b/CRM/Utils/Cache/Redis.php @@ -35,6 +35,7 @@ class CRM_Utils_Cache_Redis implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation const DEFAULT_HOST = 'localhost'; const DEFAULT_PORT = 6379; diff --git a/CRM/Utils/Cache/SerializeCache.php b/CRM/Utils/Cache/SerializeCache.php index 910fe1bf7e..fdf0eb6d47 100644 --- a/CRM/Utils/Cache/SerializeCache.php +++ b/CRM/Utils/Cache/SerializeCache.php @@ -37,6 +37,7 @@ class CRM_Utils_Cache_SerializeCache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation /** * 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 91237b5847..3b983ae106 100644 --- a/CRM/Utils/Cache/SqlGroup.php +++ b/CRM/Utils/Cache/SqlGroup.php @@ -39,6 +39,7 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. + use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation /** * The host name of the memcached server. -- 2.25.1