-
Notifications
You must be signed in to change notification settings - Fork 9
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
Fix search cache #57
Fix search cache #57
Conversation
8dd4fbb
to
0340c3d
Compare
0340c3d
to
05b662d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the strategy to prevent double-caching works, creating s new custom directive can become complex because you'll have one more moving part to handle from the client-side POV.
Also, I think problem lies deeper in the architecture: re-usage of list
. So we should tackle it directly.
I can see 2 options:
(A) list-no-cache
A new list method with no cache (eg. def _list_no_cache
) will be reused by the cached list (def list
) and the search (def search
) and each one of them have their own dedicated cache.
(B) don't reuse list
Simply don't reuse list
at all. Copy its logic to the search
.
At first glance it might seem against DRY, but search might have it's own custom logic that differs from the default list
, which is actually good.
The code itself is quite simple.
8bc8e35
to
973f5bb
Compare
973f5bb
to
dddd0e2
Compare
drf_kit/cache.py
Outdated
elif request.META.get("X-Bypass-Cache"): | ||
valid_cache_control = False | ||
response_dict = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this too from the previous approach.
dddd0e2
to
6c42ab7
Compare
Having a view method calling another adds complexity to the viewset. For example, it is harder to handle cache if two different methods have their own cache rules. This change aims to make it easier for upcoming changes to deal with view methods gracefully.
In addition to new cache strategy, new ViewSets were created to support specific cache-searching funcionality
6c42ab7
to
0ed89b2
Compare
Context
Cache policy does not take into account parameters in the request body. It should not, as this is an anti-pattern. However,
search
requests use a proxy tolist
method, which, contrary tosearch
, supports cache.The issue
The problem is that the proxified request still use the arguments passed in the body, just like
search
request received it. And as cache does not look at body arguments, asearch
request that looked like this:Will have a cache hit with the following request (which is completely different from the previous):
Solution
The solution has two layers:
key_func
forsearch
requests. Thiskey_func
takes into account the body passed to the request, so calls for the same url, but with different bodies will now map to different keys, avoiding unexpected cache hits.