-
Notifications
You must be signed in to change notification settings - Fork 333
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,14 @@ class Base(Configuration): | |
* DB_USER | ||
""" | ||
|
||
DEBUG = False | ||
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) | ||
|
||
USE_SWAGGER = False | ||
|
||
API_VERSION = "v1.0" | ||
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Yes in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As long as it is only theme customization we are caching. In an attempt to achieve the same functionality on the backend |
||
|
||
|
||
class Build(Base): | ||
"""Settings used when the application is built. | ||
|
@@ -795,6 +805,17 @@ class Development(Base): | |
CSRF_TRUSTED_ORIGINS = ["http://localhost:8072", "http://localhost:3000"] | ||
DEBUG = True | ||
|
||
# Enable debug namespaces as needed | ||
# | ||
# DEBUG_NAMESPACES = [ | ||
# 'no-cache', | ||
# 'features:language' | ||
# ] | ||
# | ||
# They can be enabled in all environments | ||
# via DEBUG environment variable | ||
# DEBUG="features:language,no-cache,..." | ||
|
||
SESSION_COOKIE_NAME = "impress_sessionid" | ||
|
||
USE_SWAGGER = True | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,11 @@ | ||||||||||
import { CunninghamProvider } from '@openfun/cunningham-react'; | ||||||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||||||||||
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'; | ||||||||||
import { QueryClient } from '@tanstack/react-query'; | ||||||||||
import type { Persister } from '@tanstack/react-query-persist-client'; | ||||||||||
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'; | ||||||||||
import debug from 'debug'; | ||||||||||
import { useRouter } from 'next/router'; | ||||||||||
import { useEffect } from 'react'; | ||||||||||
import { useEffect, useMemo } from 'react'; | ||||||||||
|
||||||||||
import { useCunninghamTheme } from '@/cunningham'; | ||||||||||
import { Auth, KEY_AUTH, setAuthUrl } from '@/features/auth'; | ||||||||||
|
@@ -13,12 +17,16 @@ import { ConfigProvider } from './config/'; | |||||||||
* QueryClient: | ||||||||||
* - defaultOptions: | ||||||||||
* - staleTime: | ||||||||||
* - global cache duration - we decided 3 minutes | ||||||||||
* - It can be overridden to each query | ||||||||||
* - global time until cache is considered stale and will be refetched in the background | ||||||||||
* - instant if debug flag "no-cache" active - 3 minutes otherwise | ||||||||||
* - gcTime: | ||||||||||
* - global time until cache is purged from the persister and needs to be renewed | ||||||||||
* - since its cached in localStorage, we can set it to a long time (48h) | ||||||||||
*/ | ||||||||||
const defaultOptions = { | ||||||||||
queries: { | ||||||||||
staleTime: 1000 * 60 * 3, | ||||||||||
staleTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 3, // 3 minutes | ||||||||||
gcTime: debug.enabled('no-cache') ? 0 : 1000 * 60 * 60 * 48, // 48 hours | ||||||||||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. After if you don't want cache, we could set these variables from settings:
Suggested change
By default they will be in
but in
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 commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback.
|
||||||||||
retry: 1, | ||||||||||
}, | ||||||||||
}; | ||||||||||
|
@@ -29,11 +37,21 @@ const queryClient = new QueryClient({ | |||||||||
export function AppProvider({ children }: { children: React.ReactNode }) { | ||||||||||
const { theme } = useCunninghamTheme(); | ||||||||||
const { replace } = useRouter(); | ||||||||||
|
||||||||||
const initializeResizeListener = useResponsiveStore( | ||||||||||
(state) => state.initializeResizeListener, | ||||||||||
); | ||||||||||
|
||||||||||
const persister = useMemo(() => { | ||||||||||
// Create persister only when the browser is available | ||||||||||
if (typeof window !== 'undefined') { | ||||||||||
return createSyncStoragePersister({ | ||||||||||
storage: window.localStorage, | ||||||||||
}); | ||||||||||
} | ||||||||||
// Return undefined otherwise (PersistQueryClientProvider handles undefined persister gracefully) | ||||||||||
return undefined; | ||||||||||
}, []); | ||||||||||
|
||||||||||
useEffect(() => { | ||||||||||
return initializeResizeListener(); | ||||||||||
}, [initializeResizeListener]); | ||||||||||
|
@@ -60,12 +78,15 @@ export function AppProvider({ children }: { children: React.ReactNode }) { | |||||||||
}, [replace]); | ||||||||||
|
||||||||||
return ( | ||||||||||
<QueryClientProvider client={queryClient}> | ||||||||||
<PersistQueryClientProvider | ||||||||||
client={queryClient} | ||||||||||
persistOptions={{ persister: persister as Persister }} | ||||||||||
> | ||||||||||
<CunninghamProvider theme={theme}> | ||||||||||
<ConfigProvider> | ||||||||||
<Auth>{children}</Auth> | ||||||||||
</ConfigProvider> | ||||||||||
</CunninghamProvider> | ||||||||||
</QueryClientProvider> | ||||||||||
</PersistQueryClientProvider> | ||||||||||
); | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,28 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) { | |
const getLayout = Component.getLayout ?? ((page) => page); | ||
const { t } = useTranslation(); | ||
|
||
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'); | ||
} | ||
|
||
Comment on lines
+21
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||
return ( | ||
<> | ||
<Head> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4881,29 +4881,56 @@ | |
dependencies: | ||
"@typescript-eslint/utils" "^8.18.1" | ||
|
||
"@tanstack/[email protected]": | ||
version "5.75.4" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.75.4.tgz#e05f2fe4145fb5354271ad19e63eec61f6ce3012" | ||
integrity sha512-pcqOUgWG9oGlzkfRQQMMsEFmtQu0wq81A414CtELZGq+ztVwSTAaoB3AZRAXQJs88LmNMk2YpUKuQbrvzNDyRg== | ||
"@tanstack/[email protected]": | ||
version "5.75.5" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.75.5.tgz#198c27c21b5d15aad09167af8a021f214e194d4b" | ||
integrity sha512-kPDOxtoMn2Ycycb76Givx2fi+2pzo98F9ifHL/NFiahEDpDwSVW6o12PRuQ0lQnBOunhRG5etatAhQij91M3MQ== | ||
|
||
"@tanstack/[email protected]": | ||
version "5.76.0" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.76.0.tgz#3b4d5d34ce307ba0cf7d1a3e90d7adcdc6c46be0" | ||
integrity sha512-FN375hb8ctzfNAlex5gHI6+WDXTNpe0nbxp/d2YJtnP+IBM6OUm7zcaoCW6T63BawGOYZBbKC0iPvr41TteNVg== | ||
|
||
"@tanstack/[email protected]": | ||
version "5.74.7" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-devtools/-/query-devtools-5.74.7.tgz#c9b022b386ac86e6395228b5d6912e6444b3b971" | ||
integrity sha512-nSNlfuGdnHf4yB0S+BoNYOE1o3oAH093weAYZolIHfS2stulyA/gWfSk/9H4ZFk5mAAHb5vNqAeJOmbdcGPEQw== | ||
|
||
"@tanstack/[email protected]": | ||
version "5.76.0" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-persist-client-core/-/query-persist-client-core-5.76.0.tgz#a3bcdd687384dc6b5b61b402bef153ad54515321" | ||
integrity sha512-xcTZjILf4q49Nsl6wcnhBYZ4O0gpnuNwV6vPIEWIrwTuSNWz2zd/g9bc8SxnXy7xCV8SM1H0IJn8KjLQIUb2ag== | ||
dependencies: | ||
"@tanstack/query-core" "5.76.0" | ||
|
||
"@tanstack/[email protected]": | ||
version "5.76.0" | ||
resolved "https://registry.yarnpkg.com/@tanstack/query-sync-storage-persister/-/query-sync-storage-persister-5.76.0.tgz#29643062f1a424873afb22032ce70ee72436bb9b" | ||
integrity sha512-N8d8voY61XkM+jfXTySduLrevD6wRM3pwQ1kG0syLiWWx/sX2+CpaTMSPr0GggjQuhmjhUPo83LaV+e449tizA== | ||
dependencies: | ||
"@tanstack/query-core" "5.76.0" | ||
"@tanstack/query-persist-client-core" "5.76.0" | ||
|
||
"@tanstack/[email protected]": | ||
version "5.75.4" | ||
resolved "https://registry.yarnpkg.com/@tanstack/react-query-devtools/-/react-query-devtools-5.75.4.tgz#89614363d63c997ade81ade4a18e90b57512d4d8" | ||
integrity sha512-CSJZWa316EFtLZtr6RQLAnqWb1MESzyZ7j0bMQjuhYas5FDp/3MA7G6RE4hWauqCCDsNIfIm2Rnm1zJTZVye/w== | ||
dependencies: | ||
"@tanstack/query-devtools" "5.74.7" | ||
|
||
"@tanstack/[email protected]": | ||
version "5.75.4" | ||
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.75.4.tgz#721e1cdf7debb110671f558dc2b6276f637554a5" | ||
integrity sha512-Vf65pzYRkf8fk9SP1ncIZjvaXszBhtsvpf+h45Y/9kOywOrVZfBGUpCdffdsVzbmBzmz6TCFes9bM0d3pRrIsA== | ||
"@tanstack/[email protected]": | ||
version "5.76.0" | ||
resolved "https://registry.yarnpkg.com/@tanstack/react-query-persist-client/-/react-query-persist-client-5.76.0.tgz#97718fec844708cb98a5902d4b1eeb72adea555b" | ||
integrity sha512-QPKgkHX1yC1Ec21FTQHBTbQcHYI+6157DgsmxABp94H7/ZUJ3szZ7wdpdBPQyZ9VxBXlKRN+aNZkOPC90+r/uA== | ||
dependencies: | ||
"@tanstack/query-persist-client-core" "5.76.0" | ||
|
||
"@tanstack/[email protected]": | ||
version "5.75.5" | ||
resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.75.5.tgz#650a6b58c7bcd4c33062426e05c4467571148a4f" | ||
integrity sha512-QrLCJe40BgBVlWdAdf2ZEVJ0cISOuEy/HKupId1aTKU6gPJZVhSvZpH+Si7csRflCJphzlQ77Yx6gUxGW9o0XQ== | ||
dependencies: | ||
"@tanstack/query-core" "5.75.4" | ||
"@tanstack/query-core" "5.75.5" | ||
|
||
"@tanstack/[email protected]": | ||
version "8.20.6" | ||
|
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?