View source
<?php
require_once DRUPAL_ROOT . '/includes/cache.inc';
require_once __DIR__ . '/includes/core.inc';
require_once __DIR__ . '/includes/core-taggable-dci.inc';
class D8Cache implements TaggableDrupalCacheInterface {
protected $bin;
protected $backend;
protected $configuration;
public function __construct($bin) {
global $conf;
$this->bin = $bin;
$class = variable_get('d8cache_cache_class_' . $bin);
if (!isset($class)) {
$class = variable_get('d8cache_cache_default_class');
}
if (!isset($class)) {
$class = variable_get('cache_default_class', 'DrupalDatabaseCache');
}
$this->backend = new $class($bin);
$this->configuration = array();
$configuration = variable_get('d8cache_cache_options', array());
if (isset($configuration[$bin])) {
$this->configuration = $configuration[$bin];
}
}
public function get($cid, $allow_invalid = FALSE) {
$cids = array(
$cid,
);
$cache = $this
->getMultiple($cids, $allow_invalid);
return reset($cache);
}
public function getMultiple(&$cids, $allow_invalid = FALSE) {
$cache = array();
$cids_map = array_flip($cids);
foreach ($this->backend
->getMultiple($cids) as $cid => $item) {
if (!isset($cids_map[$cid])) {
continue;
}
$data = $item->data;
$expire = isset($data->d8cache_expire) ? $data->d8cache_expire : $item->expire;
$item->valid = $expire === CACHE_PERMANENT || $expire === CACHE_TEMPORARY || $expire >= REQUEST_TIME;
if ($data instanceof stdClass && isset($data->d8cache_tags)) {
if (!$this
->checksumValid($data->d8cache_checksum, $data->d8cache_tags)) {
$item->valid = FALSE;
}
$item->data = $data->d8cache_data;
}
if (!$allow_invalid && !$item->valid) {
continue;
}
$cache[$cid] = $item;
unset($cids_map[$cid]);
}
$cids = array_keys($cids_map);
return $cache;
}
public function set($cid, $data, $expire = CACHE_PERMANENT, $tags = array()) {
if (isset($this->configuration['ttl'])) {
if ($this->configuration['ttl'] == CACHE_MAX_AGE_PERMANENT) {
$expire = CACHE_PERMANENT;
}
elseif ($this->configuration['ttl'] == CACHE_PERMANENT || $this->configuration['ttl'] == CACHE_TEMPORARY) {
$expire = $this->configuration['ttl'];
}
else {
$expire = REQUEST_TIME + $this->configuration['ttl'];
}
}
if (isset($this->configuration['tags'])) {
$tags = drupal_merge_cache_tags($tags, $this->configuration['tags']);
}
if ($this->bin == 'cache_page') {
$page_cache_tags =& drupal_static('d8cache_emit_cache_tags', array());
$page_cache_max_age =& drupal_static('d8cache_emit_cache_max_age', CACHE_MAX_AGE_PERMANENT);
if (!empty($page_cache_tags)) {
$tags = drupal_merge_cache_tags($tags, $page_cache_tags);
}
$expire = $this
->mergeExpireWithMaxAge($expire, $page_cache_max_age);
}
if (is_array($data) && isset($data['#attached']) && (isset($data['#attached']['drupal_add_cache_tags']) || isset($data['#attached']['drupal_set_cache_max_age']))) {
$cacheable_metadata = drupal_get_cacheable_metadata_from_render_array($data);
if (!empty($cacheable_metadata['tags'])) {
$tags = drupal_merge_cache_tags($tags, $cacheable_metadata['tags']);
$data['#attached']['drupal_add_cache_tags'] = array(
array(
0 => $cacheable_metadata['tags'],
),
);
}
$expire = $this
->mergeExpireWithMaxAge($expire, $cacheable_metadata['max-age']);
if ($cacheable_metadata['max-age'] != CACHE_MAX_AGE_PERMANENT) {
$data['#attached']['drupal_set_cache_max_age'] = array(
array(
0 => $cacheable_metadata['max-age'],
),
);
}
}
if (empty($tags)) {
$this->backend
->set($cid, $data, $expire);
return;
}
if ($this->backend instanceof TaggableDrupalCacheInterface) {
$this->backend
->set($cid, $data, $expire, $tags);
}
else {
$checksum = $this
->getCurrentChecksum($tags);
$data = (object) array(
'd8cache_tags' => $tags,
'd8cache_checksum' => $checksum,
'd8cache_expire' => $expire,
'd8cache_data' => $data,
);
$this->backend
->set($cid, $data, $expire);
}
}
public function clear($cid = NULL, $wildcard = FALSE) {
$this->backend
->clear($cid, $wildcard);
}
public function isEmpty() {
return FALSE;
}
protected function mergeExpireWithMaxAge($expire, $max_age) {
if ($expire == CACHE_TEMPORARY) {
return $expire;
}
if ($max_age === CACHE_MAX_AGE_PERMANENT) {
return $expire;
}
if ($expire == CACHE_PERMANENT) {
return REQUEST_TIME + $max_age;
}
return REQUEST_TIME + min(REQUEST_TIME - $expire, $max_age);
}
protected function getCurrentChecksum(array $tags) {
if (!$this
->ensureInstalled()) {
return 0;
}
return d8cache_cache_tags_get_current_checksum($tags);
}
protected function checksumValid($checksum, array $tags) {
if (!$this
->ensureInstalled()) {
return TRUE;
}
return d8cache_cache_tags_is_valid($checksum, $tags);
}
public function ensureInstalled() {
static $installStateKnown = FALSE;
static $isInstalled = TRUE;
if ($this->bin == 'cache_page' && drupal_get_bootstrap_phase() < DRUPAL_BOOTSTRAP_FULL) {
require_once __DIR__ . '/d8cache.module';
return TRUE;
}
if ($installStateKnown) {
return $isInstalled;
}
if (!module_exists('d8cache')) {
watchdog('d8cache', 'D8Cache Backend is in use, but the d8cache module has not been enabled! Please enable it to ensure cache tags work properly.', array(), WATCHDOG_ALERT, 'admin/modules');
$isInstalled = FALSE;
}
$installStateKnown = TRUE;
return $isInstalled;
}
}