You are here

public function MemCacheDrupal::set in Memcache API and Integration 7

Implements DrupalCacheInterface::set().

Overrides DrupalCacheInterface::set

File

./memcache.inc, line 238

Class

MemCacheDrupal
Implementation of cache.inc with memcache logic included

Code

public function set($cid, $data, $expire = CACHE_PERMANENT) {
  $created_microtime = round(microtime(TRUE), 3);

  // Positive expiration values of less than 30 days are seconds. Convert to
  // an absolute timestamp.
  if ($expire > 0 && $expire < 2592000) {
    $expire = REQUEST_TIME + $expire;
  }

  // Create new cache object.
  $cache = new stdClass();
  $cache->cid = $cid;
  $cache->data = is_object($data) ? clone $data : $data;
  $cache->created = REQUEST_TIME;
  $cache->created_microtime = $created_microtime;
  $cache->expire = $expire;

  // Record the previous number of wildcard flushes affecting our cid.
  $cache->flushes = $this
    ->wildcardFlushes($cid);

  // Determine stored memcache item lifetime.
  if ($expire == CACHE_PERMANENT) {

    // Set memcache item to never expire.
    $memcache_expire = 0;
  }
  elseif (variable_get('memcache_expire_wait_gc', FALSE) || $expire == CACHE_TEMPORARY) {

    // The cache_set() API states that an item set with a timestamp, once
    // expired, behaves the same as CACHE_TEMPORARY: not removed till next
    // general cache wipe (which is actually a garbage collection on each
    // cache bin). However, the memcache module has traditionally treated
    // timestamp-set items as expiring at that exact time. If
    // memcache_expire_wait_gc is enabled, treat timestamp items the same as
    // CACHE_TEMPORARY, and set the memcache expiration to something that
    // keeps the item valid until the next garbage collection (30 days).
    $memcache_expire = REQUEST_TIME + 2591999;
  }
  elseif (variable_get('memcache_stampede_protection', FALSE)) {

    // If stampede protection is enabled, set the item expiration to twice
    // its intended lifetime. The expired object may be served while one
    // process rebuilds it.
    $memcache_expire = REQUEST_TIME + ($expire - REQUEST_TIME) * 2;
  }
  else {

    // Allow the memcache item to expire at the exact absolute timestamp.
    $memcache_expire = $expire;
  }
  dmemcache_set($cid, $cache, $memcache_expire, $this->bin, $this->memcache);

  // Release lock if acquired in $this->valid().
  $lock = "memcache_{$cid}:{$this->bin}";
  if (variable_get('memcache_stampede_protection', FALSE) && isset($GLOBALS['locks'][$lock])) {
    lock_release("{$lock}");
  }
}