cacheGet example


  protected function getAllDisplayModesByEntityType($display_type) {
    if (!isset($this->displayModeInfo[$display_type])) {
      $key = 'entity_' . $display_type . '_info';
      $entity_type_id = 'entity_' . $display_type;
      $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId();
      if ($cache = $this->cacheGet("$key:$langcode")) {
        $this->displayModeInfo[$display_type] = $cache->data;
      }
      else {
        $this->displayModeInfo[$display_type] = [];
        foreach ($this->entityTypeManager->getStorage($entity_type_id)->loadMultiple() as $display_mode) {
          [$display_mode_entity_type$display_mode_name] = explode('.', $display_mode->id(), 2);
          $this->displayModeInfo[$display_type][$display_mode_entity_type][$display_mode_name] = $display_mode->toArray();
        }
        $this->moduleHandler->alter($key$this->displayModeInfo[$display_type]);
        $this->cacheSet("$key:$langcode", $this->displayModeInfo[$display_type], CacheBackendInterface::CACHE_PERMANENT, ['entity_types', 'entity_field_info']);
      }
    }
->disableOriginalConstructor()
      ->getMock();

    $module_handler = $this->getMockBuilder(ModuleHandlerInterface::class)
      ->getMock();

    $this->noneCache = $this->getMockBuilder(NoneCache::class)
      ->disableOriginalConstructor()
      ->getMock();

    $success_cache = $this->prophesize(CachePluginBase::class);
    $success_cache->cacheGet('results')->willReturn(TRUE);
    $this->successCache = $success_cache->reveal();

    $cache_manager = $this->prophesize(PluginManagerInterface::class);
    $cache_manager->createInstance('none')->willReturn($this->noneCache);

    $translation = $this->getStringTranslationStub();
    $container = new ContainerBuilder();
    $container->set('string_translation', $translation);
    $container->set('views.executable', $this->viewExecutableFactory);
    $container->set('module_handler', $module_handler);
    $container->set('plugin.manager.views.cache', $cache_manager->reveal());
    
$this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  }

  /** * {@inheritdoc} */
  public function getBaseFieldDefinitions($entity_type_id) {
    // Check the static cache.     if (!isset($this->baseFieldDefinitions[$entity_type_id])) {
      // Not prepared, try to load from cache.       $cid = 'entity_base_field_definitions:' . $entity_type_id . ':' . $this->languageManager->getCurrentLanguage()->getId();
      if ($cache = $this->cacheGet($cid)) {
        $this->baseFieldDefinitions[$entity_type_id] = $cache->data;
      }
      else {
        // Rebuild the definitions and put it into the cache.         $this->baseFieldDefinitions[$entity_type_id] = $this->buildBaseFieldDefinitions($entity_type_id);
        $this->cacheSet($cid$this->baseFieldDefinitions[$entity_type_id], Cache::PERMANENT, ['entity_types', 'entity_field_info']);
      }
    }
    return $this->baseFieldDefinitions[$entity_type_id];
  }

  
public function getBundleInfo($entity_type_id) {
    $bundle_info = $this->getAllBundleInfo();
    return $bundle_info[$entity_type_id] ?? [];
  }

  /** * {@inheritdoc} */
  public function getAllBundleInfo() {
    if (empty($this->bundleInfo)) {
      $langcode = $this->languageManager->getCurrentLanguage()->getId();
      if ($cache = $this->cacheGet("entity_bundle_info:$langcode")) {
        $this->bundleInfo = $cache->data;
      }
      else {
        $this->bundleInfo = $this->moduleHandler->invokeAll('entity_bundle_info');
        foreach ($this->entityTypeManager->getDefinitions() as $type => $entity_type) {
          // First look for entity types that act as bundles for others, load them           // and add them as bundles.           if ($bundle_entity_type = $entity_type->getBundleEntityType()) {
            foreach ($this->entityTypeManager->getStorage($bundle_entity_type)->loadMultiple() as $entity) {
              $this->bundleInfo[$type][$entity->id()]['label'] = $entity->label();
            }
          }
/** * Returns the cached plugin definitions of the decorated discovery class. * * @return array|null * On success this will return an array of plugin definitions. On failure * this should return NULL, indicating to other methods that this has not * yet been defined. Success with no values should return as an empty array * and would actually be returned by the getDefinitions() method. */
  protected function getCachedDefinitions() {
    if (!isset($this->definitions) && $cache = $this->cacheGet($this->cacheKey)) {
      $this->definitions = $cache->data;
    }
    return $this->definitions;
  }

  /** * Sets a cache of plugin definitions for the decorated discovery class. * * @param array $definitions * List of definitions to store in cache. */
  
$module_handler->invokeAll('views_pre_execute', [$this]);

    // Check for already-cached results.     /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
    if (!empty($this->live_preview)) {
      $cache = Views::pluginManager('cache')->createInstance('none');
    }
    else {
      $cache = $this->display_handler->getPlugin('cache');
    }

    if ($cache->cacheGet('results')) {
      if ($this->usePager()) {
        $this->pager->total_items = $this->total_rows;
        $this->pager->updatePageInfo();
      }
    }
    else {
      $this->query->execute($this);
      // Enforce the array key rule as documented in       // views_plugin_query::execute().       $this->result = array_values($this->result);
      $this->_postExecute();
      

  public function get($key) {
    if (!$key) {
      throw new \InvalidArgumentException('A valid cache entry key is required. Use getAll() to get all table data.');
    }
    if (!isset($this->storage[$key])) {
      // Prepare a cache ID for get and set.       $cid = $this->baseCid . ':' . $key;
      $from_cache = FALSE;

      if ($data = $this->cacheGet($cid)) {
        $this->storage[$key] = $data->data;
        $from_cache = TRUE;
      }
      // If there is no cached entry and data is not already fully loaded,       // rebuild. This will stop requests for invalid tables calling getData.       elseif (!$this->fullyLoaded) {
        $this->allStorage = $this->getData();
      }

      if (!$from_cache) {
        if (!isset($this->allStorage[$key])) {
          

  public function testTagCaching() {
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $view = Views::getView('test_tag_cache');
    $build = $view->buildRenderable();
    $renderer->renderPlain($build);

    // Saving the view should invalidate the tags.     $cache_plugin = $view->display_handler->getPlugin('cache');
    $this->assertTrue($cache_plugin->cacheGet('results'), 'Results cache found.');
    $this->assertNotEmpty($this->getRenderCache($view), 'Output cache found.');

    $view->storage->save();

    $this->assertFalse($cache_plugin->cacheGet('results'), 'Results cache empty after the view is saved.');
    $this->assertFalse($this->getRenderCache($view), 'Output cache empty after the view is saved.');

    $view->destroy();
    $build = $view->buildRenderable();
    $renderer->renderPlain($build);

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