ChainedFastBackend example

class ChainedFastBackendTest extends GenericCacheBackendUnitTestBase {

  /** * Creates a new instance of ChainedFastBackend. * * @return \Drupal\Core\Cache\ChainedFastBackend * A new ChainedFastBackend object. */
  protected function createCacheBackend($bin) {
    $consistent_backend = new DatabaseBackend(\Drupal::service('database'), \Drupal::service('cache_tags.invalidator.checksum')$bin, 100);
    $fast_backend = new PhpBackend($bin, \Drupal::service('cache_tags.invalidator.checksum'));
    $backend = new ChainedFastBackend($consistent_backend$fast_backend$bin);
    // Explicitly register the cache bin as it can not work through the     // cache bin list in the container.     \Drupal::service('cache_tags.invalidator')->addInvalidator($backend);
    return $backend;
  }

}
// Use the request time because that is what we will be comparing against.     $timestamp_item = (object) ['cid' => $timestamp_cid, 'data' => (int) $_SERVER['REQUEST_TIME'] - 60];
    $consistent_cache->expects($this->once())
      ->method('get')->with($timestamp_cid)
      ->willReturn($timestamp_item);
    $consistent_cache->expects($this->never())
      ->method('getMultiple');

    $fast_cache = new MemoryBackend();
    $fast_cache->set('foo', 'baz');

    $chained_fast_backend = new ChainedFastBackend(
      $consistent_cache,
      $fast_cache,
      'foo'
    );
    $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
  }

  /** * Tests a fast cache miss gets data from the consistent cache backend. */
  public function testFallThroughToConsistentCache() {
    

  public function get($bin) {
    // Use the chained backend only if there is a fast backend available and it     // is not the same as the consistent backend; otherwise, just return the     // consistent backend directly.     if (
      isset($this->fastServiceName)
      &&
      $this->fastServiceName !== $this->consistentServiceName
    ) {
      return new ChainedFastBackend(
        $this->container->get($this->consistentServiceName)->get($bin),
        $this->container->get($this->fastServiceName)->get($bin),
        $bin
      );
    }
    else {
      return $this->container->get($this->consistentServiceName)->get($bin);
    }
  }

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