You are here

function ad_cache_memcache_sync_ad in Advertisement 6

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

Syncronize counts for given advertisement with database.

2 calls to ad_cache_memcache_sync_ad()
ad_cache_memcache_adcacheapi in cache/memcache/ad_cache_memcache.module
Implementation of hook_adcacheapi().
ad_cache_memcache_sync in cache/memcache/ad_cache_memcache.module
Load advertisements into memory.

File

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

Code

function ad_cache_memcache_sync_ad($aid) {
  if (($error = ad_cache_memcache_requirements()) !== TRUE) {
    drupal_set_message(t('!module: Unable to syncronize cache: !error', array(
      '!module' => 'ad_cache_memcache.module',
      '!error' => $error,
    )), 'error');
    return;
  }
  if (!ad_memcache_lock("ad-counters-{$aid}")) {

    // Another process is already updating these values.
    return;
  }
  $counters = ad_memcache_get("ad-counters-{$aid}");
  if (!is_array($counters)) {
    ad_memcache_unlock("ad-counters-{$aid}");

    // There's nothing currently in memory for this ad.
    return;
  }
  ad_memcache_delete("ad-counters-{$aid}");
  ad_memcache_unlock("ad-counters-{$aid}");
  foreach ($counters as $map) {
    list($action, $group, $hostid, $timestamp) = explode(':', $map);
    if ($action && $group && $hostid && $timestamp) {
      $count = ad_memcache_get("ad-{$action}-{$aid}-{$group}-{$hostid}-{$timestamp}");
      if ($count) {
        ad_memcache_decrement("ad-{$action}-{$aid}-{$group}-{$hostid}-{$timestamp}", $count);
        db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $count, $aid, $action, $timestamp, $group, $hostid);

        // If column doesn't already exist, we need to add it.
        if (!db_affected_rows()) {
          db_query("INSERT INTO {ad_statistics} (aid, date, action, adgroup, hostid, count) VALUES(%d, %d, '%s', '%s', '%s', %d)", $aid, $timestamp, $action, $group, $hostid, $count);

          // If another process already added this row our INSERT will fail, if
          // so we still need to increment it so we don't loose a count.
          if (!db_affected_rows()) {
            db_query("UPDATE {ad_statistics} SET count = count + %d WHERE aid = %d AND action = '%s' AND date = %d AND adgroup = '%s' AND hostid = '%s'", $count, $aid, $action, $timestamp, $group, $hostid);
          }
        }
      }

      // If counting ad impressions, see if we've hit a limit
      if ($action = 'view') {
        $limits = db_fetch_object(db_query('SELECT activated, maxviews, maxclicks, adstatus FROM {ads} WHERE aid = %d', $aid));
        if ($limits->adstatus == 'active') {
          if ($limits->maxviews) {
            $views = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'view' AND date >= %d", $aid, date('YmdH', $limits->activated)));
            if ($views >= $limits->maxviews) {
              db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
              ad_statistics_increment($aid, 'autoexpired');
              ad_statistics_increment($aid, 'expired');
            }
          }
          if ($limits->maxclicks) {
            $clicks = (int) db_result(db_query("SELECT SUM(count) FROM {ad_statistics} WHERE aid = %d AND action = 'click' AND date >= %d", $aid, date('YmdH', $limits->activated)));
            if ($clicks >= $limits->maxclicks) {
              db_query("UPDATE {ads} SET adstatus = 'expired', autoexpire = 0, autoexpired = %d, expired = %d WHERE aid = %d", time(), time(), $aid);
              ad_statistics_increment($aid, 'autoexpired');
              ad_statistics_increment($aid, 'expired');
            }
          }
        }
      }
    }
  }
}