Unverified Commit a6bb8521 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3190815 by Wim Leers, quietone, NickDickinsonWilde, benjifisher,...

Issue #3190815 by Wim Leers, quietone, NickDickinsonWilde, benjifisher, mikelutz, larowlan: Source count caching broken: impossible to enable source count caching for SqlBase-based source plugins (plus, unneeded cache I/O)

(cherry picked from commit a6124cc0)
parent 4bc7f218
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Cache;

class MemoryCounterBackendFactory implements CacheFactoryInterface {

  /**
   * Instantiated memory cache bins.
   *
   * @var \Drupal\Core\Cache\MemoryBackend[]
   */
  protected $bins = [];

  /**
   * {@inheritdoc}
   */
  public function get($bin) {
    if (!isset($this->bins[$bin])) {
      $this->bins[$bin] = new MemoryCounterBackend();
    }
    return $this->bins[$bin];
  }

}
+13 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Post update functions for migrate.
 */

/**
 * Clear the source count cache.
 */
function migrate_post_update_clear_migrate_source_count_cache() {
  // Empty post_update hook.
}
+14 −22
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\migrate\Plugin\migrate\source;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Plugin\PluginBase;
use Drupal\migrate\Event\MigrateRollbackEvent;
use Drupal\migrate\Event\RollbackAwareInterface;
@@ -246,7 +247,9 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
        $this->$property = (bool) $configuration[$config_key];
      }
    }
    $this->cacheKey = !empty($configuration['cache_key']) ? $configuration['cache_key'] : NULL;
    if ($this->cacheCounts) {
      $this->cacheKey = $configuration['cache_key'] ?? $plugin_id . '-' . hash('sha256', Json::encode($configuration));
    }
    $this->idMap = $this->migration->getIdMap();
    $this->highWaterProperty = !empty($configuration['high_water_property']) ? $configuration['high_water_property'] : FALSE;

@@ -265,7 +268,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
   * Initializes the iterator with the source data.
   *
   * @return \Iterator
   *   Returns an iteratable object of data for this source.
   *   Returns an iterable object of data for this source.
   */
  abstract protected function initializeIterator();

@@ -486,30 +489,19 @@ public function count($refresh = FALSE) {
      return -1;
    }

    if (!isset($this->cacheKey)) {
      $this->cacheKey = hash('sha256', $this->getPluginId());
    }

    // If a refresh is requested, or we're not caching counts, ask the derived
    // class to get the count from the source.
    if ($refresh || !$this->cacheCounts) {
      $count = $this->doCount();
      $this->getCache()->set($this->cacheKey, $count);
    }
    else {
      // Caching is in play, first try to retrieve a cached count.
    // Return the cached count if we are caching counts and a refresh is not
    // requested.
    if ($this->cacheCounts && !$refresh) {
      $cache_object = $this->getCache()->get($this->cacheKey, 'cache');
      if (is_object($cache_object)) {
        // Success.
        $count = $cache_object->data;
        return $cache_object->data;
      }
    }
      else {
        // No cached count, ask the derived class to count 'em up, and cache
        // the result.
    $count = $this->doCount();
    // Update the cache if we are caching counts.
    if ($this->cacheCounts) {
      $this->getCache()->set($this->cacheKey, $count);
    }
    }
    return $count;
  }

+2 −2
Original line number Diff line number Diff line
@@ -383,9 +383,9 @@ protected function fetchNextBatch() {
  abstract public function query();

  /**
   * {@inheritdoc}
   * Gets the source count using countQuery().
   */
  public function count($refresh = FALSE) {
  protected function doCount() {
    return (int) $this->query()->countQuery()->execute()->fetchField();
  }

+5 −0
Original line number Diff line number Diff line
name: Cacheable Embedded Data Test
type: module
description: Module containing a cacheable embedded data source.
package: Testing
version: VERSION
Loading