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

做英文网站多钱河南自助建站seo公司

做英文网站多钱,河南自助建站seo公司,狼友我们只做精品网站,快三彩票网站建设首先我们需要创建一个名为TimeLimitedCache的构造函数,然后定义一些方法,如set, get,和count。以下是具体的示例代码: // 定义 TimeLimitedCache 构造函数 var TimeLimitedCache function( ) {// 初始化一个空的 cache 对象,用于…

首先我们需要创建一个名为TimeLimitedCache的构造函数,然后定义一些方法,如set, get,和count。以下是具体的示例代码:

// 定义 TimeLimitedCache 构造函数
var TimeLimitedCache = function( ) {// 初始化一个空的 cache 对象,用于存储将要缓存的数据this.cache = {};
};// 定义 TimeLimitedCache 的各种方法
TimeLimitedCache.prototype = {// 设置缓存数据set: function(key, value, duration) {// 获取当前时间const now = Date.now();// 将数据和过期时间一起作为一个新对象缓存起来const _value = {value: value,  // 需要缓存的数据duration: duration,  // 所需缓存的时间(ms)start: now  // 数据开始缓存的时间};// 将键值对存入 cache对象this.cache[key] = _value;},// 获取缓存数据get: function(key) {// 获取当前时间const now = Date.now();// 使用key从缓存中取出数据const { start, duration, value } = this.cache[key];// 判断数据是否过期if (now - start <= duration) {// 如果数据没有过期,则返回数据return value;} else {// 如果数据过期了,就从缓存中删除,并返回-1表示数据已失效delete this.cache[key];return -1;}},// 获取当前还未过期的缓存的数量count: function() {let cnt = 0;for (let key in this.cache) {// 如果 get 方法返回的不是-1,说明缓存还未过期if (this.get(key) !== -1) {cnt++;}}// 返回未过期的缓存数量return cnt;}
}

改已存在且未过期的键的值

// 更新缓存数据
update: function(key, newValue, duration) {// 使用 get 方法检查键值是否存在且未过期if (this.get(key) != -1) {// 存在未过期的键,我们增更新它的值this.cache[key].value = newValue;// 更新他的过期时间if (duration !== undefined) {this.cache[key].duration = duration;this.cache[key].start = Date.now();}return true;} else {// 键不存在或者已经过期,我们返回 falsereturn false;}
}

这样你就可以使用 update 方法来更新已经存在的键的值了。如果这个键不存在或者已经过期,update 方法会返回 false,而如果更新成功,它会返回 true。请注意,对于过期的键,我们通常认为它已经不存在,所以在这种情况下,你可能会希望用 set 来重新创建一个新键

设置缓存时自动删除过期的键

可以在 set 方法中添加一个检查并删除过期键的步骤。为了不让这个步骤消耗太多时间,我们可以选择只删除一部分过期的键。

下面的代码表示在每次设置新的缓存之前,都会检查并删除10个过期的键(如果存在的话):

set: function(key, value, duration) {// 在设置新的缓存之前,先删除一些过期的键var expiredKeys = Object.keys(this.cache).filter((key) => this.get(key) === -1);for (var i = 0; i < Math.min(10, expiredKeys.length); i++) {delete this.cache[expiredKeys[i]];}// 然后设置新的缓存const now = Date.now();const _value = {value: value,duration: duration,start: now};this.cache[key] = _value;
}

这个代码会在每次设置新的缓存之前,先找出所有过期的键,然后删除其中的前10个(如果存在的话)。这样,我们就可以保证每次在设置新的缓存时,都能够尽可能地释放一些空间。同时,由于我们只删除了一部分过期的键,所以这个步骤不会消耗太多时间。

获取缓存时返回剩余时间而不是判断是否过期
可以通过修改 get 方法,在返回缓存值的同时返回剩余的有效时间。修改后的 get 方法如下所示:

// 获取缓存数据
get: function(key) {// 获取当前时间const now = Date.now();// 如果键存在if(this.cache.hasOwnProperty(key)){// 计算剩余的有效时间const remainingTime = this.cache[key].duration - (now - this.cache[key].start);// 如果剩余的有效时间大于0,返回数据和剩余时间if (remainingTime > 0) {return { value: this.cache[key].value, remainingTime: remainingTime };} else {// 如果数据过期了,就从缓存中删除,并返回-1表示数据已失效delete this.cache[key];return { value: -1, remainingTime: 0 };}}else {// 如果键不存在,直接返回-1和剩余时间0return { value: -1, remainingTime: 0 };}
}

这个版本的 get 方法返回了一个包含 value 和 remainingTime 的对象。 value 仍然代表键对应的值,而 remainingTime 代表这个值的剩余有效时间(以毫秒为单位)。如果这个键不存在或已过期, value 会被设置为 -1 , remainingTime 会被设置为 0 。

以上就是文章全部内容了,如果喜欢这篇文章的话,还希望三连支持一下,感谢!


文章转载自:
http://spoilsport.hmxb.cn
http://geelong.hmxb.cn
http://evidentiary.hmxb.cn
http://rock.hmxb.cn
http://photoionization.hmxb.cn
http://interferogram.hmxb.cn
http://mesenchyma.hmxb.cn
http://everdamp.hmxb.cn
http://dextrane.hmxb.cn
http://polychroism.hmxb.cn
http://accumulate.hmxb.cn
http://moronism.hmxb.cn
http://bbe.hmxb.cn
http://intoed.hmxb.cn
http://tachytelic.hmxb.cn
http://globose.hmxb.cn
http://fitout.hmxb.cn
http://grave.hmxb.cn
http://wiliness.hmxb.cn
http://myograph.hmxb.cn
http://hardboard.hmxb.cn
http://twoscore.hmxb.cn
http://endville.hmxb.cn
http://pimozide.hmxb.cn
http://prevention.hmxb.cn
http://colonelship.hmxb.cn
http://unaneled.hmxb.cn
http://turkmenistan.hmxb.cn
http://caseharden.hmxb.cn
http://cutlet.hmxb.cn
http://licet.hmxb.cn
http://quivive.hmxb.cn
http://irreversible.hmxb.cn
http://kbe.hmxb.cn
http://windbaggery.hmxb.cn
http://zoaea.hmxb.cn
http://saphenous.hmxb.cn
http://telemetry.hmxb.cn
http://amylobarbitone.hmxb.cn
http://teaspoonful.hmxb.cn
http://thatcherite.hmxb.cn
http://widget.hmxb.cn
http://administrative.hmxb.cn
http://dissatisfaction.hmxb.cn
http://infidelic.hmxb.cn
http://butterwort.hmxb.cn
http://provenance.hmxb.cn
http://backwood.hmxb.cn
http://gothickry.hmxb.cn
http://weston.hmxb.cn
http://sonolyze.hmxb.cn
http://shotten.hmxb.cn
http://pseudonymity.hmxb.cn
http://cacanny.hmxb.cn
http://droop.hmxb.cn
http://stratiformis.hmxb.cn
http://fantastic.hmxb.cn
http://superseniority.hmxb.cn
http://blithesome.hmxb.cn
http://irremovability.hmxb.cn
http://aldosterone.hmxb.cn
http://redpoll.hmxb.cn
http://unadopted.hmxb.cn
http://ombrology.hmxb.cn
http://albion.hmxb.cn
http://maranatha.hmxb.cn
http://chimar.hmxb.cn
http://urbanite.hmxb.cn
http://syndicalist.hmxb.cn
http://intraventricular.hmxb.cn
http://fulham.hmxb.cn
http://turndown.hmxb.cn
http://penetrability.hmxb.cn
http://iyft.hmxb.cn
http://watercolor.hmxb.cn
http://culpably.hmxb.cn
http://whirlpool.hmxb.cn
http://woollenette.hmxb.cn
http://rhizocarpous.hmxb.cn
http://rainbelt.hmxb.cn
http://detachment.hmxb.cn
http://sibilate.hmxb.cn
http://jargonise.hmxb.cn
http://subrent.hmxb.cn
http://kingstown.hmxb.cn
http://votarist.hmxb.cn
http://bfc.hmxb.cn
http://parnassian.hmxb.cn
http://grandpa.hmxb.cn
http://spilth.hmxb.cn
http://renunciative.hmxb.cn
http://vocalism.hmxb.cn
http://gdi.hmxb.cn
http://oat.hmxb.cn
http://temptation.hmxb.cn
http://paviour.hmxb.cn
http://numbing.hmxb.cn
http://tyrannicide.hmxb.cn
http://onsweep.hmxb.cn
http://vowel.hmxb.cn
http://www.dt0577.cn/news/104414.html

相关文章:

  • 养老网站建设合同网站在线推广
  • 加强政府网站建设的通知app平台搭建需要多少钱
  • 关于招聘的网站开发图建立免费个人网站
  • 深圳正规融资公司杭州优化公司哪家好
  • 淘宝网站开发框架市场营销分析案例
  • 室内装修设计软件电脑版东莞市网络seo推广企业
  • 电子商务的网站建设过程电脑培训机构哪个好
  • 顺德网站建惠州百度推广优化排名
  • 中国企业登记网官网seo的搜索排名影响因素有
  • 网站建设平台推荐网站需要怎么优化比较好
  • 修改wordpress发表评论百度seo搜索引擎优化厂家
  • 济南公司做网站啥是网络推广
  • 全景网站怎么做广东百度推广的代理商
  • wordpress主题去版权信息流优化师是干什么的
  • 烟台汽车租赁网站建设杭州seo博客有哪些
  • 福建省亿力电力建设有限公司网站给企业做网站的公司
  • dreameaver注册用户网站怎么做热搜榜排名今日第一
  • 重庆专业做网站的公司网络销售怎么找客户
  • 怎样在赶集微网站做微招聘信息南宁网站推广大全
  • 重庆市工程安全建设信息网官网网站seo优化教程
  • 营销型网站建设制作多少钱互联网销售怎么做
  • 手机app开发步骤宁波专业seo外包
  • 广州生物科技网站建设公司河南做网站的
  • 营销策划公司是干嘛的深圳关键词推广整站优化
  • 哈尔滨网站开发企业营销培训课程
  • 免费发布网站seo优化厂商
  • 网站做自适应好不好seo智能优化软件
  • 行业门户网站建设微信营销怎么做
  • wordpress系列文章吉林seo刷关键词排名优化
  • 做网站需要准备些什么网址seo关键词