-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix 4484: reduce the Query count via InMemoryCache
- Loading branch information
Daniel Robert
authored and
Daniel Robert
committed
Sep 11, 2023
1 parent
324f845
commit 87ea075
Showing
6 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as Apollo from '@apollo/client' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { ForumPostOrderByInput } from '@/common/api/queries' | ||
import { useLocalStorage } from '@/common/hooks/useLocalStorage' | ||
import { GetForumPostsDocument } from '@/forum/queries' | ||
|
||
const orderBy = [ForumPostOrderByInput.UpdatedAtDesc] | ||
|
||
const useCacheWithExpiration = (key: string): Apollo.WatchQueryFetchPolicy => { | ||
const [lastCacheTime, setLastCacheTime] = useLocalStorage<string>(key) | ||
const [fetchPolicy, setFetchPolicy] = useState<Apollo.WatchQueryFetchPolicy>('cache-first') | ||
|
||
useEffect(() => { | ||
const currentDate = new Date() | ||
|
||
try { | ||
if (lastCacheTime != undefined) { | ||
const lastCacheDate = new Date(lastCacheTime) | ||
const timeDiff = Math.abs(currentDate.getTime() - lastCacheDate.getTime()) | ||
|
||
if (timeDiff < 1000 * 3600 * 24) { | ||
setFetchPolicy('cache-first') | ||
return | ||
} | ||
} | ||
} catch (err) { | ||
// ignore issue | ||
} | ||
|
||
setFetchPolicy('cache-and-network') | ||
setLastCacheTime(currentDate.toDateString()) | ||
}, [lastCacheTime]) | ||
|
||
return fetchPolicy | ||
} | ||
|
||
export const useForumLatestPosts = () => { | ||
const fetchPolicy = useCacheWithExpiration('last_LatestPosts') | ||
|
||
const where = {} | ||
const { data } = Apollo.useQuery(GetForumPostsDocument, { | ||
variables: { where, orderBy, limit: 500 }, | ||
fetchPolicy: fetchPolicy, | ||
}) | ||
|
||
return { posts: data?.forumPosts } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters