You are here

function ad_get_cache in Advertisement 7.3

Return the eventually cached results of a callback

Parameters

string $callback: The callback to call if the result was not found in the cache.

mixed $args: (optional). The parameter(s) to be passed to the callback. Defaults to an empty array.

Return value

array The result of the call. Any object will be turned into an array, to avoid issues with autoloaders when a full bootstrap was not performed.

Throws

AdCacheNotFoundException Throw a cache not found exception if the bootstrap phase is not full. This exception will be caught by the ad.php script, which will then initiate a full bootstrap.

5 calls to ad_get_cache()
ad_get_ads in ./ad.module
Callback for the Ajax request to get ads.
ad_get_advertisement in ./ad.module
Load an Ad node from the cache.
ad_get_cached_view in ./ad.module
Return a view, eventually from the cache.
ad_get_info in ./ad.module
Return the info about the node type(s) that implement ads.
ad_get_rendered_node in ./ad.module
Return a node rendered in a certain view mode, possibly from the cache.

File

./ad.module, line 422
Core code for the ad module.

Code

function ad_get_cache($callback, $args = array()) {
  $results =& drupal_static(__FUNCTION__);
  if (!is_array($args)) {
    $args = array(
      $args,
    );
  }
  $cid = implode(':', array_merge(array(
    'ad',
    $callback,
  ), _ad_flatten($args)));
  if (strlen($cid) > 255) {

    // We're building the cid dynamically, so we can't be sure it will be less
    // than 255 chars, which is the max length when using the db cache backend.
    // Therefore cut the cid, and replace the last part with a hash; this will
    // hide part of the cid, but it will avoid errors while guaranteeing

    //// uniqueness.
    $hash = hash('sha256', $cid);
    $cid = substr($cid, 0, 255 - strlen($hash)) . $hash;
  }
  $cache_lifetime = variable_get('ad_cache_lifetime', 0);
  if (!isset($results[$cid])) {
    if ($cache_lifetime > 0) {
      $cache = cache_get($cid);
      if (empty($cache) || $cache->expire < REQUEST_TIME) {
        ad_bootstrap_full();
        $results[$cid] = ad_object_to_array(call_user_func_array($callback, $args));
        cache_set($cid, $results[$cid], 'cache', REQUEST_TIME + $cache_lifetime);
      }
      else {
        $results[$cid] = $cache->data;
      }
    }
    else {
      ad_bootstrap_full();
      $result = call_user_func_array($callback, $args);
      $results[$cid] = ad_object_to_array($result);
    }
  }
  return $results[$cid];
}