You are here

function cache_set in Memcache API and Integration 6

Same name in this branch
  1. 6 memcache.db.inc \cache_set()
  2. 6 memcache.inc \cache_set()
Same name and namespace in other branches
  1. 5.2 memcache.db.inc \cache_set()
  2. 5.2 memcache.inc \cache_set()
  3. 5 memcache.db.inc \cache_set()
  4. 5 memcache.inc \cache_set()

Store data in memcache.

Parameters

$cid: The cache ID of the data to store.

$data: The data to store in the cache. Complex data types will be automatically serialized before insertion. Strings will be stored as plain text and not serialized.

$table: The table $table to store the data in. Valid core values are 'cache_filter', 'cache_menu', 'cache_page', or 'cache'.

$expire: One of the following values:

  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.
  • CACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.
  • A Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

$headers: A string containing HTTP header information for cached pages.

9 calls to cache_set()
cache_get in ./memcache.db.inc
Return data from the persistent cache. Data may be stored as either plain text or as serialized data. cache_get will automatically return unserialized objects and arrays.
MemCacheClearCase::clearCidTest in tests/memcache.test
Test clearing using a cid.
MemCacheClearCase::clearWildcardPrefixTest in tests/memcache.test
Test cache clears using wildcard prefixes.
MemCacheClearCase::testClearCacheLifetime in tests/memcache.test
Test full bin flushes with cache lifetime.
MemCacheClearCase::testClearWildcardFull in tests/memcache.test
Test full bin flushes with no cache lifetime.

... See full list

File

./memcache.inc, line 187

Code

function cache_set($cid, $data, $table = 'cache', $expire = CACHE_PERMANENT, $headers = NULL) {

  // Handle database fallback first.
  $bins = variable_get('memcache_bins', array());
  if (!is_null($table) && isset($bins[$table]) && $bins[$table] == 'database') {
    return _cache_set($cid, $data, $table, $expire, $headers);
  }

  // The created time should always be set as late as possible, this is
  // especially true immediately after a full bin flush, so use time() here
  // instead of request time.
  $created = time();

  // Create new cache object.
  $cache = new stdClass();
  $cache->cid = $cid;
  $cache->data = is_object($data) ? memcache_clone($data) : $data;
  $cache->created = $created;
  $cache->headers = $headers;

  // Record the previous number of wildcard flushes affecting our cid.
  $cache->flushes = memcache_wildcard_flushes($cid, $table);
  if ($expire == CACHE_TEMPORARY) {

    // Convert CACHE_TEMPORARY (-1) into something that will live in memcache
    // until the next flush.
    $cache->expire = $_SERVER['REQUEST_TIME'] + 2591999;
  }
  else {
    if ($expire != CACHE_PERMANENT && $expire < 2592000) {

      // Expire is expressed in seconds, convert to the proper future timestamp
      // as expected in dmemcache_get().
      $cache->expire = $_SERVER['REQUEST_TIME'] + $expire;
    }
    else {
      $cache->expire = $expire;
    }
  }

  // Manually track the expire time in $cache->expire.  When the object
  // expires, if stampede protection is enabled, it may be served while one
  // process rebuilds it. The ttl sent to memcache is set to the expire twice
  // as long into the future, this allows old items to be expired by memcache
  // rather than evicted along with a sufficient period for stampede protection
  // to continue to work.
  if ($cache->expire == CACHE_PERMANENT) {
    $memcache_expire = $cache->expire;
  }
  else {
    $memcache_expire = $cache->expire + ($cache->expire - $_SERVER['REQUEST_TIME']) * 2;
  }
  dmemcache_set($cid, $cache, $memcache_expire, $table);
  if (isset($GLOBALS['locks']["memcache_{$cid}:{$table}"])) {
    lock_release("memcache_{$cid}:{$table}");
  }
}