Skip to content

Commit

Permalink
Remove token info caching, use render/data cache bins, remove cache_t…
Browse files Browse the repository at this point in the history
…oken
  • Loading branch information
Berdir committed Jun 10, 2014
1 parent 27adef0 commit 2aea544
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
64 changes: 26 additions & 38 deletions token.module
Expand Up @@ -304,9 +304,7 @@ function token_field_delete_instance($instance) {
* Clear token caches and static variables.
*/
function token_clear_cache() {
if (db_table_exists('cache_token')) {
\Drupal::cache('cache_token')->flush();
}
\Drupal::token()->resetInfo();
drupal_static_reset('token_get_info');
drupal_static_reset('token_get_global_token_types');
drupal_static_reset('token_get_entity_mapping');
Expand Down Expand Up @@ -435,40 +433,30 @@ function token_get_info($token_type = NULL, $token = NULL) {
$token_info = &$drupal_static_fast['token_info'];

if (empty($token_info)) {
$langcode = \Drupal::languageManager()->getCurrentLanguage()->id;
$cid = "info:{$langcode}";

if ($cache = \Drupal::cache('cache_token')->get($cid)) {
$token_info = $cache->data;
}
else {
$token_info = \Drupal::token()->getInfo();

foreach (array_keys($token_info['types']) as $type_key) {
if (isset($token_info['types'][$type_key]['type'])) {
$base_type = $token_info['types'][$type_key]['type'];
// If this token type extends another token type, then merge in
// the base token type's tokens.
if (isset($token_info['tokens'][$base_type])) {
$token_info['tokens'] += array($type_key => array());
$token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
}
}
else {
// Add a 'type' value to each token type so we can properly use
// token_type_load().
$token_info['types'][$type_key]['type'] = $type_key;
$token_info = \Drupal::token()->getInfo();

// @todo: Move this into the token service and deprecate this function.
foreach (array_keys($token_info['types']) as $type_key) {
if (isset($token_info['types'][$type_key]['type'])) {
$base_type = $token_info['types'][$type_key]['type'];
// If this token type extends another token type, then merge in
// the base token type's tokens.
if (isset($token_info['tokens'][$base_type])) {
$token_info['tokens'] += array($type_key => array());
$token_info['tokens'][$type_key] += $token_info['tokens'][$base_type];
}
}

// Pre-sort tokens.
uasort($token_info['types'], 'token_asort_tokens');
foreach (array_keys($token_info['tokens']) as $type) {
uasort($token_info['tokens'][$type], 'token_asort_tokens');
else {
// Add a 'type' value to each token type so we can properly use
// token_type_load().
$token_info['types'][$type_key]['type'] = $type_key;
}
}

// Store info in cache for future use.
\Drupal::cache('cache_token')->set($cid, $token_info);
// Pre-sort tokens.
uasort($token_info['types'], 'token_asort_tokens');
foreach (array_keys($token_info['tokens']) as $type) {
uasort($token_info['tokens'][$type], 'token_asort_tokens');
}
}

Expand Down Expand Up @@ -829,18 +817,18 @@ function token_build_tree($token_type, array $options = array()) {
}

$langcode = \Drupal::languageManager()->getCurrentLanguage()->id;
$tree_cid = "tree:{$token_type}:{$langcode}:{$options['depth']}";
$tree_cid = "token_tree:{$token_type}:{$langcode}:{$options['depth']}";

// If we do not have this base tree in the static cache, check {cache_token}
// If we do not have this base tree in the static cache, check the cache
// otherwise generate and store it in the cache.
if (!isset($trees[$tree_cid])) {
if ($cache = \Drupal::cache('cache_token')->get($tree_cid)) {
if ($cache = \Drupal::cache('data')->get($tree_cid)) {
$trees[$tree_cid] = $cache->data;
}
else {
$options['parents'] = array();
$trees[$tree_cid] = _token_build_tree($token_type, $options);
\Drupal::cache('cache_token')->set($tree_cid, $trees[$tree_cid]);
\Drupal::cache('data')->set($tree_cid, $trees[$tree_cid]);
}
}

Expand Down Expand Up @@ -1080,7 +1068,7 @@ function token_render_cache_get($elements) {
if (!$cid = drupal_render_cid_create($elements)) {
return FALSE;
}
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'cache';
$bin = isset($elements['#cache']['bin']) ? $elements['#cache']['bin'] : 'render';

if (!empty($cid) && $cache = \Drupal::cache($bin)->get($cid)) {
// Add additional libraries, JavaScript, CSS and other data attached
Expand Down
1 change: 0 additions & 1 deletion token.pages.inc
Expand Up @@ -108,7 +108,6 @@ function theme_token_tree($variables) {
$element = array(
'#cache' => array(
'cid' => 'tree-rendered:' . hash('sha256', serialize(array('token_types' => $token_types, 'global_types' => NULL) + $variables)),
'bin' => 'cache_token',
),
);
if ($cached_output = token_render_cache_get($element)) {
Expand Down
7 changes: 0 additions & 7 deletions token.services.yml
@@ -1,8 +1 @@
services:
cache.cache_token:
class: Drupal\Core\Cache\CacheBackendInterface
tags:
- { name: cache.bin }
factory_method: get
factory_service: cache_factory
arguments: [cache_token]

0 comments on commit 2aea544

Please sign in to comment.