Blame view

src/utils/cache/index.ts 1.04 KB
1
import { getStorageShortName } from '/@/utils/env';
Vben authored
2
import { createStorage as create, CreateStorageParams } from './storageCache';
3
import { enableStorageEncryption, DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
陈文彬 authored
4
Vben authored
5
6
7
export type Options = Partial<CreateStorageParams>;

const createOptions = (storage: Storage, options: Options = {}): Options => {
陈文彬 authored
8
  return {
9
10
    // No encryption in debug mode
    hasEncrypt: enableStorageEncryption,
陈文彬 authored
11
12
    storage,
    prefixKey: getStorageShortName(),
Vben authored
13
    ...options,
陈文彬 authored
14
15
  };
};
16
Vben authored
17
18
19
20
21
export const WebStorage = create(createOptions(sessionStorage));

export const createStorage = (storage: Storage = sessionStorage, options: Options = {}) => {
  return create(createOptions(storage, options));
};
陈文彬 authored
22
23
24
25
26
27
28
export const createSessionStorage = (options: Options = {}) => {
  return createStorage(sessionStorage, { ...options, timeout: DEFAULT_CACHE_TIME });
};

export const createLocalStorage = (options: Options = {}) => {
  return createStorage(localStorage, { ...options, timeout: DEFAULT_CACHE_TIME });
陈文彬 authored
29
};
30
陈文彬 authored
31
export default WebStorage;