You are here

protected function S3fsStream::readCache in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x src/StreamWrapper/S3fsStream.php \Drupal\s3fs\StreamWrapper\S3fsStream::readCache()

Fetch an object from the file metadata cache table.

Parameters

string $uri: The uri of the resource to check.

Return value

array An array of metadata if the $uri is in the cache. Otherwise, FALSE.

4 calls to S3fsStream::readCache()
S3fsStream::getExternalUrl in src/StreamWrapper/S3fsStream.php
Returns a web accessible URL for the resource.
S3fsStream::getS3fsObject in src/StreamWrapper/S3fsStream.php
Try to fetch an object from the metadata cache.
S3fsStream::mkdir in src/StreamWrapper/S3fsStream.php
Support for mkdir().
S3fsStream::rename in src/StreamWrapper/S3fsStream.php
Support for rename().

File

src/StreamWrapper/S3fsStream.php, line 1298

Class

S3fsStream
Defines a Drupal s3 (s3://) stream wrapper class.

Namespace

Drupal\s3fs\StreamWrapper

Code

protected function readCache($uri) {
  $uri = $this->streamWrapperManager
    ->normalizeUri($uri);

  // Cache DB reads so that faster caching mechanisms (e.g. redis, memcache)
  // can further improve performance.
  $cid = S3FS_CACHE_PREFIX . $uri;
  $cache = \Drupal::cache(S3FS_CACHE_BIN);
  if ($cached = $cache
    ->get($cid)) {
    $record = $cached->data;
  }
  else {
    $lock = \Drupal::lock();

    // Cache miss. Avoid a stampede.
    if (!$lock
      ->acquire($cid, 1)) {

      // Another request is building the variable cache. Wait, then re-run
      // this function.
      $lock
        ->wait($cid);
      $record = $this
        ->readCache($uri);
    }
    else {
      $record = \Drupal::database()
        ->select('s3fs_file', 's')
        ->fields('s')
        ->condition('uri', $uri, '=')
        ->execute()
        ->fetchAssoc();
      $cache
        ->set($cid, $record, Cache::PERMANENT, [
        S3FS_CACHE_TAG,
      ]);
      $lock
        ->release($cid);
    }
  }
  return $record ? $record : FALSE;
}