Static cache does not get invalidated

Created on 13 July 2018, almost 7 years ago
Updated 22 April 2025, 5 days ago

Static cache in \Drupal\key\Entity\Key::getKeyValue() method does not gets invalidated when I update an existing entity.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇭🇺Hungary mxr576 Hungary

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India vishalkhode

    To fix the issue, I think we can do the following:

    1. Update the method to also update the static cache. For example:
        $key_values = &drupal_static('getKeyValue', []);
        $key_values[$this->id()] = $key_value;
      
    2. Update the method to remove the value from the static cache as well. For ex:
        $key_values = &drupal_static('getKeyValue');
        unset($key_values[$this->id()]);
      
    3. Lastly, we should consider removing the $reset argument from the method and the related code, since the static cache should now always reflect the correct and updated value. Are there any edge cases where we might still need the flag ?
    4. Add / Update the PHPUnit tests to cover all above scenarios.
  • First commit to issue fork.
  • Merge request !40Resolve #2985590 "Static cache does" → (Open) created by balsama
  • Pipeline finished with Failed
    5 days ago
    Total: 214s
    #479490
  • 🇺🇸United States balsama boston

    Note: The preceding MR was generate with Claude Code

    Summary:

    The Key module uses a static cache in the `getKeyValue()` method to avoid retrieving key values multiple times during a single page request. However, this cache is not invalidated when a key value is updated via `setKeyValue()` or deleted via `deleteKeyValue()`. This causes stale data to be returned if the key is updated and then accessed again within the same request.

    Solution:

    1. Replace the function-level `drupal_static()` cache with a class-level static property (`static::$keyValues`)
    2. Update `getKeyValue()` to use this class-level cache
    3. Invalidate the cache in both `setKeyValue()` and `deleteKeyValue()` methods
  • 🇺🇸United States balsama boston

    Setting this back to NW for PHPCS.

  • 🇮🇳India rajeshreeputra Pune

    We should replace the drupal_static() function with entity.memory_cache service.

    • Properly removes the line $key_values = &drupal_static(__FUNCTION__, []); which was the original static cache implementation
    • Completely eliminates any references to the $key_values array
    • Updates the caching and retrieval logic to consistently use the memory cache service
    • Uses prefixed keys (key_value:$cache_id) to avoid potential collision with other cached data
    • Updates the return statement to get the value from the memory cache service
    • Implements a proper cache deletion method in deleteKeyValue()
  • 🇮🇳India rajeshreeputra Pune

    rajeshreeputra changed the visibility of the branch 2985590-static-cache-does to hidden.

  • Pipeline finished with Failed
    2 days ago
    Total: 149s
    #481973
Production build 0.71.5 2024