You are here

function DrupalAPCCache::set in APC - Alternative PHP Cache 7

Stores data in the persistent cache.

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. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

$expire: (optional) Controls the maximum lifetime of this cache entry. Note that caches might be subject to clearing at any time, so this setting does not guarantee a minimum lifetime. With this in mind, the cache should not be used for data that must be kept during a cache clear, like sessions.

Use 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.

Overrides DrupalCacheInterface::set

File

./drupal_apc_cache.inc, line 203
This integrates the drupal APC cache backend.

Class

DrupalAPCCache
APC cache implementation.

Code

function set($cid, $data, $expire = CACHE_PERMANENT, array $headers = NULL) {

  // Add set to statistics.
  $GLOBALS['apc_statistics'][] = array(
    'set',
    $this->bin,
    $cid,
  );

  // Create new cache object.
  $cache = new stdClass();
  $cache->cid = $cid;

  // APC will serialize any structure we give itself.
  $cache->serialized = 0;
  $cache->created = REQUEST_TIME;
  $cache->expire = $expire;
  $cache->headers = isset($headers) ? $headers : NULL;
  $cache->data = $data;

  // What kind of expiration is being used.
  switch ($expire) {
    case CACHE_PERMANENT:
      $set_result = apc_store($this
        ->key($cid), $cache);
      break;
    case CACHE_TEMPORARY:
      if (variable_get('cache_lifetime', 0) > 0) {
        $set_result = apc_store($this
          ->key($cid), $cache, variable_get('cache_lifetime', 0));
      }
      else {
        $set_result = apc_store($this
          ->key($cid), $cache);
      }
      break;
    default:
      $set_result = apc_store($this
        ->key($cid), $cache, $expire - time());
      break;
  }
}