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

Invalidate cached URL #853

Closed
shaunchurch opened this issue May 21, 2014 · 16 comments · Fixed by #1252
Closed

Invalidate cached URL #853

shaunchurch opened this issue May 21, 2014 · 16 comments · Fixed by #1252
Labels
enhancement Feature not a bug
Milestone

Comments

@shaunchurch
Copy link

I'd like to be able invalidate an API route / method in the cache.

I have a cached response from /api/route. When I take some action in my app which updates the data on the server, I know that the previous response from /api/route is now invalid, and I need to ensure next time I call that route a network call is made to get the new data.

I'm currently working around this by calling the route with Cache-Control: no-cache set, which seems to work to force a network request and then caches the new response next time it's called without no-cache, but obviously this is not ideal, causing network activity which might never be required (or might then be again invalidated by taking action before /api/route is called again).

I'm using OKHttp as a client in retrofit. See: http://stackoverflow.com/questions/23782531/how-to-invalidate-force-update-of-cache-route-at-next-request-with-retrofit-and

@swankjesse swankjesse added this to the 3.0 milestone May 23, 2014
@swankjesse
Copy link
Collaborator

Yup, we should offer this. Not sure what the API will look like yet.

@swankjesse swankjesse changed the title Invalidate cache route, forcing update over network at next request Invalidate cached URL May 25, 2014
@swankjesse swankjesse modified the milestones: 2.1, 3.0 May 25, 2014
@swankjesse swankjesse modified the milestones: 2.2, 2.1 Nov 5, 2014
@swankjesse
Copy link
Collaborator

We probably need a mechanism to enumerate the cache.

@andrejvanderzee
Copy link

Facing exactly the same issue here. I have to force the URL not to be cached at all with Cache-Control: no-store. Is this work in progress? :)

@andrejvanderzee
Copy link

Actually, it looks like Cache-Control: no-store in the request header is not implemented.

Quote from RFC: "If sent in a request, a cache MUST NOT store any part of either this request or any response to it."

Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2

@swankjesse
Copy link
Collaborator

@andrejvanderzee we fixed that in 2.1.

@andrejvanderzee
Copy link

Yes thanks confirmed, it is working in 2.1 :)

The milestone for invalidating cached URLs changed to 2.2. Will this be in 2014?

@swankjesse
Copy link
Collaborator

@andrejvanderzee invaliding a specific URL is pretty easy, but what I think most developers will want is a mechanism to invalidate a full URL pattern, like /account/*. What's your preference?

@andrejvanderzee
Copy link

At this moment I need a specific URL path. For example, suppose I open a screen with text messages with my user ID with the following URL:

@get(/messages/user/{userId}")
Messages getMessages(@path("userId") int userId);

Then I post a message, leave the screen and come back before Cache-Control expires. In that case, I will be served the cache and my own message is not included.

One awkward point is indeed that I have to reconstruct the URL with the user id myself. It would be nice to have an easy way to re-construct the URL from the annotated getMessages(), providing just the userId. It is awkward to keep the syntax of the URL in two places.

At this moment I cannot see any use for regular expressions. But I am sure there will be occasions!

@JakeWharton
Copy link
Collaborator

It would be nice to have an easy way to re-construct the URL from the annotated getMessages(), providing just the userId

Note that this is not OkHttp's problem. It only knows URLs.

@andrejvanderzee
Copy link

Yes indeed, its Retrofit.

Nevertheless, invalidating exact URLs would be a good solution.

@niqo01
Copy link

niqo01 commented Dec 5, 2014

what I think most developers will want is a mechanism to invalidate a full URL pattern, like /account/*

We are interested by URL pattern invalidation. I think pagination might be a good example where you want invalidate all page URLs at once.
For example if an user is searching and joining individual events (/events/1), it would be nice to invalidate its own events list (/users/1/events?page=1, /users/1/events?page=2). with an invalidation pattern like /users/1/events*

@alican
Copy link

alican commented Mar 1, 2016

Hello,

is invalidating a specific cached URL possible now?

@swankjesse
Copy link
Collaborator

@alican yep. Use the iterator to iterate the URLs in the cache, then call remove() when you see a URL you don’t want anymore.

@alican
Copy link

alican commented Mar 1, 2016

Thank you Jesse for your answer :)
Have you an example code how to do this with retrofit?
I looked in the javadocs and didint see how do get the URLs from inside an android activity.

@alenz316
Copy link

@swankjesse or @niqo01 Do either of you have a gist of how to do URL pattern invalidation for cache eviction? Like suggested in the previous comment.

@Sternbach-Software
Copy link

Sternbach-Software commented Jun 17, 2021

For a code example, see https://stackoverflow.com/a/68012702/12528345.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature not a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants