You are here

function DrupalFileCache::get in File Cache 7

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.

Parameters

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

Return value

The cache or FALSE on failure.

Overrides DrupalCacheInterface::get

1 call to DrupalFileCache::get()
DrupalFileCache::getMultiple in ./filecache.inc
Return data from the persistent cache when given an array of cache IDs.

File

./filecache.inc, line 220
DrupalFileCache class that implements DrupalCacheInterface.

Class

DrupalFileCache

Code

function get($cid) {
  if (!$this->ok || !is_string($cid)) {
    return FALSE;
  }
  $filename = $this->directory . '/' . $this
    ->encode_cid($cid);

  // XXX should be in getMultiple() and get() to call getMultiple()
  $this
    ->delete_flushed();

  // Use @ because cache entry may not exist
  $content = @file_get_contents($filename);
  if ($content === FALSE) {
    return FALSE;
  }
  $cache = @unserialize($content);
  if ($cache === FALSE) {

    // we are in the middle of cache_set
    $fh = fopen($filename, 'rb');
    if ($fh === FALSE) {
      return FALSE;
    }
    if (flock($fh, LOCK_SH) === FALSE) {
      fclose($fh);
      return FALSE;
    }
    $cache = @unserialize(@stream_get_contents($fh));
    if ($cache === FALSE || flock($fh, LOCK_UN) === FALSE || fclose($fh) === FALSE) {
      unlink($filename);

      // remove broken file
      flock($fh, LOCK_UN);
      fclose($fh);
      return FALSE;
    }
  }

  // XXX Should reproduce the cache_lifetime / cache_flush_$bin logic
  $cache_flush = variable_get('filecache_flush_' . $this->bin, 0);
  if ($cache->expire != CACHE_TEMPORARY && $cache->expire != CACHE_PERMANENT && ($cache->expire < REQUEST_TIME || $cache_flush && $cache->created < $cache_flush)) {
    unlink($filename);
    return FALSE;
  }

  // Some systems don't update access time so we do it this way
  // XXX There's a chance that file no longer exists at this point
  // XXX but it's ok because we deal fine with broken cache entries
  // XXX should check only once in a page request if we have such
  // XXX filesystem and set $this->touch so that here we now what to do
  // XXX should be configurable
  // touch($filename);
  // XXX should assert($cache->cid == $cid)
  return $cache;
}