Blame view

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

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

export const createStorage = (storage: Storage = sessionStorage, options: Options = {}) => {
  return create(createOptions(storage, options));
};
陈文彬 authored
23
24
25
26
27
28
29
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
30
};
31
陈文彬 authored
32
export default WebStorage;