You are here

function _dfp_token_replace_cache in Doubleclick for Publishers (DFP) 7

Same name and namespace in other branches
  1. 7.2 dfp.module \_dfp_token_replace_cache()

Helper to store and retrieve tokens from cache.

Parameters

$text:

null $tag:

array $options:

Return value

mixed

1 call to _dfp_token_replace_cache()
dfp_token_replace in ./dfp.module
Replaces all tokens in a given string with appropriate values, with caching.

File

./dfp.module, line 904

Code

function _dfp_token_replace_cache($text, $data, array $options = array()) {
  $processed_strings =& drupal_static(__FUNCTION__, NULL);

  // Determine the cache key for this text string. That way we can cache
  // reliably.
  $key = _dfp_token_replace_make_key($text, $data);

  // Use the same granularity as the blocks.
  $cache_item = DFP_TOKEN_CACHE . ':' . implode(':', drupal_render_cid_parts(DRUPAL_CACHE_PER_PAGE));

  // Lookup any already-cached token replacements.
  if (is_null($processed_strings)) {
    $cache = cache_get($cache_item, 'cache');
    $processed_strings = $cache ? $cache->data : array();
  }

  // If the processed string we're looking for isn't already in the cache,
  // then, and only then, do we call the expensive token_replace() (and cache
  // the result).
  if (!isset($processed_strings[$key]) || is_null($processed_strings[$key])) {

    // Regenerate this particular replacement.
    $processed_strings[$key] = token_replace($text, $data, $options);
    $lifetime = variable_get('dfp_token_cache_lifetime', 0);
    $expire_at = $lifetime == 0 ? CACHE_TEMPORARY : REQUEST_TIME + $lifetime;
    cache_set($cache_item, $processed_strings, 'cache', $expire_at);
  }
  return $processed_strings[$key];
}