Static cache does not get invalidated

Created on 13 July 2018, almost 7 years ago
Updated 22 April 2025, about 2 months 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
    about 2 months 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
    about 1 month ago
    Total: 149s
    #481973
  • Pipeline finished with Success
    about 1 month ago
    Total: 172s
    #483541
  • Pipeline finished with Success
    about 1 month ago
    Total: 163s
    #484591
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rajeshreeputra Pune

    Requesting review.

  • Pipeline finished with Success
    about 1 month ago
    Total: 226s
    #484607
  • Pipeline finished with Success
    about 1 month ago
    Total: 210s
    #485375
  • Pipeline finished with Success
    about 1 month ago
    Total: 190s
    #485406
  • Pipeline finished with Running
    about 1 month ago
    #485678
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rajeshreeputra Pune

    Implemented cache bin in latest commit. Requesting review.

  • Pipeline finished with Failed
    about 1 month ago
    #485708
  • Pipeline finished with Success
    about 1 month ago
    Total: 252s
    #486433
  • Pipeline finished with Success
    about 1 month ago
    Total: 218s
    #487011
  • Pipeline finished with Success
    about 1 month ago
    Total: 211s
    #487014
  • Pipeline finished with Success
    about 1 month ago
    Total: 466s
    #487037
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia rajeshreeputra Pune

    Thank you @mxr576,

    This is kinda great learning while working on this issue about choosing between memory cache and cache bin.
    It's all about asking the right questions on how sensitive and secure the data needs to be. We need to think, "Is this data okay to just be around for a bit to keep it safe or stored for longer?".
    Memory cache is fine for quick stuff, but if you need it saved, then cache bin is there, just need to be cautious about where the data might end up.
    So yeah, itโ€™s all about figuring out whatโ€™s most important for the situation, and then choose accordingly.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 321s
    #487216
  • Pipeline finished with Success
    about 1 month ago
    Total: 168s
    #487224
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia vishalkhode

    Reviewed changes, overall looks good to me. A PHUnit test covering above scenario would be good or we should handle separately ? As we can also then remove the $reset parameter from getKeyValue() and getKeyValues() methods as cache is now being updated / deleted in CRUD operations and ensuring it always returns the correct value. Hence, RTBC.

  • Pipeline finished with Success
    about 1 month ago
    Total: 154s
    #490944
Production build 0.71.5 2024