From b5d3f3c5659456f4baaca5a592d644a17872da90 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 20 Jun 2018 17:35:14 -0700 Subject: [PATCH 1/2] (dev/core#174) ArrayCache - Updates to comply with PSR-16 --- CRM/Utils/Cache/ArrayCache.php | 32 ++++++++++++++--- tests/phpunit/E2E/Cache/ArrayCacheTest.php | 41 ++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/E2E/Cache/ArrayCacheTest.php diff --git a/CRM/Utils/Cache/ArrayCache.php b/CRM/Utils/Cache/ArrayCache.php index c74e1900f1bc..287c6a3e5ce1 100644 --- a/CRM/Utils/Cache/ArrayCache.php +++ b/CRM/Utils/Cache/ArrayCache.php @@ -39,11 +39,15 @@ class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { use CRM_Utils_Cache_NaiveMultipleTrait; use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation + const DEFAULT_TIMEOUT = 3600; + /** * The cache storage container, an in memory array by default */ protected $_cache; + protected $_expires; + /** * Constructor. * @@ -54,6 +58,7 @@ class CRM_Utils_Cache_Arraycache implements CRM_Utils_Cache_Interface { */ public function __construct($config) { $this->_cache = array(); + $this->_expires = array(); } /** @@ -61,12 +66,12 @@ public function __construct($config) { * @param mixed $value * @param null|int|\DateInterval $ttl * @return bool + * @throws \Psr\SimpleCache\InvalidArgumentException */ public function set($key, $value, $ttl = NULL) { - if ($ttl !== NULL) { - throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL"); - } - $this->_cache[$key] = $value; + CRM_Utils_Cache::assertValidKey($key); + $this->_cache[$key] = $this->reobjectify($value); + $this->_expires[$key] = CRM_Utils_Date::convertCacheTtlToExpires($ttl, self::DEFAULT_TIMEOUT); return TRUE; } @@ -75,22 +80,35 @@ public function set($key, $value, $ttl = NULL) { * @param mixed $default * * @return mixed + * @throws \Psr\SimpleCache\InvalidArgumentException */ public function get($key, $default = NULL) { - return CRM_Utils_Array::value($key, $this->_cache, $default); + CRM_Utils_Cache::assertValidKey($key); + if (isset($this->_expires[$key]) && is_numeric($this->_expires[$key]) && $this->_expires[$key] <= time()) { + return $default; + } + if (array_key_exists($key, $this->_cache)) { + return $this->reobjectify($this->_cache[$key]); + } + return $default; } /** * @param string $key * @return bool + * @throws \Psr\SimpleCache\InvalidArgumentException */ public function delete($key) { + CRM_Utils_Cache::assertValidKey($key); + unset($this->_cache[$key]); + unset($this->_expires[$key]); return TRUE; } public function flush() { unset($this->_cache); + unset($this->_expires); $this->_cache = array(); return TRUE; } @@ -99,4 +117,8 @@ public function clear() { return $this->flush(); } + private function reobjectify($value) { + return is_object($value) ? unserialize(serialize($value)) : $value; + } + } diff --git a/tests/phpunit/E2E/Cache/ArrayCacheTest.php b/tests/phpunit/E2E/Cache/ArrayCacheTest.php new file mode 100644 index 000000000000..65bc045eb4b8 --- /dev/null +++ b/tests/phpunit/E2E/Cache/ArrayCacheTest.php @@ -0,0 +1,41 @@ + 'e2e arraycache test', + 'type' => ['ArrayCache'], + ]); + } + +} From 26733eb54f98cf45e136f2f6901057cccff578f2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 22 Jun 2018 19:22:34 -0700 Subject: [PATCH 2/2] (dev/core#174) Redis - Updates to comply with PSR-16 --- CRM/Utils/Cache/Redis.php | 14 ++++-- .../E2E/Cache/ConfiguredMemoryTest.php | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/E2E/Cache/ConfiguredMemoryTest.php diff --git a/CRM/Utils/Cache/Redis.php b/CRM/Utils/Cache/Redis.php index 01fc538c9355..3e2f2f7f9b3f 100644 --- a/CRM/Utils/Cache/Redis.php +++ b/CRM/Utils/Cache/Redis.php @@ -123,16 +123,18 @@ public function __construct($config) { * @throws Exception */ public function set($key, $value, $ttl = NULL) { - if ($ttl !== NULL) { - throw new \RuntimeException("FIXME: " . __CLASS__ . "::set() should support non-NULL TTL"); + CRM_Utils_Cache::assertValidKey($key); + if (is_int($ttl) && $ttl <= 0) { + return $this->delete($key); } - if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) { + $ttl = CRM_Utils_Date::convertCacheTtl($ttl, self::DEFAULT_TIMEOUT); + if (!$this->_cache->setex($this->_prefix . $key, $ttl, serialize($value))) { if (PHP_SAPI === 'cli' || (Civi\Core\Container::isContainerBooted() && CRM_Core_Permission::check('view debug output'))) { - CRM_Core_Error::fatal("Redis set ($key) failed: " . $this->_cache->getLastError()); + throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed: " . $this->_cache->getLastError()); } else { Civi::log()->error("Redis set ($key) failed: " . $this->_cache->getLastError()); - CRM_Core_Error::fatal("Redis set ($key) failed"); + throw new CRM_Utils_Cache_CacheException("Redis set ($key) failed"); } return FALSE; } @@ -146,6 +148,7 @@ public function set($key, $value, $ttl = NULL) { * @return mixed */ public function get($key, $default = NULL) { + CRM_Utils_Cache::assertValidKey($key); $result = $this->_cache->get($this->_prefix . $key); return ($result === FALSE) ? $default : unserialize($result); } @@ -156,6 +159,7 @@ public function get($key, $default = NULL) { * @return bool */ public function delete($key) { + CRM_Utils_Cache::assertValidKey($key); $this->_cache->delete($this->_prefix . $key); return TRUE; } diff --git a/tests/phpunit/E2E/Cache/ConfiguredMemoryTest.php b/tests/phpunit/E2E/Cache/ConfiguredMemoryTest.php new file mode 100644 index 000000000000..5f1ea8bc079f --- /dev/null +++ b/tests/phpunit/E2E/Cache/ConfiguredMemoryTest.php @@ -0,0 +1,48 @@ +markTestSkipped('This environment is not configured to use a memory-backed cache service.'); + } + } + +}