diff --git a/CRM/Utils/Cache/SqlGroup.php b/CRM/Utils/Cache/SqlGroup.php index 3b983ae10632..e476f3b8320a 100644 --- a/CRM/Utils/Cache/SqlGroup.php +++ b/CRM/Utils/Cache/SqlGroup.php @@ -38,8 +38,11 @@ */ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { + // 6*60*60 + const DEFAULT_TTL = 21600; + + const TS_FMT = 'Y-m-d H:i:s'; use CRM_Utils_Cache_NaiveMultipleTrait; // TODO Consider native implementation. - use CRM_Utils_Cache_NaiveHasTrait; // TODO Native implementation /** * The host name of the memcached server. @@ -56,7 +59,18 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { /** * @var array in-memory cache to optimize redundant get()s */ - protected $frontCache; + protected $valueCache; + + /** + * @var array in-memory cache to optimize redundant get()s + * Note: expiresCache[$key]===NULL means cache-miss + */ + protected $expiresCache; + + /** + * @var string + */ + protected $table; /** * Constructor. @@ -71,6 +85,7 @@ class CRM_Utils_Cache_SqlGroup implements CRM_Utils_Cache_Interface { * @return \CRM_Utils_Cache_SqlGroup */ public function __construct($config) { + $this->table = CRM_Core_DAO_Cache::getTableName(); if (isset($config['group'])) { $this->group = $config['group']; } @@ -83,7 +98,7 @@ public function __construct($config) { else { $this->componentID = NULL; } - $this->frontCache = array(); + $this->valueCache = array(); if (CRM_Utils_Array::value('prefetch', $config, TRUE)) { $this->prefetch(); } @@ -96,11 +111,53 @@ public function __construct($config) { * @return bool */ 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); + + // CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID); + $lock = Civi::lockManager()->acquire("cache.{$this->group}_{$key}._null"); + if (!$lock->isAcquired()) { + throw new \CRM_Utils_Cache_CacheException("SqlGroup: Failed to acquire lock on cache key."); } - CRM_Core_BAO_Cache::setItem($value, $this->group, $key, $this->componentID); - $this->frontCache[$key] = $value; + + $dataExists = CRM_Core_DAO::singleValueQuery("SELECT COUNT(*) FROM {$this->table} WHERE {$this->where($key)}"); + $now = date(self::TS_FMT); // FIXME - Use SQL NOW() or CRM_Utils_Time? + $expires = $this->convertTtlToExpires($ttl); + + $dataSerialized = CRM_Core_BAO_Cache::encode($value); + + // This table has a wonky index, so we cannot use REPLACE or + // "INSERT ... ON DUPE". Instead, use SELECT+(INSERT|UPDATE). + if ($dataExists) { + $sql = "UPDATE {$this->table} SET data = %1, created_date = %2, expired_date = %3 WHERE {$this->where($key)}"; + $args = array( + 1 => array($dataSerialized, 'String'), + 2 => array($now, 'String'), + 3 => array(date(self::TS_FMT, $expires), 'String'), + ); + // printf("WRITE: %s\n", CRM_Core_DAO::composeQuery($sql, $args)); + $dao = CRM_Core_DAO::executeQuery($sql, $args, FALSE, NULL, FALSE, FALSE); + } + else { + $insert = CRM_Utils_SQL_Insert::into($this->table) + ->row(array( + 'group_name' => $this->group, + 'path' => $key, + 'component_id' => NULL, + 'data' => $dataSerialized, + 'created_date' => $now, + 'expired_date' => date(self::TS_FMT, $expires), + )); + $sql = $insert->toSQL(); + // printf("WRITE: %s\n", $sql); + $dao = CRM_Core_DAO::executeQuery($sql, array(), FALSE, NULL, FALSE, FALSE); + } + + $lock->release(); + + $dao->free(); + + $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dataSerialized); + $this->expiresCache[$key] = $expires; return TRUE; } @@ -111,13 +168,21 @@ public function set($key, $value, $ttl = NULL) { * @return mixed */ public function get($key, $default = NULL) { - if ($default !== NULL) { - throw new \RuntimeException("FIXME: " . __CLASS__ . "::get() only supports NULL default"); + CRM_Utils_Cache::assertValidKey($key); + if (!isset($this->expiresCache[$key]) || time() >= $this->expiresCache[$key]) { + $sql = "SELECT path, data, UNIX_TIMESTAMP(expired_date) as expires FROM {$this->table} WHERE " . $this->where($key); + $dao = CRM_Core_DAO::executeQuery($sql); + while ($dao->fetch()) { + $this->expiresCache[$key] = $dao->expires; + $this->valueCache[$key] = CRM_Core_BAO_Cache::decode($dao->data); + } + $dao->free(); } - if (!array_key_exists($key, $this->frontCache)) { - $this->frontCache[$key] = CRM_Core_BAO_Cache::getItem($this->group, $key, $this->componentID); - } - return $this->frontCache[$key]; + return (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]) ? $this->reobjectify($this->valueCache[$key]) : $default; + } + + private function reobjectify($value) { + return is_object($value) ? unserialize(serialize($value)) : $value; } /** @@ -127,7 +192,17 @@ public function get($key, $default = NULL) { * @return mixed */ public function getFromFrontCache($key, $default = NULL) { - return CRM_Utils_Array::value($key, $this->frontCache, $default); + if (isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key] && $this->valueCache[$key]) { + return $this->reobjectify($this->valueCache[$key]); + } + else { + return $default; + } + } + + public function has($key) { + $this->get($key); + return isset($this->expiresCache[$key]) && time() < $this->expiresCache[$key]; } /** @@ -135,18 +210,17 @@ public function getFromFrontCache($key, $default = NULL) { * @return bool */ public function delete($key) { - CRM_Core_BAO_Cache::deleteGroup($this->group, $key, FALSE); - CRM_Core_BAO_Cache::$_cache = NULL; // FIXME: remove multitier cache - CRM_Utils_Cache::singleton()->flush(); // FIXME: remove multitier cache - unset($this->frontCache[$key]); + CRM_Utils_Cache::assertValidKey($key); + CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where($key)}"); + unset($this->valueCache[$key]); + unset($this->expiresCache[$key]); return TRUE; } public function flush() { - CRM_Core_BAO_Cache::deleteGroup($this->group, NULL, FALSE); - CRM_Core_BAO_Cache::$_cache = NULL; // FIXME: remove multitier cache - CRM_Utils_Cache::singleton()->flush(); // FIXME: remove multitier cache - $this->frontCache = array(); + CRM_Core_DAO::executeQuery("DELETE FROM {$this->table} WHERE {$this->where()}"); + $this->valueCache = array(); + $this->expiresCache = array(); return TRUE; } @@ -155,7 +229,47 @@ public function clear() { } public function prefetch() { - $this->frontCache = CRM_Core_BAO_Cache::getItems($this->group, $this->componentID); + $dao = CRM_Core_DAO::executeQuery("SELECT path, data, UNIX_TIMESTAMP(expired_date) AS expires FROM {$this->table} WHERE " . $this->where(NULL)); + $this->valueCache = array(); + $this->expiresCache = array(); + while ($dao->fetch()) { + $this->valueCache[$dao->path] = CRM_Core_BAO_Cache::decode($dao->data); + $this->expiresCache[$dao->path] = $dao->expires; + } + $dao->free(); + } + + protected function where($path = NULL) { + $clauses = array(); + $clauses[] = ('group_name = "' . CRM_Core_DAO::escapeString($this->group) . '"'); + if ($path) { + $clauses[] = ('path = "' . CRM_Core_DAO::escapeString($path) . '"'); + } + return $clauses ? implode(' AND ', $clauses) : '(1)'; + } + + /** + * Translate a TTL to a concrete expiration time. + * + * @param NULL|int|DateInterval $ttl + * @return int + * Timestamp (seconds since epoch). + * @throws \CRM_Utils_Cache_InvalidArgumentException + */ + private function convertTtlToExpires($ttl) { + if ($ttl === NULL) { + $ttl = self::DEFAULT_TTL; + } + + if (is_int($ttl)) { + return time() + $ttl; + } + elseif ($ttl instanceof DateInterval) { + return date_add(new DateTime(), $ttl)->getTimestamp(); + } + else { + throw new CRM_Utils_Cache_InvalidArgumentException("Invalid cache TTL"); + } } } diff --git a/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php b/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php index 85de83acc698..77f31a26b4ae 100644 --- a/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php +++ b/tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php @@ -47,7 +47,9 @@ public function testTwoInstance() { )); $fooValue = array('whiz' => 'bang', 'bar' => 3); $a->set('foo', $fooValue); - $this->assertEquals($a->get('foo'), array('whiz' => 'bang', 'bar' => 3)); + $getValue = $a->get('foo'); + $expectValue = array('whiz' => 'bang', 'bar' => 3); + $this->assertEquals($getValue, $expectValue); $b = new CRM_Utils_Cache_SqlGroup(array( 'group' => 'testTwoInstance', diff --git a/tests/phpunit/E2E/Cache/SqlGroupTest.php b/tests/phpunit/E2E/Cache/SqlGroupTest.php new file mode 100644 index 000000000000..8ed89e028990 --- /dev/null +++ b/tests/phpunit/E2E/Cache/SqlGroupTest.php @@ -0,0 +1,41 @@ + 'e2e sqlgroup test', + 'type' => ['SqlGroup'], + ]); + } + +}