View source
<?php
namespace Drupal\restful\RenderCache\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource;
class CacheFragmentController extends \EntityAPIController {
const ENTITY_TYPE = 'cache_fragment';
protected static $tableName;
protected static $tableIdKey;
public function createCacheFragments(ArrayCollection $cache_fragments) {
$hash = $this
->generateCacheHash($cache_fragments);
if ($fragments = $this
->existingFragments($hash)) {
return $fragments;
}
foreach ($cache_fragments as $tag_type => $tag_value) {
$cache_fragment = new CacheFragment(array(
'value' => $tag_value,
'type' => $tag_type,
'hash' => $hash,
), static::ENTITY_TYPE);
try {
if ($id = $this
->save($cache_fragment)) {
$fragments[] = $cache_fragment;
}
} catch (\Exception $e) {
watchdog_exception('restful', $e);
}
}
return $fragments;
}
protected function existingFragments($hash) {
$query = new \EntityFieldQuery();
$results = $query
->entityCondition('entity_type', static::ENTITY_TYPE)
->propertyCondition('hash', $hash)
->execute();
return empty($results[static::ENTITY_TYPE]) ? array() : $this
->load(array_keys($results[static::ENTITY_TYPE]));
}
public function generateCacheHash(ArrayCollection $cache_fragments) {
return substr(sha1(serialize($cache_fragments
->toArray())), 0, 40);
}
public static function lookUpHashes(\EntityFieldQuery $query) {
$results = $query
->execute();
if (empty($results[static::ENTITY_TYPE])) {
return array();
}
$fragment_ids = array_keys($results[static::ENTITY_TYPE]);
$hashes = db_query('SELECT hash FROM {' . static::getTableName() . '} WHERE ' . static::getTableIdkey() . ' IN (:ids)', array(
':ids' => $fragment_ids,
))
->fetchCol();
return $hashes;
}
public function wipe() {
$query = new \EntityFieldQuery();
$results = $query
->entityCondition('entity_type', static::ENTITY_TYPE)
->execute();
if (empty($results[static::ENTITY_TYPE])) {
return;
}
if ($this
->isFastDeleteEnabled()) {
db_truncate($this::getTableName())
->execute();
return;
}
$this
->delete(array_keys($results[static::ENTITY_TYPE]));
}
public function delete($ids, \DatabaseTransaction $transaction = NULL) {
if ($this
->isFastDeleteEnabled()) {
$this
->fastDelete($ids, $transaction);
return;
}
parent::delete($ids, $transaction);
}
protected function fastDelete($ids, \DatabaseTransaction $transaction = NULL) {
$transaction = isset($transaction) ? $transaction : db_transaction();
try {
db_delete($this::getTableName())
->condition($this::getTableIdkey(), $ids, 'IN')
->execute();
$this
->resetCache($ids);
db_ignore_slave();
} catch (\Exception $e) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
throw $e;
}
}
protected function isFastDeleteEnabled() {
return (bool) variable_get('restful_fast_cache_clear', TRUE);
}
public static function resourceIdFromHash($hash) {
$query = new \EntityFieldQuery();
$results = $query
->entityCondition('entity_type', static::ENTITY_TYPE)
->propertyCondition('type', 'resource')
->propertyCondition('hash', $hash)
->range(0, 1)
->execute();
if (empty($results[static::ENTITY_TYPE])) {
return NULL;
}
$cache_fragment = entity_load_single(static::ENTITY_TYPE, key($results[static::ENTITY_TYPE]));
$pos = strpos($cache_fragment->value, CacheDecoratedResource::CACHE_PAIR_SEPARATOR);
return $pos === FALSE ? $cache_fragment->value : substr($cache_fragment->value, 0, $pos);
}
protected static function getTableName() {
if (static::$tableName) {
return static::$tableName;
}
$info = entity_get_info(static::ENTITY_TYPE);
static::$tableName = $info['base table'];
return static::$tableName;
}
protected static function getTableIdkey() {
if (static::$tableIdKey) {
return static::$tableIdKey;
}
$info = entity_get_info(static::ENTITY_TYPE);
static::$tableIdKey = $info['entity keys']['id'];
return static::$tableIdKey;
}
}