封装storage存储class
export enum StorageType {
session = 'session',
local = 'local'
}
export class MyStorage {
private prefix: string
private static _instance: Map<string, MyStorage>
private storage: Storage
private engine = {
[StorageType.session]: sessionStorage,
[StorageType.local]: localStorage
}
constructor(prefix = '', type: StorageType = StorageType.local) {
this.prefix = prefix
this.storage = this.engine[type]
if (!this.storage) {
throw new Error("engine type error. it must be 'session','local'")
}
if (!MyStorage._instance) {
MyStorage._instance = new Map()
}
if (!MyStorage._instance?.has(type)) {
MyStorage._instance.set(type, this)
}
return MyStorage._instance.get(type) as MyStorage
}
private getFullKey(value: string) {
return this.prefix + value
}
set(key: string, value: any, expire = 0) {
try {
const wrapData = {
value,
expire,
time: Date.now()
}
const data = JSON.stringify(wrapData)
this.storage.setItem(this.getFullKey(key), data)
return true
} catch (error) {
console.error(error)
return false
}
}
setInto(key: string, value: Record<string, any>, expire = 0) {
try {
const preData = this.get(key)
const wrapData = {
value,
expire,
time: Date.now()
}
if (preData) {
Object.assign(wrapData.value, preData, value)
}
const data = JSON.stringify(wrapData)
this.storage.setItem(this.getFullKey(key), data)
return true
} catch (error) {
console.error(error)
return false
}
}
get(key: string) {
try {
const localData = this.storage.getItem(this.getFullKey(key))
if (localData === null) return localData
const data = JSON.parse(localData)
if (data.expire > 0 && Date.now() - data.time > data.expire * 1000) {
this.remove(key)
return null
}
return data.value
} catch (error) {
console.error(error)
return null
}
}
remove(key: string) {
this.storage.removeItem(this.getFullKey(key))
}
clear(): void {
Object.keys(this.storage).forEach(key => {
if (key.startsWith(this.prefix)) {
this.storage.removeItem(key)
}
})
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
封装 vue hook
import { MyStorage, StorageType } from 'MyStorage.ts'
import { ref} from 'vue'
export const useStorage = (key: string, prefix = 'test_', type = 'local') => {
const storageHandle = new MyStorage(prefix, type as StorageType)
const value = ref(storageHandle.get(key))
const setValue = (storage: any) => {
return (newValue: any) => {
value.value = newValue
storage.set(key, newValue)
}
}
return [value, setValue(storageHandle), storageHandle]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
vue中使用
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30