Version 1
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / ReplicaDatabaseIgnoreSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Drupal\Core\Database\Database;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10 /**
11  * System subscriber for controller requests.
12  */
13 class ReplicaDatabaseIgnoreSubscriber implements EventSubscriberInterface {
14
15   /**
16    * Checks and disables the replica database server if appropriate.
17    *
18    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
19    *   The Event to process.
20    */
21   public function checkReplicaServer(GetResponseEvent $event) {
22     // Ignore replica database servers for this request.
23     //
24     // In Drupal's distributed database structure, new data is written to the
25     // master and then propagated to the replica servers.  This means there is a
26     // lag between when data is written to the master and when it is available
27     // on the replica. At these times, we will want to avoid using a replica server
28     // temporarily. For example, if a user posts a new node then we want to
29     // disable the replica server for that user temporarily to allow the replica
30     // server to catch up.
31     // That way, that user will see their changes immediately while for other
32     // users we still get the benefits of having a replica server, just with
33     // slightly stale data.  Code that wants to disable the replica server should
34     // use the db_set_ignore_replica() function to set
35     // $_SESSION['ignore_replica_server'] to the timestamp after which the replica
36     // can be re-enabled.
37     if (isset($_SESSION['ignore_replica_server'])) {
38       if ($_SESSION['ignore_replica_server'] >= REQUEST_TIME) {
39         Database::ignoreTarget('default', 'replica');
40       }
41       else {
42         unset($_SESSION['ignore_replica_server']);
43       }
44     }
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function getSubscribedEvents() {
51     $events[KernelEvents::REQUEST][] = ['checkReplicaServer'];
52     return $events;
53   }
54
55 }