Skip to content
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

'respect_response_cache_directives' is ignored #91

Closed
nunoplopes opened this issue Mar 3, 2024 · 7 comments
Closed

'respect_response_cache_directives' is ignored #91

nunoplopes opened this issue Mar 3, 2024 · 7 comments

Comments

@nunoplopes
Copy link

We have the following code in CachePlugin.php:

    public static function clientCache(CacheItemPoolInterface $pool, StreamFactoryInterface $streamFactory, array $config = [])
    {
        // Allow caching of private requests
        if (\array_key_exists('respect_response_cache_directives', $config)) {
            $config['respect_response_cache_directives'][] = 'no-cache';
            $config['respect_response_cache_directives'][] = 'max-age';
            $config['respect_response_cache_directives'] = array_unique($config['respect_response_cache_directives']);
        } else {
            $config['respect_response_cache_directives'] = ['no-cache', 'max-age'];
        }

        return new self($pool, $streamFactory, $config);
    }

If I pass a config with 'respect_response_cache_directives' => [], it is ignored and replaced with ['no-cache', 'max-age'].
I wanted it to always cache any page regardless of the server directives. It's impossible to do that right now. I'm not sure what's the intention of the code above.

@dbu
Copy link
Contributor

dbu commented Mar 4, 2024

hm, good question indeed. it looks like one can only add additional directives to respect, but not remove no-cache and max-age. this had been added like this with the original clientCache factory method in 1568bea, so it was always like this.

i see that the serverCache factory method does not tamper with that option, so maybe you could use that? (you still need to set the option to empty array, otherwise the OptionsResolver will add a bunch of directives to respect.

@nunoplopes
Copy link
Author

nunoplopes commented Mar 4, 2024

The issue is that I'm not using php-http directly but through the php github API (https://github.com/KnpLabs/php-github-api).
But not respecting the respect directive seems wrong to me.

@dbu
Copy link
Contributor

dbu commented Mar 4, 2024

oh i see. you could extend the Builder and overwrite the addCache method.

while i agree that the current behaviour is unexpected and strange, i am afraid to change it as a bugfix - it would be significant behaviour change

@dbu
Copy link
Contributor

dbu commented Mar 4, 2024

unfortunate timing btw, we just tagged 2.0 2 weeks ago. otherwise we could have declared it one of the important BC breaks of 2.0.

we could add a new factory method that does not force any of the options and deprecate this one, to have a sane upgrade path. and once that is released, we could change the php-github-api to use the new method. do you want to work on that?

@nunoplopes
Copy link
Author

Sorry for the delay; just got back to this. Unfortunately overriding the addCache method doesn't work because the fields are private.
Would you be open to a patch to add a $config['force_respect'] or similar name to skip the current code that adds no-cache & max-age?

@dbu
Copy link
Contributor

dbu commented Feb 8, 2025

indeed. i see 2 options:

  1. you work with the knplabs github api client to change the builder to use the CachePlugin constructor directly
  2. we do the BC break here in the plugin and release a new major version. actually i think a new major version sounds alright to me.

do you want to do a pull request with the change? i just created the 3.x branch now - the change would be against 3.x
and if you have the time and energy for it, implementing #31 and #47 would be awesome too (not in the same pull request though)

dbu pushed a commit that referenced this issue Feb 12, 2025
…sponse directives (#93)

* fix#91: Change clientCache so it doesn't force no-cache & max-age response directives

* Update CHANGELOG.md
@dbu
Copy link
Contributor

dbu commented Feb 12, 2025

fixed in #93

@dbu dbu closed this as completed Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants