- ๐ฎ๐ณ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
- ๐บ๐ธUnited States balsama boston
Setting this back to NW for PHPCS.
- ๐ฎ๐ณ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.
- ๐ฎ๐ณIndia rajeshreeputra Pune
Implemented cache bin in latest commit. Requesting review.
- ๐ฎ๐ณIndia rajeshreeputra Pune
Thank you @mxr576,
This is kinda great learning while working on this issue about choosing between
memory cache
andcache 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. - ๐ฎ๐ณ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 fromgetKeyValue()
andgetKeyValues()
methods as cache is now being updated / deleted in CRUD operations and ensuring it always returns the correct value. Hence, RTBC.