Skip to content

Commit

Permalink
Redis - Updates to comply with PSR-16
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jun 23, 2018
1 parent 85f5970 commit d903350
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
32 changes: 29 additions & 3 deletions CRM/Utils/Cache/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ 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)) {
if (!$this->_cache->setex($this->_prefix . $key, $this->convertTtl($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());
}
Expand All @@ -146,6 +147,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);
}
Expand All @@ -156,6 +158,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;
}
Expand All @@ -177,4 +180,27 @@ public function clear() {
return $this->flush();
}

/**
* Normalize a TTL.
*
* @param NULL|int|DateInterval $ttl
* @return int
* Seconds until expiration.
* @throws \CRM_Utils_Cache_InvalidArgumentException
*/
private function convertTtl($ttl) {
if ($ttl === NULL) {
return self::DEFAULT_TIMEOUT;
}
elseif (is_int($ttl)) {
return $ttl;
}
elseif ($ttl instanceof DateInterval) {
return date_add(new DateTime(), $ttl)->getTimestamp() - time();
}
else {
throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL");
}
}

}
48 changes: 48 additions & 0 deletions tests/phpunit/E2E/Cache/ConfiguredMemoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Verify that CRM_Utils_Cache_{Redis,Memcache} complies with PSR-16.
*
* NOTE: Only works if the local system is configured to use one of
* those services.
*
* @group e2e
*/
class E2E_Cache_ConfiguredMemoryTest extends E2E_Cache_CacheTestCase {

public function createSimpleCache() {
$cache = Civi::cache('default');

if ($cache instanceof CRM_Utils_Cache_Redis || $cache instanceof CRM_Utils_Cache_Memcache || $cache instanceof CRM_Utils_Cache_Memcached) {
return $cache;
}
else {
$this->markTestSkipped('This environment is not configured to use a memory-backed cache service.');
}
}

}

0 comments on commit d903350

Please sign in to comment.