- 🇮🇳India vishalkhode
To fix the issue, I think we can do the following:
- Update the method to also update the static cache. For example:
$key_values = &drupal_static('getKeyValue', []); $key_values[$this->id()] = $key_value;
- 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()]);
- 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 ? - Add / Update the PHPUnit tests to cover all above scenarios.
- Update the method to also update the static cache. For example:
- First commit to issue fork.
- 🇺🇸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:
- Replace the function-level `drupal_static()` cache with a class-level static property (`static::$keyValues`)
- Update `getKeyValue()` to use this class-level cache
- Invalidate the cache in both `setKeyValue()` and `deleteKeyValue()` methods
- 🇮🇳India rajeshreeputra Pune
We should replace the
drupal_static()
function withentity.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()
- Properly removes the line
- Merge request !46Resolve #2985590 "Fix: Static cache does not get invalidated using memory cache." → (Open) created by rajeshreeputra
- 🇮🇳India rajeshreeputra Pune
rajeshreeputra → changed the visibility of the branch 2985590-static-cache-does to hidden.