You are here

class ReplicaDatabaseIgnoreSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/ReplicaDatabaseIgnoreSubscriber.php \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber

System subscriber for controller requests.

Hierarchy

Expanded class hierarchy of ReplicaDatabaseIgnoreSubscriber

1 file declares its use of ReplicaDatabaseIgnoreSubscriber
IgnoreReplicaSubscriberTest.php in core/modules/system/src/Tests/System/IgnoreReplicaSubscriberTest.php
Contains \Drupal\system\Tests\System\IgnoreReplicaSubscriberTest.
1 string reference to 'ReplicaDatabaseIgnoreSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ReplicaDatabaseIgnoreSubscriber
replica_database_ignore__subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ReplicaDatabaseIgnoreSubscriber.php, line 18
Contains \Drupal\Core\EventSubscriber\ReplicaDatabaseIgnoreSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class ReplicaDatabaseIgnoreSubscriber implements EventSubscriberInterface {

  /**
   * Checks and disables the replica database server if appropriate.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function checkReplicaServer(GetResponseEvent $event) {

    // Ignore replica database servers for this request.
    //
    // In Drupal's distributed database structure, new data is written to the
    // master and then propagated to the replica servers.  This means there is a
    // lag between when data is written to the master and when it is available
    // on the replica. At these times, we will want to avoid using a replica server
    // temporarily. For example, if a user posts a new node then we want to
    // disable the replica server for that user temporarily to allow the replica
    // server to catch up.
    // That way, that user will see their changes immediately while for other
    // users we still get the benefits of having a replica server, just with
    // slightly stale data.  Code that wants to disable the replica server should
    // use the db_set_ignore_replica() function to set
    // $_SESSION['ignore_replica_server'] to the timestamp after which the replica
    // can be re-enabled.
    if (isset($_SESSION['ignore_replica_server'])) {
      if ($_SESSION['ignore_replica_server'] >= REQUEST_TIME) {
        Database::ignoreTarget('default', 'replica');
      }
      else {
        unset($_SESSION['ignore_replica_server']);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array(
      'checkReplicaServer',
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReplicaDatabaseIgnoreSubscriber::checkReplicaServer public function Checks and disables the replica database server if appropriate.
ReplicaDatabaseIgnoreSubscriber::getSubscribedEvents static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents