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

Returning cache expired data if there is no network #2502

Closed
juliagarrigos opened this issue Aug 5, 2020 · 7 comments
Closed

Returning cache expired data if there is no network #2502

juliagarrigos opened this issue Aug 5, 2020 · 7 comments

Comments

@juliagarrigos
Copy link

Hi,

I want a query to be cached during 12h, after 12h I want it to make a request to the server. The issue is that I also need to be able to return the data when there is no connection and in some cases, the cache will be expired and will return an error.

This is my code:

apolloClient.query(query)
            .httpCachePolicy(HttpCachePolicy.CACHE_FIRST.expireAfter(12, TimeUnit.HOURS))

I need to configure the cache in such a way that data is only requested to the server every 12h but cached data is returned if the user has no internet (even when 12h passed since the last successful request).

Is there a way to accomplish this? Can I configure the cache to don't remove the expired data (only setting a flag "expired" to it) so it can be reused?

Thanks!

@martinbonnin
Copy link
Contributor

Hi 👋 I'm afraid this is not possible using the Apollo APIs but maybe this can be done using the OkHttp cache. ApolloClient takes a OkHttpClient parameter so you could maybe configure the cache there?

@juliagarrigos
Copy link
Author

Thanks a lot for the quick answer @martinbonnin. I found a very old issue where there is a discussion that could be related to this topic. The issue was closed and the API has changed a lot in the last years, do you know if there are plans to implement something like NETWORK_BEFORE_STALE policy?

Signals the apollo client to first fetch the data from the network request. If the network request fails, then the data is fetched from the http cache even if it has become stale.

@martinbonnin
Copy link
Contributor

There are a lot of improvements we'd like to do to the cache that we're trying to keep track of there: #2331.

Most of them are around the normalized cache at this point so it'll most likely come first.

@juliagarrigos
Copy link
Author

Do you think this change could be done to the HTTP cache? Is there a list of improvements for the HTTP cache?

@martinbonnin
Copy link
Contributor

The above ticket is a mixed bag that lists everything cache-related (HTTP and normalized, memory and SQL, etc...).

Changes to the HTTP cache can definitely be done but I have no idea when that could land.

@martinbonnin
Copy link
Contributor

Then, can you add this to the list? I think is better to have it there and close this issue, what do you think?

It should be there already :-). It's called Network before stale but I can rename if needed.

@juliagarrigos
Copy link
Author

Sorry I just saw it. We can close this issue then. Thanks!!

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

2 participants