Commit a6a23779 authored by catch's avatar catch
Browse files

task: #3123401 Stop caching route lookups with query parameters or without results

By: kadiiski
By: leoleoasd
By: drclaw
By: catch
By: berdir
By: scott_euser
(cherry picked from commit 9f63dc70)
parent 754909b2
Loading
Loading
Loading
Loading
Loading
+24 −3
Changes for core/lib/Drupal/Core/Routing/RouteProvider.php: 24 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -70,9 +70,11 @@ public function __construct(protected Connection $connection, protected StateInt
   */
  public function getRouteCollectionForRequest(Request $request) {
    // Cache both the system path as well as route parameters and matching
    // routes.
    $cid = $this->getRouteCollectionCacheId($request);
    if ($cached = $this->cache->get($cid)) {
    // routes if the request has no query parameters. Requests with query
    // strings likely have many variations and are often not requested more
    // than once.
    $cid = $this->useRouteCache($request) ? $this->getRouteCollectionCacheId($request) : NULL;
    if ($cid && $cached = $this->cache->get($cid)) {
      $this->currentPath->setPath($cached->data['path'], $request);
      $request->query->replace($cached->data['query']);
      if ($cached->data['routes'] === FALSE) {
@@ -89,16 +91,35 @@ public function getRouteCollectionForRequest(Request $request) {
      // Incoming path processors may also set query parameters.
      $query_parameters = $request->query->all();
      $routes = $this->getRoutesByPath(rtrim($path, '/'));
      // Only cache lookups with results.
      if ($cid && ($routes->all() || $routes->getResources())) {
        $cache_value = [
          'path' => $path,
          'query' => $query_parameters,
          'routes' => $routes->count() === 0 ? FALSE : $routes,
        ];
        $this->cache->set($cid, $cache_value, CacheBackendInterface::CACHE_PERMANENT, ['route_match']);
      }

      return $routes;
    }
  }

  /**
   * Whether the route lookup for the given request should be cached.
   *
   * By default, requests with query parameters are not cached.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @return bool
   *   Whether the lookup should be cached.
   */
  protected function useRouteCache(Request $request): bool {
    return $request->query->count() === 0;
  }

  /**
   * Find the route using the provided route name.
   *
+2 −2
Changes for core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -267,8 +267,8 @@ public function testPathAliasesWithTranslation(): void {
    $this->assertNotAccessiblePaths([$stage_translation_path]);

    $this->switchToWorkspace($stage);
    $this->assertAccessiblePaths([$default_path]);
    $this->assertNotAccessiblePaths([$translation_path, $stage_translation_path]);
    $this->assertAccessiblePaths([$default_path, $translation_path]);
    $this->assertNotAccessiblePaths([$stage_translation_path]);

    // Create a new workspace-specific revision for the translation with a new
    // path alias.
+2
Changes for core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTestAssertions/umamiLogin.sql: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ INSERT INTO "sessions" ("sid", "uid", "hostname", "session", "timestamp") VALUES
SELECT "session" FROM "sessions" WHERE "sid" = "SESSION_ID" LIMIT 0, 1
SELECT * FROM "users_field_data" "u" WHERE "u"."uid" = "13" AND "u"."default_langcode" = 1
SELECT "roles_target_id" FROM "user__roles" WHERE "entity_id" = "13"
SELECT "base_table"."id" AS "id", "base_table"."path" AS "path", "base_table"."alias" AS "alias", "base_table"."langcode" AS "langcode" FROM "path_alias" "base_table" WHERE ("base_table"."status" = 1) AND ("base_table"."alias" LIKE "/user/13" ESCAPE '\\') AND ("base_table"."langcode" IN ("en", "und")) ORDER BY "base_table"."langcode" ASC, "base_table"."id" DESC
SELECT "name", "route", "fit" FROM "router" WHERE "pattern_outline" IN ( "/user/13", "/user/%", "/user" ) AND "number_parts" >= 2
SELECT "base"."uid" AS "uid", "base"."uuid" AS "uuid", "base"."langcode" AS "langcode" FROM "users" "base" WHERE "base"."uid" IN (13)
SELECT "users_field_data".*, "users_field_data"."langcode" AS "users_field_data__langcode", "user__user_picture"."user_picture_target_id" AS "user_picture_target_id", "user__user_picture"."user_picture_alt" AS "user_picture_alt", "user__user_picture"."user_picture_title" AS "user_picture_title", "user__user_picture"."user_picture_width" AS "user_picture_width", "user__user_picture"."user_picture_height" AS "user_picture_height" FROM "users_field_data" "users_field_data" LEFT OUTER JOIN "user__user_picture" "user__user_picture" ON "user__user_picture"."entity_id" = "users_field_data"."uid" AND "user__user_picture"."langcode" = "users_field_data"."langcode" AND "user__user_picture"."deleted" = 0 WHERE "users_field_data"."uid" IN (13)
SELECT "user__roles"."entity_id" AS "id", "user__roles"."langcode" AS "langcode", "user__roles"."roles_target_id" AS "roles_target_id", "user__roles"."delta" AS "roles_delta", "base"."default_langcode" AS "base_default_langcode" FROM "user__roles" "user__roles" INNER JOIN "users_field_data" "base" ON "base"."uid" = "user__roles"."entity_id" AND "base"."langcode" = "user__roles"."langcode" WHERE ("user__roles"."entity_id" IN (13)) AND ("user__roles"."deleted" = 0) ORDER BY "user__roles"."delta" ASC
+3 −3
Changes for core/profiles/demo_umami/tests/src/FunctionalJavascript/MultipleRequestsPerformanceTestAssertions/umamiLogin.yml: 3 added lines, 3 removed lines.
Original line number Diff line number Diff line
QueryCount: 15
CacheGetCount: 97
QueryCount: 17
CacheGetCount: 96
CacheGetCountByBin:
  config: 28
  bootstrap: 17
  discovery: 32
  data: 5
  data: 4
  entity: 4
  routes: 5
  dynamic_page_cache: 1
+8 −19
Changes for core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php: 8 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -593,25 +593,7 @@ public function testRouteCaching(): void {
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:[language]=en:[query_parameters]=foo=bar,2fb8f40115dd1e695cbe23d4f97ce5b1fb697eee:/path/add/one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals(['foo' => 'bar'], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);

    // A path with multivalued query parameters.
    $path = '/path/add/one?foo=bar&foo2[]=bar2&foo2[]=bar3';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:[language]=en:[query_parameters]=foo=bar&foo2%5B0%5D=bar2&foo2%5B1%5D=bar3,2d3a0851c4970a16be1c851a0e9946e6d10a3fe2:/path/add/one');
    $this->assertEquals('/path/add/one', $cache->data['path']);
    $this->assertEquals(
      [
        'foo' => 'bar',
        'foo2' => ['bar2', 'bar3'],
      ],
      $cache->data['query']
    );
    $this->assertCount(3, $cache->data['routes']);
    $this->assertFalse($cache);

    // A path with placeholders.
    $path = '/path/1/one';
@@ -638,6 +620,13 @@ public function testRouteCaching(): void {
    $this->assertEquals([], $cache->data['query']);
    $this->assertCount(3, $cache->data['routes']);

    $path = '/non-existing-path';
    $request = Request::create($path, 'GET');
    $provider->getRouteCollectionForRequest($request);

    $cache = $this->cache->get('route:[language]=en:/non-existing-path');
    $this->assertFalse($cache);

    // Test with a different current language by switching out the default
    // language.
    $swiss = ConfigurableLanguage::createFromLangcode('gsw-berne');