Skip to content

Commit

Permalink
(dev/core#174) SqlGroup - Rewrite with PSR-16 and TTL support. Bypass…
Browse files Browse the repository at this point in the history
… BAO multitier cache.
  • Loading branch information
totten committed Jun 25, 2018
1 parent 8c2402c commit daae501
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 24 deletions.
160 changes: 137 additions & 23 deletions CRM/Utils/Cache/SqlGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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'];
}
Expand All @@ -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();
}
Expand All @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -127,26 +192,35 @@ 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];
}

/**
* @param string $key
* @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;
}

Expand All @@ -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");
}
}

}
4 changes: 3 additions & 1 deletion tests/phpunit/CRM/Utils/Cache/SqlGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/E2E/Cache/SqlGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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_SqlGroup complies with PSR-16.
*
* @group e2e
*/
class E2E_Cache_SqlGroupTest extends E2E_Cache_CacheTestCase {

public function createSimpleCache() {
return CRM_Utils_Cache::create([
'name' => 'e2e sqlgroup test',
'type' => ['SqlGroup'],
]);
}

}

0 comments on commit daae501

Please sign in to comment.