-
Notifications
You must be signed in to change notification settings - Fork 307
Feature/query caching and debug flags #961
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
base: main
Are you sure you want to change the base?
Feature/query caching and debug flags #961
Conversation
b9cc806
to
436410d
Compare
436410d
to
1084952
Compare
Query-results are automatically stored in localstorage. This leverages caching in a controlled way, improves render-blocking loading times and unnecessary repeated requests. - Adds @tanstack/react-query-persist-client - Adds @tanstack/query-sync-storage-persister - Updates @tanstack/react-query Signed-off-by: Robin Weber <[email protected]>
47ef827
to
867840e
Compare
Debug flags allow the application to behave differently. They can be set in various ways and allow to debug certain aspects of the application. They can ease development and help debug errors in production environments. Debug flags need to be enabled with debug namespace(s). e.g."feature:language" would instruct the application to be more verbose about its language features. e.g. "no-cache" would instruct the application to avoid caching entirely. The value for the debug configuration is always a string with debug namespaces separated by comma. Signed-off-by: Robin Weber <[email protected]>
94f78cb
to
b0de2ee
Compare
@@ -761,6 +768,9 @@ def post_setup(cls): | |||
"OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " | |||
) | |||
|
|||
if cls.DEBUG and "no-cache" in cls.DEBUG_NAMESPACES: | |||
cls.THEME_CUSTOMIZATION_CACHE_TIMEOUT = 0 |
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 directly set this settings in your environment variable IMO
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.
Yes in env.d\development\common
.
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.
As long as it is only theme customization we are caching.
But what if there will be more in the future?
In an attempt to achieve the same functionality on the backend
i found this to be a functional example to illustrate how the debug namespaces would work.
(The frontend has the same functionality)
DEBUG_RAW = values.Value( | ||
default=False, | ||
environ_name="DEBUG", | ||
environ_prefix=None, | ||
) | ||
DEBUG_NAMESPACES = DEBUG_RAW.split(",") if isinstance(DEBUG_RAW, str) else [] | ||
DEBUG = bool(DEBUG_RAW) |
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.
I'm not really confident with this and It's I think error prone.
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.
Agree, it is.
Arguably we can remove the extended DEBUG handling for the backend.
I see too options:
A: Remove the debug handling for the backend
B: Write a dedicated function to handle it solidly
Which to choose?
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.
It is not working as expected, it keeps all the requests in cache, by doing like that:
- the logout is not working anymore
- the doc list is in cache so if someone invite you or send you a public link, it will not appear anymore in the doc list (until the cache is invalidate)
- I guess all the requests that change regularly between multiple users will be in cache, giving lot of side effect during collaboration
What could be interesting is to have by default all the requests not in cache (as we have already), and to decide the ones that we want to have with a persistant cache, like useConfig
.
useConfig
, it has a persistant cache, but we still do the request in background, if the response has some changes, it will rehydrate the components with the new changes.
if (process.env.NODE_ENV === 'development') { | ||
/** | ||
* Enable debug namespaces as needed | ||
* | ||
* They can be enabled: | ||
* | ||
* via DEBUG environment variable | ||
* DEBUG="features:language,no-cache,..." | ||
* | ||
* via browser console | ||
* window.debug = "features:language,no-cache,..."; | ||
* | ||
* via Local storage | ||
* window.localStorage.debug = "features:language,no-cache,..."; | ||
* | ||
* via code (uses Local storage) | ||
* import debug from 'debug'; | ||
* debug.enable('no-cache,features:language,...'); | ||
*/ | ||
//debug.enable('no-cache,features:language'); | ||
} | ||
|
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.
Do you use this part ?
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.
It is just here to illustrate usage for future development and enable quick toggle from code.
staleTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 3, // 3 minutes | ||
gcTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 60 * 48, // 48 hours |
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.
About the debug, I am not sure it is the best way to do.
As a developer I like to be near the production setting, it helps me in this case to see which query I have to invalidate. If I have no-cache
, I will not know that when I change the title of a document I need to invalidate the cache of the document list request.
After if you don't want cache, we could set these variables from settings:
staleTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 3, // 3 minutes | |
gcTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 60 * 48, // 48 hours | |
staleTime: process.env.NEXT_PUBLIC_CACHE_STALE_TIME, | |
gcTime: process.env.NEXT_PUBLIC_CACHE_GC_TIME |
By default they will be in .env
:
NEXT_PUBLIC_CACHE_STALE_TIME=1000 * 60 * 3
NEXT_PUBLIC_CACHE_GC_TIME=1000 * 60 * 60 * 48
but in .env.local
you could set them as you want, .env.local
is not versioned:
NEXT_PUBLIC_CACHE_STALE_TIME=0
NEXT_PUBLIC_CACHE_GC_TIME=0
https://nextjs.org/docs/pages/guides/environment-variables#environment-variable-load-order
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.
Thanks for the feedback.
-
Debug Mode and Cache: The
debug
flag for cache control is off by default, aligning with production behavior. Developers who enable it for local debugging are intentionally altering cache settings for temporary diagnostic purposes. -
Data Updates and Cache Invalidation: For data modifications (e.g., updating a document title), we should adhere to the mutation pattern (as demonstrated here). This ensures that related queries are correctly invalidated and fresh data is fetched.
-
Configurability of
staleTime
andgcTime
:- Making the default
staleTime
andgcTime
configurable via environment variables could introduce complexity. Because eachuseQuery
can override defaults - we would need to add env configuration for eachuseQuery
that has a differentstaleTime
andgcTime
. - Generally, we, as developers designing the queries, are best positioned to determine appropriate
staleTime
andgcTime
values based on the nature of the data. Allowing arbitrary instance-level customization or overly flexible environment configurations might lead to inconsistent caching behavior and unexpected issues. It's preferable to establish sensible, well-reasoned defaults within the codebase for different types of queries.
- Making the default
@@ -761,6 +768,9 @@ def post_setup(cls): | |||
"OIDC_ALLOW_DUPLICATE_EMAILS cannot be set to True simultaneously. " | |||
) | |||
|
|||
if cls.DEBUG and "no-cache" in cls.DEBUG_NAMESPACES: | |||
cls.THEME_CUSTOMIZATION_CACHE_TIMEOUT = 0 |
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.
Yes in env.d\development\common
.
Thanks for the catch! To the points you found i will address the following additional points in the next days:
This is already happening. The staleTime is the time that defines when a query will be refetched. It is just important for us to properly invalidate queries. |
Perfomance Improvement: Adds query caching that is persisted across browser restarts
Developer Exprience Improvement: Adds and documents usage of debug flags