useCache.ts 838 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * 配置浏览器本地存储的方式,可直接存储对象数组。
  3. */
  4. import WebStorageCache from 'web-storage-cache'
  5. type CacheType = 'localStorage' | 'sessionStorage'
  6. export const CACHE_KEY = {
  7. // 用户相关
  8. ROLE_ROUTERS: 'roleRouters',
  9. USER: 'user',
  10. // 系统设置
  11. IS_DARK: 'isDark',
  12. LANG: 'lang',
  13. THEME: 'theme',
  14. LAYOUT: 'layout',
  15. DICT_CACHE: 'dictCache',
  16. // 登录表单
  17. LoginForm: 'loginForm',
  18. TenantId: 'tenantId'
  19. }
  20. export const useCache = (type: CacheType = 'localStorage') => {
  21. const wsCache: WebStorageCache = new WebStorageCache({
  22. storage: type
  23. })
  24. return {
  25. wsCache
  26. }
  27. }
  28. export const deleteUserCache = () => {
  29. const { wsCache } = useCache()
  30. wsCache.delete(CACHE_KEY.USER)
  31. wsCache.delete(CACHE_KEY.ROLE_ROUTERS)
  32. // 注意,不要清理 LoginForm 登录表单
  33. }