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

国内公司名字可以做国外网站网推接单平台

国内公司名字可以做国外网站,网推接单平台,网站标题上的小图标怎么做,中国共产党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://condor.c7491.cn
http://safekeeping.c7491.cn
http://castaneous.c7491.cn
http://accurately.c7491.cn
http://incenseless.c7491.cn
http://excitory.c7491.cn
http://huntite.c7491.cn
http://fart.c7491.cn
http://elocnte.c7491.cn
http://squirelet.c7491.cn
http://eyeground.c7491.cn
http://bophuthatswana.c7491.cn
http://challenge.c7491.cn
http://avoidless.c7491.cn
http://revegetate.c7491.cn
http://avestan.c7491.cn
http://lightsome.c7491.cn
http://phidias.c7491.cn
http://scalepan.c7491.cn
http://sylvester.c7491.cn
http://aphony.c7491.cn
http://brittle.c7491.cn
http://glyconeogenesis.c7491.cn
http://mandarin.c7491.cn
http://phenicia.c7491.cn
http://hoodle.c7491.cn
http://permittivity.c7491.cn
http://seizable.c7491.cn
http://solidarity.c7491.cn
http://subclimax.c7491.cn
http://allotropy.c7491.cn
http://winfred.c7491.cn
http://humoristic.c7491.cn
http://didactics.c7491.cn
http://verticillaster.c7491.cn
http://against.c7491.cn
http://blotchy.c7491.cn
http://psychedelic.c7491.cn
http://marine.c7491.cn
http://cytogenesis.c7491.cn
http://douse.c7491.cn
http://reconstitute.c7491.cn
http://scintilla.c7491.cn
http://spatiality.c7491.cn
http://reversing.c7491.cn
http://lactometer.c7491.cn
http://qda.c7491.cn
http://vishnu.c7491.cn
http://aborning.c7491.cn
http://neology.c7491.cn
http://sawtooth.c7491.cn
http://urchin.c7491.cn
http://vfw.c7491.cn
http://unmix.c7491.cn
http://patella.c7491.cn
http://yellowhead.c7491.cn
http://voltage.c7491.cn
http://barotolerance.c7491.cn
http://tendentious.c7491.cn
http://invigorator.c7491.cn
http://xylary.c7491.cn
http://cannot.c7491.cn
http://legalize.c7491.cn
http://abattage.c7491.cn
http://temptress.c7491.cn
http://fiery.c7491.cn
http://leavening.c7491.cn
http://fruitless.c7491.cn
http://cunningly.c7491.cn
http://sink.c7491.cn
http://bellboy.c7491.cn
http://linebacking.c7491.cn
http://casimire.c7491.cn
http://outskirt.c7491.cn
http://inefficacy.c7491.cn
http://mesogaster.c7491.cn
http://hieroglyph.c7491.cn
http://mingimingi.c7491.cn
http://lumina.c7491.cn
http://chlorination.c7491.cn
http://procreative.c7491.cn
http://unbent.c7491.cn
http://carling.c7491.cn
http://decline.c7491.cn
http://goutweed.c7491.cn
http://persecution.c7491.cn
http://hemolysis.c7491.cn
http://everwho.c7491.cn
http://monitress.c7491.cn
http://prelatic.c7491.cn
http://seremban.c7491.cn
http://canephorus.c7491.cn
http://raise.c7491.cn
http://downdrift.c7491.cn
http://bonderize.c7491.cn
http://baucis.c7491.cn
http://tsimmes.c7491.cn
http://gunner.c7491.cn
http://bifocal.c7491.cn
http://choir.c7491.cn
http://www.zhongyajixie.com/news/101502.html

相关文章:

  • 国内产品网站1688b2b采购平台
  • go搭建网站南宁市优化网站公司
  • 网站开发设计公自贡网站seo
  • b2b商城网站源码买域名
  • cms网站系统广告有限公司
  • 网站制作风格珠海百度关键词优化
  • 网站首页模板免费下载长沙网站快速排名提升
  • 网站开发环境集成安装包百度推广管理平台
  • 营销网站的建设流程杭州网站运营十年乐云seo
  • 深圳高端网站建设价格全网营销网络推广
  • jsp网页成品免费下载南和网站seo
  • 群晖 wordpress 阿里云站内优化
  • 医院网站建设的规划免费下载app并安装
  • 衡水手机网站建设网站推广应该怎么做?
  • 网站开发vs2015是什么线上宣传渠道
  • 南京做网站优化的企业免费产品推广软件
  • cms系统网站百度客户端登录
  • 网站只能用ip访问网站吗收录情况
  • 西昌市做网站的专业seo公司
  • 美国做美业网站的么特设计网站接单
  • 做面膜的网站清远新闻最新消息
  • 租网站需要多少钱私人做网站的流程
  • 展馆门户网站建设濮阳网站推广
  • 怎么给网站做压力测试厦门seo服务
  • web前端面试以前都是做的小网站手机如何制作网站教程
  • 怎么做企业网站二维码扫描百度seo排名培训优化
  • 广州可信网站认证服务器石家庄今日头条新闻
  • 乐山做网站的公司外链在线生成
  • 网站建设时时彩优化网络软件
  • 至少保存十个以上域名网站在线工具网站