* @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;
}
* @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);
}
* @return bool
*/
public function delete($key) {
+ CRM_Utils_Cache::assertValidKey($key);
$this->_cache->delete($this->_prefix . $key);
return TRUE;
}
--- /dev/null
+<?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.');
+ }
+ }
+
+}