You are here

function ad_cache_memcache_build in Advertisement 6

Same name and namespace in other branches
  1. 5.2 cache/memcache/ad_cache_memcache.module \ad_cache_memcache_build()
  2. 5 cache/memcache/ad_cache_memcache.module \ad_cache_memcache_build()

Caches ad information into memory.

3 calls to ad_cache_memcache_build()
ad_cache_memcache_adcacheapi in cache/memcache/ad_cache_memcache.module
Implementation of hook_adcacheapi().
ad_cache_memcache_cron in cache/memcache/ad_cache_memcache.module
Regularily syncronize counters into RAM.
ad_cache_memcache_get_ad in cache/memcache/ad_cache_memcache.inc

File

cache/memcache/ad_cache_memcache.module, line 225
A plug in for the ad.module, integrating the ad module with memcache.

Code

function ad_cache_memcache_build($changed = NULL) {
  variable_set('ad_memcache_build', time());
  if (($error = ad_cache_memcache_requirements()) !== TRUE) {
    drupal_set_message(t('!module: Unable to build cache: !error', array(
      '!module' => 'ad_cache_memcache.module',
      '!error' => $error,
    )), 'error');
    return;
  }
  if (is_object($changed) && isset($changed->aid)) {

    // An advertisement has changed, rebuild cache on next cron run.
    variable_set('ad_memcache_build', '');
  }
  else {

    // Rebuilding entire cache.
    $result = db_query("SELECT aid, adtype, redirect FROM {ads} WHERE adstatus = 'active' OR adstatus = 'approved' OR adstatus = 'offline'");
    while ($ad = db_fetch_object($result)) {
      $node = node_load($ad->aid);
      $ad->display = module_invoke("ad_{$ad->adtype}", 'display_ad', $node);
      ad_memcache_set("ad-aid-{$ad->aid}", $ad);
      $ads[] = $ad->aid;

      // Owner indexes.
      $ad_owners = db_query('SELECT o.uid, h.hostid FROM {ad_owners} o LEFT JOIN {ad_hosts} h ON o.uid = h.uid WHERE aid = %d', $ad->aid);
      $counter = 0;
      while ($owner = db_fetch_object($ad_owners)) {
        $owners[$owner->uid][$ad->aid] = $ad->aid;
        ad_memcache_set("ad-{$ad->aid}-uid", $owner->uid);
      }
      $match = FALSE;

      // Taxonomy index.
      $terms = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $ad->aid);
      while ($term = db_fetch_object($terms)) {
        $taxonomy[$term->tid][$ad->aid] = $ad->aid;
        $match = TRUE;
      }
      if (!$match) {
        $taxonomy[0][] = $ad->aid;
      }
    }
    ad_memcache_set("ad-ads", $ads);
    ad_memcache_set("ad-owners", $owners);
    ad_memcache_set("ad-taxonomy", $taxonomy);

    // HostID index
    $owners = db_query('SELECT uid, hostid FROM {ad_hosts}');
    while ($owner = db_fetch_object($owners)) {
      ad_memcache_set("ad-hosts-{$owner->uid}", $owner->hostid);
      if (($user = user_load(array(
        'uid' => $owner->uid,
      ))) && user_access('host remote advertisements', $user)) {
        ad_memcache_set("ad-hostid-{$owner->hostid}", TRUE);
      }
    }

    // Always invoke hooks, they can decide to queue or act immediately.
    ad_memcache_set('ad-cache-hook', module_invoke_all('ad_build_cache'));
  }
}