Split Up Pattern Definition Cache by Library to Reduce Collisions

Created on 16 November 2023, about 1 year ago
Updated 17 November 2023, about 1 year ago

Problem/Motivation

When the pattern discovery cache is empty, but multiple requests attempt to render patterns at the same time, the processing-intensive pattern discovery process may happen in multiple threads simultaneously. If the requests differ in which libraries need discovery, the cache entry may also be overwritten between the processes without capturing all discovery data.

A first pass at mitigating this issue will focus on breaking up the existing cache bin containing all discovered patterns into independent cache bins per library. This will ideally help with collisions and overwrites, but cache locking at this level may end up still being necessary after some testing since most sites focus heavily on a single library.

Steps to reproduce

  1. Install a Drupal site and enable the Patternkit and Patternkit Example modules
  2. Enable layout builder and node-specific overrides on a content type
  3. Rebuild all caches: drush cr
  4. Create a test Node and customize the layout for the page
  5. Create Patternkit block content on the page using any patterns
  6. Save the layout
  7. Review the discovery cache table (cache_discovery) in the database for the following:
    • Before
      • All pattern definitions are contained in a serialized string within the patternkit.patterns
        entry
    • After
      • Pattern definitions for each pattern on the site should be contained in separate cache entries like patternkit_pattern_definitions:patternkit

Proposed resolution

  • Reimplement caching of pattern definitions into PatternDiscovery service using a model similar to plugin cache discovery instead of the CacheCollector implementation currently in use by the PatternDiscoveryCollector service that stores everything in memory and then a single cache bin
  • Simplify caching out of the PatternDiscoveryCollector service and mark it as internal as an implementation mechanic
  • Move debug mode and database cache bypassing support into the reimplemented PatternDiscovery service (See #3306679: Add ability to disable pattern cache for active development .)

Remaining tasks

Code review and testing

User interface changes

None

API changes

  • PatternDiscoveryCollector service (patternkit.pattern.discovery.collector) is removed and replaced with the private PatternDiscoveryLoader service (patternkit.pattern.discovery.loader)

Data model changes

  • The collector service's cache entry (patternkit.patterns) in the discovery cache is no longer created or used.
  • The PatternDiscovery service (patternkit.pattern.discovery) now caches each library's pattern definitions in a separate entry of the format: patternkit_pattern_definitions:<library> like patternkit_pattern_definitions:patternkit
📌 Task
Status

Fixed

Version

9.1

Component

Module Core

Created by

🇺🇸United States slucero Arkansas

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024