Skip to content

Commit

Permalink
feat(user): add cache cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Jul 4, 2023
1 parent eb4ebda commit 05f930b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/GZCTF/ClientApp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useLocalStorage } from '@mantine/hooks'
import { ModalsProvider } from '@mantine/modals'
import { Notifications } from '@mantine/notifications'
import { ThemeOverride } from '@Utils/ThemeOverride'
import { localStorageProvider, useBanner } from '@Utils/useConfig'
import { useLocalStorageCache, useBanner } from '@Utils/useConfig'
import { fetcher } from '@Api'

export const App: FC = () => {
Expand All @@ -29,6 +29,8 @@ export const App: FC = () => {

useBanner()

const { localCacheProvider } = useLocalStorageCache()

return (
<ColorSchemeProvider colorScheme={colorScheme} toggleColorScheme={toggleColorScheme}>
<MantineProvider withGlobalStyles withCSSVariables theme={{ ...ThemeOverride, colorScheme }}>
Expand All @@ -38,7 +40,7 @@ export const App: FC = () => {
<SWRConfig
value={{
refreshInterval: 10000,
provider: localStorageProvider,
provider: localCacheProvider,
fetcher,
}}
>
Expand Down
6 changes: 6 additions & 0 deletions src/GZCTF/ClientApp/src/components/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
mdiAccountCircleOutline,
mdiLogout,
mdiAccountGroupOutline,
mdiCached,
} from '@mdi/js'
import { Icon } from '@mdi/react'
import { useIsMobile } from '@Utils/ThemeOverride'
import { useLocalStorageCache } from '@Utils/useConfig'
import { useLoginOut, useUser } from '@Utils/useUser'
import LogoHeader from './LogoHeader'

Expand All @@ -31,6 +33,7 @@ const AppHeader: FC = () => {
const navigate = useNavigate()

const { colorScheme, toggleColorScheme } = useMantineColorScheme()
const { clearLocalCache } = useLocalStorageCache()
const { user, error } = useUser()

const logout = useLoginOut()
Expand Down Expand Up @@ -61,6 +64,9 @@ const AppHeader: FC = () => {
>
用户信息
</Menu.Item>
<Menu.Item onClick={clearLocalCache} icon={<Icon path={mdiCached} size={1} />}>
清除缓存
</Menu.Item>
<Menu.Item color="red" onClick={logout} icon={<Icon path={mdiLogout} size={1} />}>
登出
</Menu.Item>
Expand Down
7 changes: 6 additions & 1 deletion src/GZCTF/ClientApp/src/components/AppNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {
mdiLogout,
mdiWrenchOutline,
mdiNoteTextOutline,
mdiCached,
} from '@mdi/js'
import { Icon } from '@mdi/react'
import { useLocalStorageCache } from '@Utils/useConfig'
import { useLoginOut, useUser } from '@Utils/useUser'
import { Role } from '@Api'
import MainIcon from './icon/MainIcon'
Expand Down Expand Up @@ -137,7 +139,7 @@ const AppNavbar: FC = () => {
const { colorScheme, toggleColorScheme } = useMantineColorScheme()

const logout = useLoginOut()

const { clearLocalCache } = useLocalStorageCache()
const { user, error } = useUser()

useEffect(() => {
Expand Down Expand Up @@ -214,6 +216,9 @@ const AppNavbar: FC = () => {
>
用户信息
</Menu.Item>
<Menu.Item onClick={clearLocalCache} icon={<Icon path={mdiCached} size={1} />}>
清除缓存
</Menu.Item>
<Menu.Item color="red" onClick={logout} icon={<Icon path={mdiLogout} size={1} />}>
登出
</Menu.Item>
Expand Down
26 changes: 20 additions & 6 deletions src/GZCTF/ClientApp/src/utils/useConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,28 @@ export const useBanner = () => {
}, [])
}

export const localStorageProvider = () => {
export const useLocalStorageCache = () => {
const cacheKey = 'gzctf-cache'
const map = new Map(JSON.parse(LZString.decompress(localStorage.getItem(cacheKey) || '') || '[]'))

window.addEventListener('beforeunload', () => {
const appCache = LZString.compress(JSON.stringify(Array.from(map.entries())))
const mapRef = useRef(
new Map(JSON.parse(LZString.decompress(localStorage.getItem(cacheKey) || '') || '[]'))
)

const saveCache = () => {
const appCache = LZString.compress(JSON.stringify(Array.from(mapRef.current.entries())))
localStorage.setItem(cacheKey, appCache)
})
}

const localCacheProvider = () => {
window.addEventListener('beforeunload', saveCache)
return mapRef.current as Cache
}

const clearLocalCache = () => {
window.removeEventListener('beforeunload', saveCache)
localStorage.removeItem('gzctf-cache')
window.location.reload()
}

return map as Cache
return { localCacheProvider, clearLocalCache }
}

0 comments on commit 05f930b

Please sign in to comment.