From: Tim Otten Date: Mon, 21 Jan 2019 11:06:10 +0000 (-0800) Subject: Implement CRM_Utils_Cache_FastArrayDecorator X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=62ea328c4867d874cf64e32f7bf2fb773cd0211e;p=civicrm-core.git Implement CRM_Utils_Cache_FastArrayDecorator --- diff --git a/CRM/Utils/Cache/FastArrayDecorator.php b/CRM/Utils/Cache/FastArrayDecorator.php new file mode 100644 index 0000000000..5926f23354 --- /dev/null +++ b/CRM/Utils/Cache/FastArrayDecorator.php @@ -0,0 +1,134 @@ + mixed $cacheValue). + */ + private $values = []; + + /** + * CRM_Utils_Cache_FastArrayDecorator constructor. + * @param \CRM_Utils_Cache_Interface $delegate + * @param int $defaultTimeout + * Default number of seconds each cache-item should endure. + */ + public function __construct(\CRM_Utils_Cache_Interface $delegate, $defaultTimeout = 3600) { + $this->defaultTimeout = $defaultTimeout; + $this->delegate = $delegate; + } + + public function set($key, $value, $ttl = NULL) { + if (is_int($ttl) && $ttl <= 0) { + return $this->delete($key); + } + + if ($this->delegate->set($key, $value, $ttl)) { + $this->values[$key] = $value; + return TRUE; + } + else { + return FALSE; + } + } + + public function get($key, $default = NULL) { + if (array_key_exists($key, $this->values)) { + return $this->values[$key]; + } + + $nack = CRM_Utils_Cache::nack(); + $value = $this->delegate->get($key, $nack); + if ($value === $nack) { + return $default; + } + + $this->values[$key] = $value; + return $value; + } + + public function delete($key) { + unset($this->values[$key]); + return $this->delegate->delete($key); + } + + public function flush() { + return $this->clear(); + } + + public function clear() { + $this->values = []; + return $this->delegate->clear(); + } + + public function has($key) { + return $this->delegate->has($key); + } + +} diff --git a/tests/phpunit/E2E/Cache/FastArrayDecoratorTest.php b/tests/phpunit/E2E/Cache/FastArrayDecoratorTest.php new file mode 100644 index 0000000000..fbc37b4050 --- /dev/null +++ b/tests/phpunit/E2E/Cache/FastArrayDecoratorTest.php @@ -0,0 +1,68 @@ +a = CRM_Utils_Cache::create([ + 'name' => 'e2e fast-arr-dec test', + 'type' => ['ArrayCache'], + ]) + ); + } + + public function testSetTtl() { + $this->markTestSkipped('FastArrayDecorator breaks convention: Does not track TTL locally. However, TTL is passed along to delegate.'); + } + + public function testSetMultipleTtl() { + $this->markTestSkipped('FastArrayDecorator breaks convention: Does not track TTL locally. However, TTL is passed along to delegate.'); + } + + public function testDoubleLifeWithDelete() { + $this->markTestSkipped('FastArrayDecorator breaks convention: Does not track TTL locally. However, TTL is passed along to delegate.'); + } + + public function testDoubleLifeWithClear() { + $this->markTestSkipped('FastArrayDecorator breaks convention: Does not track TTL locally. However, TTL is passed along to delegate.'); + } + + public function testObjectDoesNotChangeInCache() { + $this->markTestSkipped('FastArrayDecorator breaks convention: No deep-copying cache content'); + } + +}