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

Add no-store, no-cache in Cache-Control headers #373

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

Bharat23
Copy link

Changes

  • Removed the logic to never use cache if either of no-cache/no-store was present
  • Instead they work as follows
  • no-cache: doesn't use cache even if the value is present but stores the response in the cache
  • no-store: can use cache if present but will not add/update to cache
  • If both are passed, it will neither store nor use the cache. Will remove the max-age and ETag as well.
  • Test cases

Associated Issue: #144

@zozoh94
Copy link

zozoh94 commented Jul 4, 2024

Can I do something to help merging the PR ? I need it in one of my project.

@Bharat23
Copy link
Author

Bharat23 commented Jul 6, 2024

Can I do something to help merging the PR ? I need it in one of my project.

@zozoh94 the owner @long2ice seems to be inactive. Probably adding more 👍 (+1) to the PR by community will help.

@sultaniman
Copy link

Hey @long2ice do you have time to review and push thru this fix please?

@long2ice
Copy link
Owner

long2ice commented Sep 2, 2024

Thanks! Please resolve conflicts.

@sultaniman
Copy link

@Bharat23 @zozoh94 can anyone rebase and resolve conflicts?

@Bharat23
Copy link
Author

Bharat23 commented Sep 7, 2024

Thanks! Please resolve conflicts.

@long2ice resolved the conflict.

headers = {header_key.lower(): header_val for header_key, header_val in request.headers.items()}
cache_control_header = headers.get("cache-control", None)
if cache_control_header:
return set([cache_control_val.strip().lower() for cache_control_val in cache_control_header.split(",")])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be set comprehension imo

{cache_control_val.strip().lower() for cache_control_val in cache_control_header.split(",")}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sultaniman thanks for pointing it out. Pushed the suggested change.

@Bharat23 Bharat23 requested a review from sultaniman September 9, 2024 04:45
returns an empty set if header not set
"""
if request is not None:
headers = {header_key.lower(): header_val for header_key, header_val in request.headers.items()}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can omit lowecasing since FastAPI uses starlette where headers are case-insensitive https://fastapi.tiangolo.com/tutorial/header-params/?h=insensitive#automatic-conversion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it looks redundant if Fastapi takes care of it. I pushed the fix.

headers = {header_key.lower(): header_val for header_key, header_val in request.headers.items()}
cache_control_header = headers.get("cache-control", None)
if cache_control_header:
return {cache_control_val.strip().lower() for cache_control_val in cache_control_header.split(",")}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is case conversion of the header value which Fastapi doesn't convert based on some of my testing

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sorry for confusion, I mean header keys are case insensitive.

Copy link

@sultaniman sultaniman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@sultaniman
Copy link

@long2ice can you please take a look again?

@long2ice long2ice enabled auto-merge September 11, 2024 13:24
@long2ice
Copy link
Owner

Thanks!

@github-actions github-actions bot added the auto-merge Auto-merge enabled label Sep 11, 2024
@sultaniman
Copy link

@Bharat23 @zozoh94 I think we need to make tests pass, thanks to @long2ice for enabling auto merge.

@Bharat23
Copy link
Author

@Bharat23 @zozoh94 I think we need to make tests pass, thanks to @long2ice for enabling auto merge.

I'll take care of them.

@sultaniman
Copy link

Thanks @Bharat23

auto-merge was automatically disabled September 11, 2024 20:53

Head branch was pushed to by a user without write access

@github-actions github-actions bot removed the auto-merge Auto-merge enabled label Sep 11, 2024
@Bharat23
Copy link
Author

@Bharat23 @zozoh94 I think we need to make tests pass, thanks to @long2ice for enabling auto merge.

@sultaniman @long2ice
Had to alter and remove some test cases as they were breaking due to the change introduced in this PR

@@ -23,18 +24,18 @@ def test_datetime() -> None:
assert response.headers.get("X-FastAPI-Cache") == "MISS"
now = response.json().get("now")
now_ = pendulum.now()
assert pendulum.parse(now) == now_
assert pendulum.parse(now).to_atom_string() == now_.to_atom_string()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to convert to_atom_string since we are comparing datetime values?

@sultaniman
Copy link

@long2ice can you please check again once you have time?

@sultaniman
Copy link

sultaniman commented Sep 19, 2024

@Bharat23 can you please ignore mypy warnings for line 26 and 30 in tests/test_decorator.py, I can't push changes into your repo, otherwise I would do it.

@Bharat23
Copy link
Author

@Bharat23 can you please ignore mypy warnings for line 26 and 30 in tests/test_decorator.py, I can't push changes into your repo, otherwise I would do it.

@sultaniman I didn't understand. I am not seeing any mypy issue on 26 and 30. Can you point me to how/where I can see that and what specific error should I ignore?

@sultaniman
Copy link

@Bharat23 I meant the linter failed in test/test_decorator.py I suggested to use per line ignore instead of a global ignore.

@Bharat23
Copy link
Author

@Bharat23 I meant the linter failed in test/test_decorator.py I suggested to use per line ignore instead of a global ignore.

@sultaniman done

@Bharat23
Copy link
Author

Bharat23 commented Oct 1, 2024

@sultaniman @long2ice, can we get this merged please? Thanks

@sultaniman
Copy link

I would also be happy to get this PR done, only @long2ice can merge it.

@long2ice long2ice enabled auto-merge October 2, 2024 07:39
@github-actions github-actions bot added the auto-merge Auto-merge enabled label Oct 2, 2024
@sultaniman
Copy link

@Bharat23 looks like the datetime dependency has minor upgrade and it no longer has to_atom_string method

/home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:16 - error: Type of "to_atom_string" is partially unknown
    Type of "to_atom_string" is "Unknown | (() -> str)" (reportUnknownMemberType)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Date"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Time"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Duration"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
4 errors, 0 warnings, 0 informations 
WARNING: there is a new pyright version available (v1.1.381 -> v1.1.383).

auto-merge was automatically disabled October 13, 2024 00:32

Head branch was pushed to by a user without write access

@github-actions github-actions bot removed the auto-merge Auto-merge enabled label Oct 13, 2024
@Bharat23
Copy link
Author

Bharat23 commented Oct 13, 2024

@Bharat23 looks like the datetime dependency has minor upgrade and it no longer has to_atom_string method

/home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:16 - error: Type of "to_atom_string" is partially unknown
    Type of "to_atom_string" is "Unknown | (() -> str)" (reportUnknownMemberType)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Date"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Time"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
  /home/runner/work/fastapi-cache/fastapi-cache/tests/test_decorator.py:37:20 - error: Cannot access attribute "to_atom_string" for class "Duration"
    Attribute "to_atom_string" is unknown (reportAttributeAccessIssue)
4 errors, 0 warnings, 0 informations 
WARNING: there is a new pyright version available (v1.1.381 -> v1.1.383).

@sultaniman the method exists, this is a mypy issue. There is a mismatch between how the local vs github workflow env is configured because they contradict each other. Ultimately I had to add 2 ignores to satisfy both. Hopefully this is the last commit before this can be merged.

@vicchi vicchi added the enhancement New feature or request label Nov 11, 2024
@vicchi
Copy link
Collaborator

vicchi commented Nov 11, 2024

@Bharat23 @sultaniman Just looking at this PR now ... as it's a change to existing behaviour I'm minded to roadmap this for 1.0.0 which I'll be working on as soon as 0.3.0 is out the door. But happy to be persueded otherwise

@vicchi vicchi added this to the Version 1.0.0a1 milestone Nov 11, 2024
@ermiyaeskandary
Copy link

+1; this brings the package in line with the RFC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants