feat: 侧边栏记录

This commit is contained in:
ChenZhaoYu
2023-02-14 11:34:46 +08:00
parent b6e5c59a9c
commit b03f804e35
10 changed files with 177 additions and 36 deletions

View File

@@ -0,0 +1,59 @@
import { deCrypto, enCrypto } from '../crypto'
interface StorageData<T = any> {
value: T
expire: number | null
}
function createLocalStorage() {
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7 // 7 days
function set<T = any>(key: string, value: T, expire: number | null = DEFAULT_CACHE_TIME) {
const storageData: StorageData<T> = {
value,
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
}
const json = enCrypto(storageData)
window.localStorage.setItem(key, json)
}
function get(key: string) {
const json = window.localStorage.getItem(key)
if (json) {
let storageData: StorageData | null = null
try {
storageData = deCrypto(json)
}
catch {
// Prevent failure
}
if (storageData) {
const { value, expire } = storageData
if (expire === null || expire >= Date.now())
return value
}
remove(key)
return null
}
}
function remove(key: string) {
window.localStorage.removeItem(key)
}
function clear() {
window.localStorage.clear()
}
return {
set,
get,
remove,
clear,
}
}
export const ls = createLocalStorage()