You are here

public function S3fsService::refreshCache in S3 File System 8.3

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

Refreshes the metadata cache.

Iterates over the full list of objects in the s3fs_root_folder within S3 bucket (or the entire bucket, if no root folder has been set), caching their metadata in the database.

It then caches the ancestor folders for those files, since folders are not normally stored as actual objects in S3.

Parameters

array $config: An s3fs configuration array.

Overrides S3fsServiceInterface::refreshCache

File

src/S3fsService.php, line 370

Class

S3fsService
Defines a S3fsService service.

Namespace

Drupal\s3fs

Code

public function refreshCache(array $config) {
  $s3 = $this
    ->getAmazonS3Client($config);
  $args = $this
    ->getListObjectVersionArgs($config);
  try {
    $operation = empty($config['disable_version_sync']) ? "ListObjectVersions" : "ListObjectsV2";

    // Create an paginator that will emit iterators of all of the objects
    // matching the key prefix.
    $paginator = $s3
      ->getPaginator($operation, $args);
  } catch (\Exception $e) {
    watchdog_exception('S3FS', $e);
    $this
      ->messenger()
      ->addStatus($this
      ->t('Error refreshing cache. Please check the logs for more info.'));
    return;
  }
  $file_metadata_list = [];
  $folders = $this
    ->getExistingFolders();
  $this
    ->setupTempTable();
  try {
    foreach ($paginator as $result) {
      if ($result
        ->hasKey('Versions')) {
        foreach ($result
          ->get('Versions') as $s3_metadata) {
          $this
            ->getObjectMetadata($file_metadata_list, $folders, $s3_metadata, $config);

          // Splits the data into manageable parts for the database.
          if (count($file_metadata_list) >= 10000) {
            $this
              ->writeTemporaryMetadata($file_metadata_list, $folders);
          }
        }
      }
      elseif ($result
        ->hasKey('Contents')) {
        foreach ($result
          ->get('Contents') as $s3_metadata) {
          $this
            ->getObjectMetadata($file_metadata_list, $folders, $s3_metadata, $config);

          // Splits the data into manageable parts for the database.
          if (count($file_metadata_list) >= 10000) {
            $this
              ->writeTemporaryMetadata($file_metadata_list, $folders);
          }
        }
      }
    }
  } catch (\Exception $e) {
    watchdog_exception('S3FS', $e);
    $this
      ->messenger()
      ->addStatus($this
      ->t('Error refreshing cache. Please check the logs for more info.'));
    return;
  }

  // The event listener doesn't fire after the last page is done, so we have
  // to write the last page of metadata manually.
  $this
    ->writeTemporaryMetadata($file_metadata_list, $folders);

  // Store the folders in the database.
  $this
    ->writeFolders($folders);

  // Set the final tables.
  $this
    ->setTables();

  // Clear every s3fs entry in the Drupal cache.
  Cache::invalidateTags([
    S3FS_CACHE_TAG,
  ]);
  $this
    ->messenger()
    ->addStatus($this
    ->t('S3 File System cache refreshed.'));
}