本地存儲(chǔ)localStorage設(shè)置過(guò)期時(shí)間示例詳解
思考
在我們使用cookie
的時(shí)候是可以設(shè)置有效期的,但是localStorage
本身是沒(méi)有該機(jī)制的,只能人為的手動(dòng)刪除,否則會(huì)一直存放在瀏覽器當(dāng)中,可不可以跟cookie一樣設(shè)置一個(gè)有效期。如果一直存放在瀏覽器又感覺(jué)有點(diǎn)浪費(fèi),那我們可以把localStorage
進(jìn)行二次封裝實(shí)現(xiàn)該方案。
實(shí)現(xiàn)思路
在存儲(chǔ)的時(shí)候設(shè)置一個(gè)過(guò)期時(shí)間,并且存儲(chǔ)的數(shù)據(jù)進(jìn)行格式化方便統(tǒng)一校驗(yàn),在讀取的時(shí)候獲取當(dāng)前時(shí)間進(jìn)行判斷是否過(guò)期,如果過(guò)期進(jìn)行刪除即可。
代碼實(shí)現(xiàn)
目錄結(jié)構(gòu)
enum ts 定義枚舉
//字典 Dictionaries expire過(guò)期時(shí)間key permanent永久不過(guò)期 export enum Dictionaries { expire = '__expire__', permanent = 'permanent' }
type ts 定義類型
import { Dictionaries } from "../enum" export type Key = string //key類型 export type expire = Dictionaries.permanent | number //有效期類型 export interface Data<T> { //格式化data類型 value: T [Dictionaries.expire]: Dictionaries.expire | number } export interface Result<T> { //返回值類型 message: string, value: T | null } export interface StorageCls { //class方法約束 set: <T>(key: Key, value: T, expire: expire) => void get: <T>(key: Key) => Result<T | null> remove: (key: Key) => void clear: () => void }
index.ts 主要邏輯實(shí)現(xiàn)
import { StorageCls, Key, expire, Data,Result } from "./type"; import { Dictionaries } from "./enum"; export class Storage implements StorageCls { //存儲(chǔ)接受 key value 和過(guò)期時(shí)間 默認(rèn)永久 public set<T = any>(key: Key, value: T, expire: expire = Dictionaries.permanent) { //格式化數(shù)據(jù) const data = { value, [Dictionaries.expire]: expire } //存進(jìn)去 localStorage.setItem(key, JSON.stringify(data)) } public get<T = any>(key: Key):Result<T | null> { const value = localStorage.getItem(key) //讀出來(lái)的數(shù)據(jù)是否有效 if (value) { const obj: Data<T> = JSON.parse(value) const now = new Date().getTime() //有效并且是數(shù)組類型 并且過(guò)期了 進(jìn)行刪除和提示 if (typeof obj[Dictionaries.expire] == 'number' && obj[Dictionaries.expire] < now) { this.remove(key) return { message:`您的${key}已過(guò)期`, value:null } }else{ //否則成功返回 return { message:"成功讀取", value:obj.value } } } else { //否則key值無(wú)效 console.warn('key值無(wú)效') return { message:`key值無(wú)效`, value:null } } } //刪除某一項(xiàng) public remove(key:Key) { localStorage.removeItem(key) } //清空所有值 public clear() { localStorage.clear() } }
rollup.js 簡(jiǎn)易打包暫時(shí)沒(méi)有寫的很復(fù)雜 所用到的依賴 rollup rollup-plugin-typescript2 typescript
import ts from 'rollup-plugin-typescript2' import path from 'path' export default { input:'./src/index.ts', output:{ file:path.resolve(__dirname,'./dist/index.js') }, plugins:[ ts() ] }
代碼測(cè)試
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script type="module"> import { Storage } from './dist/index.js' const sl = new Storage() //五秒后過(guò)期 sl.set('a', 123, new Date().getTime() + 5000) setInterval(() => { const a = sl.get('a') console.log(a) },500) </script> </body> </html>
測(cè)試五秒后過(guò)期增加計(jì)時(shí)器觀察值
過(guò)期之后 成功刪除 測(cè)試成功
以上就是本地存儲(chǔ)localStorage設(shè)置過(guò)期時(shí)間示例詳解的詳細(xì)內(nèi)容,更多關(guān)于本地存儲(chǔ)localStorage設(shè)置過(guò)期時(shí)間的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
微信小程序中使用Promise進(jìn)行異步流程處理的實(shí)例詳解
這篇文章主要介紹了微信小程序中使用Promise進(jìn)行異步流程處理的實(shí)例詳解的相關(guān)資料,這里詳細(xì)說(shuō)明該如何使用Promise 來(lái)進(jìn)行異步流程的處理,提供具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2017-08-08微信小程序 按鈕滑動(dòng)的實(shí)現(xiàn)方法
這篇文章主要介紹了微信小程序 按鈕滑動(dòng)的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09判斷Spartacus?SSR的Transfer?State是否正常工作技巧
這篇文章主要為大家介紹了判斷Spartacus?SSR的Transfer?State是否正常工作技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10微信小程序 常見(jiàn)問(wèn)題總結(jié)(4058,40013)及解決辦法
這篇文章主要介紹了微信小程序 常見(jiàn)問(wèn)題總結(jié)及解決辦法的相關(guān)資料,這里首先對(duì)微信小程序的結(jié)構(gòu)進(jìn)行了介紹,然后對(duì)常見(jiàn)問(wèn)題進(jìn)行說(shuō)明講解,需要的朋友可以參考下2017-01-01以JS開發(fā)為例詳解版本號(hào)的作用與價(jià)值
這篇文章主要為大家介紹了以JS開發(fā)為例詳解版本號(hào)的作用與價(jià)值詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09