You are here

function ad_cache_memcache_adcacheapi in Advertisement 5

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

Ad module's adcacheapi _hook().

File

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

Code

function ad_cache_memcache_adcacheapi($op, &$node) {
  switch ($op) {
    case 'method':
      ad_cache_memcache_sync();
      ad_cache_memcache_build();
      return array(
        'memcache' => t('Memcache'),
      );
    case 'description':
      return t('Memcache allows improved performance by caching data directly in RAM.');
    case 'settings':
      $form['memcache'] = array(
        '#type' => 'fieldset',
        '#title' => t('Memcache settings'),
        '#collapsible' => TRUE,
        '#collapsed' => variable_get('ad_cache', 'none') == 'memcache' ? FALSE : TRUE,
      );
      $period = drupal_map_assoc(array(
        60,
        120,
        180,
        240,
        300,
        600,
        900,
        1800,
        2700,
        3600,
        10800,
        21600,
        43200,
        86400,
      ), 'format_interval');
      $form['memcache']['ad_cache_memcache_sync'] = array(
        '#type' => 'select',
        '#title' => t('Sync frequency'),
        '#default_value' => variable_get('ad_cache_memcache_sync', 600),
        '#options' => $period,
        '#description' => t('Specify how often statistics stored in RAM should be synced to the database (requires cron runs with the same or greater frequency). The longer you store data in memcache, the more data you risk loosing in the event of a system failure. This configuration option is only relevant if memcache is enabled.'),
      );

      // Sanity tests...
      if (variable_get('ad_cache', 'none') == 'memcache') {
        $sync = variable_get('ad_cache_memcache_sync', 600);
        $cron_last = variable_get('cron_last', NULL);
        if (is_numeric($cron_last)) {
          if (time() - $cron_last > $sync) {
            drupal_set_message(t('Memcache warning:  your last cron run was !time ago.  Advertisement view data is only synchronized into the database when cron runs.  You are risking data loss.  To learn more about how Drupal cron works, please check the online help pages for <a href="@url">configuring cron jobs</a>.', array(
              '@url' => 'http://drupal.org/cron',
              '!sync' => format_interval($sync),
              '!time' => format_interval(time() - $cron_last),
            )), 'error');
          }
        }
        else {
          drupal_set_message(t('Memcache warning:  Cron has not run.  Advertisement view data is only synchronized into the database when cron runs.  You are risking data loss.  It appears cron jobs have not been setup on your system. Please check the help pages for <a href="@url">configuring cron jobs</a>.', array(
            '@url' => 'http://drupal.org/cron',
          )), 'error');
        }
      }
      return $form;
    case 'settings_submit':
      variable_set('ad_cache_memcache_sync', $node['ad_cache_memcache_sync']);
      break;
    case 'insert':
    case 'update':
    case 'delete':
      if (variable_get('ad_cache', 'none') == 'memcache') {
        ad_cache_memcache_sync_ad($node->nid);
        ad_cache_memcache_build($node);
      }
      break;
  }
}