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

browser cache #841

Merged
merged 4 commits into from
Feb 6, 2025
Merged

browser cache #841

merged 4 commits into from
Feb 6, 2025

Conversation

HansKallekleiv
Copy link
Collaborator

@HansKallekleiv HansKallekleiv commented Jan 7, 2025

  • Added browser-cache middleware to set max-age and stale-while-revalidate time.
  • Default cache for all endpoints set in config.py (currently max-age: 1h, stale-time: 24h).
  • Added two decorators that can be used in endpoints
    • @no_cache and @add_custom_cache_time(max_age:int, stale_while_revalidate:int)

@anders-kiaer
Copy link
Collaborator

anders-kiaer commented Jan 16, 2025

I'm confident this will increase the perceived performance for users re-visiting the application. 👍

I think we also want to give our endpoints the possibility to both define how long a cache is valid before a new response should be fetched (max-age) but also how long a previously cached response can directly be shown to the user (stale-while-revalidate).

The way I understand how this flag can be used: E.g. for the (slow) well trajectory endpoint, we could have a max-age of one week and a stale-while-revalidate of many weeks. If the user revisits webviz after 10 days he/she will still get to see the trajectories immediately (since it less than stale-while-revalidate), and the browser will behind the scenes update the cache such that next time the cache is visited it has a fresh instance.

https://web.dev/articles/stale-while-revalidate

@HansKallekleiv HansKallekleiv marked this pull request as ready for review January 28, 2025 11:45
print(f"{RESOURCE_SCOPES_DICT=}")

DEFAULT_CACHE_MAX_AGE = 3600 # 1 hour
DEFAULT_STALE_WHILE_REVALIDATE = 3600 # 1 hour
Copy link
Collaborator

@anders-kiaer anders-kiaer Jan 29, 2025

Choose a reason for hiding this comment

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

Looks good. Just thinking loud, since most of the data we request/aggregate on is quite static (e.g. calculations performed on a completed FMU run): Should we have the defaults to be react-query-staleTime <= react-query-cacheTime << DEFAULT_CACHE_MAX_AGE << DEFAULT_STALE_WHILE_REVALIDATA?

The "stale while revalidate" increases the time window where we have a browser cache hit (= where we immediately show data). At the same time, the user will not be stuck with "old" data in cache longer than max-age (pending a first request after max-age, and a subsequent React component re-render/re-mount and timeout of React Query staleTime). It would be interesting to see how this dynamic and potential upside turns out in practice, if the default is to have stale-while-revalidate (significantly) larger than max-age.

Copy link
Collaborator

@anders-kiaer anders-kiaer left a comment

Choose a reason for hiding this comment

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

LGTM! This will be interesting to see the effect of in practice! ⏩


from . import schemas

router = APIRouter()


@router.get("/fields")
@no_cache
Copy link
Collaborator

Choose a reason for hiding this comment

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

This probably could be cached. It very rarely changes - and when it does the user has to wait for access to sync through infrastructure and Azure anyway. E.g. max-age of one minute and a default stale-while-revalidate of one day?

@anders-kiaer anders-kiaer linked an issue Jan 31, 2025 that may be closed by this pull request
@HansKallekleiv HansKallekleiv merged commit b5b49f4 into equinor:main Feb 6, 2025
7 checks passed
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.

Browser cache
2 participants