CachedStorage example


  protected $fileStorage;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // Create a directory.     $dir = PublicStream::basePath() . '/config';
    $this->fileStorage = new FileStorage($dir);
    $this->storage = new CachedStorage($this->fileStorage, \Drupal::service('cache.config'));
    $this->cache = \Drupal::service('cache_factory')->get('config');
  }

  /** * {@inheritdoc} */
  public function testInvalidStorage() {
    $this->markTestSkipped('No-op as this test does not make sense');
  }

  /** * {@inheritdoc} */
$prefix = __FUNCTION__;
    $storage = $this->createMock('Drupal\Core\Config\StorageInterface');

    $response = ["$prefix." . $this->randomMachineName(), "$prefix." . $this->randomMachineName()];
    $storage->expects($this->once())
      ->method('listAll')
      ->with($prefix)
      ->willReturn($response);

    $cache = new NullBackend(__FUNCTION__);

    $cachedStorage = new CachedStorage($storage$cache);
    $this->assertEquals($response$cachedStorage->listAll($prefix));
    $this->assertEquals($response$cachedStorage->listAll($prefix));
  }

}
public function __construct(StorageInterface $source_storage, StorageInterface $target_storage) {
    if ($source_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) {
      $source_storage = $source_storage->createCollection(StorageInterface::DEFAULT_COLLECTION);
    }
    if ($target_storage->getCollectionName() !== StorageInterface::DEFAULT_COLLECTION) {
      $target_storage = $target_storage->createCollection(StorageInterface::DEFAULT_COLLECTION);
    }

    // Wrap the storages in a static cache so that multiple reads of the same     // raw configuration object are not costly.     $this->sourceCacheStorage = new MemoryBackend();
    $this->sourceStorage = new CachedStorage(
      $source_storage,
      $this->sourceCacheStorage
    );
    $this->targetCacheStorage = new MemoryBackend();
    $this->targetStorage = new CachedStorage(
      $target_storage,
      $this->targetCacheStorage
    );
    $this->changelist[StorageInterface::DEFAULT_COLLECTION] = $this->getEmptyChangelist();
  }

  
Home | Imprint | This part of the site doesn't use cookies.