Skip to content

[Cache] CacheContract doesn't seem to cache unrelated keys in recursive cache calls. #31399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tapetersen opened this issue May 6, 2019 · 2 comments

Comments

@tapetersen
Copy link

Symfony version(s) affected: 4.2.2

Description
When using the new CacheContract interface as explained at https://symfony.com/doc/current/components/cache/cache_pools.html#using-the-cache-contracts using the CacheContract::get() interface all recursive calls made to the same pool fails to save their value and are recomputed even if it's an unrelated cache key.

I do understand that the stampede protection must be disabled for recursive calls for the same key but for unrelated keys this is at least for me unexpected behavior that is hard to diagnose since the calls may not be immediate and in different services.

How to reproduce

I've made a simple test gist that demonstrates the problem at https://gist.github.com/tapetersen/cc72ce3f1aaf689bf1450684e364802f

Possible Solution
If possible only skip the recursion on same key, not all recursive calls.

Additional context
In the Snippet from the test case below I would expect the second call to not be made but to return the cached value.

function functionToCache() {
    echo '<p>Computing value to cache</p>';
    sleep(1);
    return 'value';
}
$pool->get('key1', function(\Symfony\Contracts\Cache\ItemInterface $item) use ($pool) {
    // The first call should actually call the function.
    echo '<p>' . $pool->get('key2',  'functionToCache', 0) . '</p>';
    // Second call should use the now cached value
    echo '<p>' . $pool->get('key2',  'functionToCache', 0) . '</p>';
});
@tapetersen
Copy link
Author

In my case the problem is basically that a service calling an external endpoint is caching the responses but the requests needs an api token that is fetched and cached when needed in the same cache pool. Since this token is not fetched explicitly it's always nested in a CacheContract::get() call and therefore never saved and refetched for every request.

@nicolas-grekas
Copy link
Member

Nice catch, thanks for the detailed description and reproducer.
Here is the fix: #31479

nicolas-grekas added a commit that referenced this issue May 12, 2019
…lls (nicolas-grekas)

This PR was merged into the 4.2 branch.

Discussion
----------

[Cache] fix saving unrelated keys in recursive callback calls

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #31399
| License       | MIT
| Doc PR        | -

Commits
-------

4443c20 [Cache] fix saving unrelated keys in recursive callback calls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants