mergeCacheMaxAge example


  public function checkAccess(AccountInterface $account) {
    // The access result is uncacheable because it is just limiting access to     // the migrate UI which is not worth caching.     return AccessResultAllowed::allowedIf((int) $account->id() === 1)->mergeCacheMaxAge(0);
  }

}
/** * {@inheritdoc} */
  public function addCacheContexts(array $cache_contexts) {
    return $this->storage->addCacheContexts($cache_contexts);
  }

  /** * {@inheritdoc} */
  public function mergeCacheMaxAge($max_age) {
    return $this->storage->mergeCacheMaxAge($max_age);
  }

  /** * {@inheritdoc} */
  public function getCacheTagsToInvalidate() {
    return $this->storage->getCacheTagsToInvalidate();
  }

  /** * {@inheritdoc} */
'id' => 'system_powered_by_block',
      'label' => 'Test block title',
      'label_display' => 'visible',
    ]));
    return [$section];
  }

  /** * {@inheritdoc} */
  public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
    $cacheability->mergeCacheMaxAge(0);
    return \Drupal::state()->get('layout_builder_test_state', FALSE);
  }

  /** * {@inheritdoc} */
  public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
    throw new \RuntimeException(__METHOD__ . " not implemented for " . __CLASS__);
  }

  /** * {@inheritdoc} */
/** * @covers ::getCacheMaxAge * @covers ::mergeCacheMaxAge */
  public function testCacheMaxAge() {
    // Cache max age is permanent by default.     $this->assertEquals(Cache::PERMANENT, $this->entity->getCacheMaxAge());

    // Set two cache max ages, the lower value is the one that needs to be     // returned.     $this->entity->mergeCacheMaxAge(600);
    $this->entity->mergeCacheMaxAge(1800);
    $this->assertEquals(600, $this->entity->getCacheMaxAge());
  }

}

class EntityBaseTest extends EntityBase {
  public $id;
  public $langcode;
  public $uuid;
  public $label;

}
trait RefinableCacheableDependencyTrait {

  use CacheableDependencyTrait;

  /** * {@inheritdoc} */
  public function addCacheableDependency($other_object) {
    if ($other_object instanceof CacheableDependencyInterface) {
      $this->addCacheContexts($other_object->getCacheContexts());
      $this->addCacheTags($other_object->getCacheTags());
      $this->mergeCacheMaxAge($other_object->getCacheMaxAge());
    }
    else {
      // Not a cacheable dependency, this can not be cached.       $this->cacheMaxAge = 0;
    }
    return $this;
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.