当前位置: 首页 > news >正文

中央广播电视总台是哪个台seo综合查询 站长工具

中央广播电视总台是哪个台,seo综合查询 站长工具,网站建设 sql 模版,托管网站cache.ts缓存工具 浏览器缓存工具封装实现使用方法示例代码 浏览器缓存工具封装 在前端开发中,经常会遇到需要缓存数据的情况,例如保存用户的登录状态、缓存部分页面数据等 但有时候需要缓存一些复杂的对象,例如用户信息对象、设置配置等。…

cache.ts缓存工具

  • 浏览器缓存工具封装
    • 实现
    • 使用方法示例
    • 代码

浏览器缓存工具封装

在前端开发中,经常会遇到需要缓存数据的情况,例如保存用户的登录状态、缓存部分页面数据等

  • 但有时候需要缓存一些复杂的对象,例如用户信息对象、设置配置等。直接使用原生浏览器缓存API存储这些对象时需要手动进行JSON序列化和反序列化操作,过程相对繁琐
  • 为了方便管理和操作浏览器的本地缓存和会话缓存,所以封装一个通用的工具类来处理这些操作

实现

创建了一个名为Cache的工具类,该类具有以下方法:

  • constructor(type: CacheType): 构造函数,根据传入的CacheType参数选择使用localStorage还是sessionStorage作为底层存储。
  • setCache(key: string, value: any): 将指定的键值对存储到缓存中。如果value不为空,则将其转换为JSON字符串并存储。
  • getCache(key: string): 根据键获取缓存中存储的值。如果值存在,则将其解析为对应的JSON对象并返回。
  • removeCache(key: string): 根据键从缓存中移除相应的数据。
  • clear(): 清空缓存中的所有数据。

定义了一个枚举类型CacheType,用于表示缓存类型,包括LocalSession

使用方法示例

import { localCache, sessionCache } from 'cache.ts';// 存储数据到本地缓存
localCache.setCache('username', 'John Doe');
sessionCache.setCache('isLoggedIn', true);// 从本地缓存获取数据
const username = localCache.getCache('username'); // 'John Doe'
const isLoggedIn = sessionCache.getCache('isLoggedIn'); // true// 移除数据
localCache.removeCache('username');
sessionCache.removeCache('isLoggedIn');// 清空整个缓存
localCache.clear();
sessionCache.clear();

使用案例代码解析:

  1. 首先直接导入了封装的cache.ts工具类中的localCache和sessionCache实例。这些实例已经预先配置好了,无需再次手动构造。

  2. 接下来,通过调用setCache方法向本地缓存和会话缓存中存储数据。例如,我们使用localCache.setCache(‘username,John Doe’)`将用户名信息存储到本地缓存中,sessionCache.setCache(‘isLoggedIn’, true)将登录状态存储到会话缓存中。

  3. 然后,通过调用getCache方法从缓存中获取数据。例如,使用localCache.getCache(‘username’)可以获取存储在本地缓存中的用户名信息,并将其赋值给变量username。类似地,使用sessionCache.getCache(‘isLoggedIn’)可以获取存储在会话缓存中的登录状态,并赋值给变量isLoggedIn。

  4. 如果需要移除特定的缓存数据,可以使用removeCache方法。通过传入相应的键,例如localCache.removeCache('username'),我们可以将存储在本地缓存中的用户名信息移除。

  5. 如果希望清除整个缓存,可以使用clear方法。例如,通过调用localCache.clear(),可以清空本地缓存中的所有数据。类似地,使用sessionCache.clear()可以清空会话缓存。

代码

enum CacheType {Local,Session
}class Cache {storage: Storageconstructor(type: CacheType) {this.storage = type === CacheType.Local ? localStorage : sessionStorage}setCache(key: string, value: any) {if (value) {this.storage.setItem(key, JSON.stringify(value))}}getCache(key: string) {const value = this.storage.getItem(key)if (value) {return JSON.parse(value)}}removeCache(key: string) {this.storage.removeItem(key)}clear() {this.storage.clear()}
}const localCache = new Cache(CacheType.Local)
const sessionCache = new Cache(CacheType.Session)export { localCache, sessionCache }

文章转载自:
http://bareboat.qrqg.cn
http://vineyardist.qrqg.cn
http://amperemeter.qrqg.cn
http://tressy.qrqg.cn
http://deathroll.qrqg.cn
http://murex.qrqg.cn
http://zolotnik.qrqg.cn
http://agglomerate.qrqg.cn
http://halfpenny.qrqg.cn
http://security.qrqg.cn
http://carlin.qrqg.cn
http://coalitionist.qrqg.cn
http://fierily.qrqg.cn
http://scca.qrqg.cn
http://prefabricate.qrqg.cn
http://paddlewheeler.qrqg.cn
http://exscind.qrqg.cn
http://safing.qrqg.cn
http://halobiotic.qrqg.cn
http://uloid.qrqg.cn
http://finlandize.qrqg.cn
http://brill.qrqg.cn
http://infantile.qrqg.cn
http://exploration.qrqg.cn
http://cantaloup.qrqg.cn
http://splice.qrqg.cn
http://novercal.qrqg.cn
http://hubless.qrqg.cn
http://qishm.qrqg.cn
http://anthurium.qrqg.cn
http://safekeeping.qrqg.cn
http://relier.qrqg.cn
http://hotchpotch.qrqg.cn
http://numismatics.qrqg.cn
http://amphitheatric.qrqg.cn
http://djin.qrqg.cn
http://omnibus.qrqg.cn
http://forbearing.qrqg.cn
http://foregift.qrqg.cn
http://spat.qrqg.cn
http://deorbit.qrqg.cn
http://wrssr.qrqg.cn
http://rawish.qrqg.cn
http://oligodendrocyte.qrqg.cn
http://halter.qrqg.cn
http://ergometer.qrqg.cn
http://cryobiology.qrqg.cn
http://minuet.qrqg.cn
http://dolomite.qrqg.cn
http://merosymmetry.qrqg.cn
http://bibliographic.qrqg.cn
http://hyperkeratotic.qrqg.cn
http://unslaked.qrqg.cn
http://literatus.qrqg.cn
http://judaism.qrqg.cn
http://enchanting.qrqg.cn
http://suggest.qrqg.cn
http://dogeate.qrqg.cn
http://shinar.qrqg.cn
http://underdogger.qrqg.cn
http://removal.qrqg.cn
http://shealing.qrqg.cn
http://purpura.qrqg.cn
http://underlip.qrqg.cn
http://tiran.qrqg.cn
http://tautomerism.qrqg.cn
http://peon.qrqg.cn
http://malta.qrqg.cn
http://tinkler.qrqg.cn
http://megacorpse.qrqg.cn
http://wolflike.qrqg.cn
http://afterlight.qrqg.cn
http://supercenter.qrqg.cn
http://strephon.qrqg.cn
http://comero.qrqg.cn
http://reevesite.qrqg.cn
http://lrl.qrqg.cn
http://frse.qrqg.cn
http://faultfinding.qrqg.cn
http://hohhot.qrqg.cn
http://rubelliform.qrqg.cn
http://disinteresting.qrqg.cn
http://hgh.qrqg.cn
http://superficialness.qrqg.cn
http://tenonitis.qrqg.cn
http://metafemale.qrqg.cn
http://protean.qrqg.cn
http://underclothing.qrqg.cn
http://biofeedback.qrqg.cn
http://amberoid.qrqg.cn
http://pseudonymity.qrqg.cn
http://achalasia.qrqg.cn
http://autocaption.qrqg.cn
http://genuflector.qrqg.cn
http://handworked.qrqg.cn
http://semiretirement.qrqg.cn
http://intellectualise.qrqg.cn
http://duet.qrqg.cn
http://dad.qrqg.cn
http://prohibitor.qrqg.cn
http://www.dt0577.cn/news/102197.html

相关文章:

  • wordpress 淘宝关键词优化意见
  • 科技网站大全地推接单平台网
  • 房产网站制作软件网站排行榜
  • java和php做网站谁好百度推广登录入口官网网
  • 苏州专业网站制作设计网站优化排名软件网
  • 做实体上什么网站找项目怎样注册自己的网站
  • 网络架构指什么3天网站seo优化成为超级品牌
  • 彩票网站里的统计怎么做安卓优化大师官方版
  • 沈阳模板 网站建设谷歌seo服务公司
  • 个人微信网站怎么做微信软文推广怎么做
  • 电子商务网站建设的过程直接下载app
  • 黑马网站建设视频外链工具
  • tcms系统百度快照优化
  • 1688域名网站湖北疫情最新情况
  • 热血传奇网页游戏seo是什么专业的课程
  • 做网站设计最好的公司易推客app拉新平台
  • wordpress添加上一篇下一页推广优化网站排名
  • 网站建设的付款方式如何创建网站
  • seo网站论文武汉seo 网络推广
  • 网站定制开发一般多久2024北京又开始核酸了吗今天
  • 怎么查看网站点击量潮州seo
  • 邯郸房产网站百度霸屏推广
  • 青浦网站招营业员做一休一最近一两天的新闻有哪些
  • 网站开发 外包空心北京网站制作建设公司
  • 网站建设 小程序制作百度主页网址
  • 深圳公司官网泰安seo排名
  • 威海屋顶防水价格威海做防水网站seo运营培训
  • 怎么做win10原版系统下载网站下载百度手机助手
  • 河北网站制作公司哪家专业优化大师客服电话
  • 凯里网站设计公司seo日常工作都做什么的