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

Cache requests client side #40

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

WilliamMcCumstie
Copy link
Contributor

The client now caches its API requests by using the inbuilt mechanism within use-fetch.

I initially implemented the CacheControl headers server side, to allow greater control over how it works. Unfortunately, use-fetch ignores these headers. The commit was reverted here: 29ab890


The cache needs to be persisted so it gets added to the local_storage. This allows for the cache to be destroyed on non-GET requests.

Currently the entire cache is being cleared on such a requests. This is for two reasons:

  • The includes for related requests will be correct, and
  • The local_storage keys are hard to predicted as the contain the URL and query string

e.g. url:/dev/job-scripts/api/v0/scripts?include=template||method:GET||body::

@benarmston
Copy link
Collaborator

benarmston commented Jul 8, 2021

Unfortunately, use-fetch ignores [cache-control] headers.

Well that's disappointing.

@benarmston
Copy link
Collaborator

@WilliamMcCumstie what you had here was a good start, but it wasn't quite right when it came to clearing the cache.

  1. The cache was being cleared too early. See 3908f79 commit message for an explanation.
  2. use-http needed to be updated. The version we were using had a bug where frequently accessed caches would never expire.
  3. We need to also clear the cache whenever the user logs out. This is now done in flight-web-components itself. See Clear cache on logout flight-webapp-components#2.
  4. I've also made a stylistic change of preferring 10 * 60 * 1000 over 600000 to express 10 minutes in ms.

WilliamMcCumstie and others added 10 commits July 8, 2021 17:30
This reverts commit 72084ad.
This reverts commit 21d1f69.
POST/PUT/PATCH/DELETE requests trigger the cache to
be cleared
AKA dividing by 60,000 is too hard for Ben.
Without this we don't pull in the bug fix that has been blocking us
adding this.  The new version allows caches to expire even if they are
being used.
`useFetch` has a couple of different ways of being called.

1. Pass an array of dependencies as the third argument to `useFetch`.
   This will cause the request to be made immediately and again each
   time the given array of dependencies changes.  This is how we make
   GET requests.

2. Do not pass a third arguments to `useFetch`.  The request will not be
   executed until `patch`, `put`, `del`, `get` or `post` is called on
   the request object.  This is how we make `POST` requests.

The important thing to not above is that we cannot clear the cache when
we construct the request in the second form, we need to wait until the
request is executed.

I've added an response interceptor which clears the cache when the
relevant requests are exectued.  It only does so if the response is OK.

I've also changed the way in which the cache was being cleared.  We now
clear the cache via the `useFetch` API.  We can get a copy of the entire
cache by calling `useFetch` without a path/url.

I've also made sure that we clear both the memory and persisted caches.
Probably overkill.
@benarmston benarmston force-pushed the dev/cache-control branch from 6b44cf7 to f58db16 Compare July 8, 2021 16:31
@benarmston
Copy link
Collaborator

Rebased on latest master.

@benarmston
Copy link
Collaborator

Unfortunately, when caching is used use-http caches non-2xx responses including 5xx responses. I've opened an issue. When that is dealt with we can look at this again.

Unfortunately, we need to construct the cache key, as use-http doesn't
expose that.
@benarmston benarmston force-pushed the master branch 2 times, most recently from 6069d25 to 4a8efc3 Compare October 28, 2021 10:47
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

Successfully merging this pull request may close these issues.

2 participants