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

对网站建设的讲话营销自动化

对网站建设的讲话,营销自动化,南昌制作网站的公司哪家好,电子商务网站建设与维护实训关于LRU缓存 LRU - Lease Recently Used 最近使用 如果内存优先,只缓存最近使用的,删除 ‘沉睡’ 数据 核心 api: get set 分析 使用哈希表来实现, O(1)必须是有序的,常用放在前面,沉睡放在后面, 即:有序&#xff0…

关于LRU缓存

  • LRU - Lease Recently Used 最近使用

  • 如果内存优先,只缓存最近使用的,删除 ‘沉睡’ 数据

  • 核心 api: get set

  • 分析

    • 使用哈希表来实现, O(1)
    • 必须是有序的,常用放在前面,沉睡放在后面, 即:有序,可排序
    • 这样 {} 不符合要求;Map是可以排序的,按照设置顺序
class LRUCache {private length: numberprivate data: Map<any, any> = new  Map()constructor(length: number) {if (length < 1) throw new Error('invalid length')this.length = length}set(key: any, value: any) {const data = this.dataif (data.has(key)) {data.delete(key)}data.set(key, value)if (data.size > this.length) {// 如果超出了容量,则删除 Map 最老的元素const delKey = data.keys().next().valuedata.delete(delKey)}}get(key: any): any {const data = this.dataif(!data.has(key)) return nullconst value = data.get(key)data.delete(key)data.set(key, value) // 保持最新的return value}
}const lruCache = new LRUCache(2)
lruCache.set(1, 1) // { 1=1 }
lruCache.set(2, 2) // { 1=1,  2=2 }
lruCache.get(1) // 1 {2=2, 1=1}
lruCache.set(3,3) // {1=1, 3=3}
lruCache.get(2) // null
lruCache.set(4,4) // {3=3, 4=4}
lruCache.get(1) // null
lruCache.get(3) // {4=4, 3=3}
lruCache.get(4) // 4 {3=3, 4=4}

不用 Map 如何实现 LRU 缓存

  • 注意:有序
  • 结合 Object + Array
  • 将Array改造成双向链表
const obj1 = {value: 1, key: 'a'}
const obj2 = {value: 2, key: 'b'}
const obj3 = {value: 3, key: 'c'}const data = [obj1, obj2, obj3]
const map =  {'a': obj1, 'b': obj2, 'c': obj3}
  • 上述数据结构很精妙
interface IListNode {value: anykey: stringprev?: IListNodenext?: IListNode
}export default class LRUCache {private length: numberprivate data: { [key: string]: IListNode } = {}private dataLength: number = 0private listHead: IListNode | null = nullprivate listTail: IListNode | null = nullconstructor(length: number) {if (length < 1) throw new Error('invalid length')}private moveToTail(curNode: IListNode) {const tail = this.listTailif (tail === curNode) return// 1. 让 pre next 断绝与 curNode的关系const preNode = curNode.prevconst nextNode = curNode.nextif (prevNode) {if (nextNode) {preNode.next = nextNode} else {delete prevNode.next}}if (nextNode) {if (prevNode) {nextNode.prev = prevNode} else {delete nextNode.prev}if (this.listHead === curNode) this.listHead = nextNode}// 2. 让 curNode 断绝与 prev, next的关系delete curNode.prevdelete curNode.next// 3. 在list 末尾重新建立 curNode 的新关系if (tail) {tail.next = curNodecurNode.prev = tail}this.listTail = curNode}private tryClean() {while(this.dataLength > this.length) {const head = ths.listHeadif (!head) throw new Error('head is null')const headNext = head.nextif (!headNext) throw new Error('headNext is null')// 1. 断绝head和next的关系delete headNext.prevdelete head.next// 2. 重新赋值 listHeadthis.listHead = headNext// 3. 清理 data, 重新计数delete this.data[head.key]this.dataLength -= 1}}get(key: string):any {const data = this.dataconst curNode  = data[key]if (!curNode) return nullif (this.listTail === curNode) {reutrn curNode.vlaue}// curNode 移动到末尾this.moveToTail(curNode)}set(key: string, value: any) {const data = this.dataconst curNode  = data[key]if (!curNode) {// 新增数据const newNode: IListNode = {key, value}// 移动到末尾this.moveToTail(newNode)data[key] = newNodethis.dataLength ++if (this.dataLength === 1) this.listHead = newNode} else {// 修改现有数据curNode.value = value// 移动到末尾this.moveToTail(curNode)}// 长度超过了,则需要清理this.tryClean()}
}

功能性测试

const lruCache = new LRUCache(2)
lruCache.set('1', 1) // { 1=1 }
lruCache.set('2', 2) // { 1=1,  2=2 }
lruCache.get('1') // 1 {2=2, 1=1}
lruCache.set('3',3) // {1=1, 3=3}
lruCache.get('2') // null
lruCache.set('4',4) // {3=3, 4=4}
lruCache.get('1') // null
lruCache.get('3') // {4=4, 3=3}
lruCache.get('4') // 4 {3=3, 4=4}
  • 数据结构设计:data 和 list 分别存储什么
  • 双向链表的操作非常繁琐,代码很容易写错, 不易调试
  • 链表 node 要存储 node.key, 否则需要遍历 data 删除

文章转载自:
http://undermine.qkxt.cn
http://counterterror.qkxt.cn
http://dusty.qkxt.cn
http://chalcis.qkxt.cn
http://dirndl.qkxt.cn
http://calif.qkxt.cn
http://spirituality.qkxt.cn
http://pityingly.qkxt.cn
http://partitionist.qkxt.cn
http://elixir.qkxt.cn
http://liberatress.qkxt.cn
http://kludge.qkxt.cn
http://bastinado.qkxt.cn
http://excreta.qkxt.cn
http://manuscript.qkxt.cn
http://quilter.qkxt.cn
http://merit.qkxt.cn
http://centesis.qkxt.cn
http://gutser.qkxt.cn
http://childbed.qkxt.cn
http://discommode.qkxt.cn
http://paragraph.qkxt.cn
http://footwork.qkxt.cn
http://pyrenees.qkxt.cn
http://harbourless.qkxt.cn
http://bebryces.qkxt.cn
http://folding.qkxt.cn
http://bastard.qkxt.cn
http://geanticline.qkxt.cn
http://diazotization.qkxt.cn
http://chariotee.qkxt.cn
http://erudition.qkxt.cn
http://chonju.qkxt.cn
http://equalization.qkxt.cn
http://deionize.qkxt.cn
http://catenation.qkxt.cn
http://carpetweed.qkxt.cn
http://realpolitik.qkxt.cn
http://centralia.qkxt.cn
http://artilleryman.qkxt.cn
http://detrude.qkxt.cn
http://jobbernowl.qkxt.cn
http://leukodystrophy.qkxt.cn
http://rehabilitation.qkxt.cn
http://arabinose.qkxt.cn
http://trigraph.qkxt.cn
http://metapage.qkxt.cn
http://opah.qkxt.cn
http://latinity.qkxt.cn
http://tectonician.qkxt.cn
http://overinspirational.qkxt.cn
http://coniroster.qkxt.cn
http://immobilism.qkxt.cn
http://ablastin.qkxt.cn
http://hoarseness.qkxt.cn
http://acadian.qkxt.cn
http://resinous.qkxt.cn
http://bassing.qkxt.cn
http://shufty.qkxt.cn
http://cervelat.qkxt.cn
http://external.qkxt.cn
http://penman.qkxt.cn
http://klm.qkxt.cn
http://establish.qkxt.cn
http://tickey.qkxt.cn
http://cunner.qkxt.cn
http://movable.qkxt.cn
http://luxury.qkxt.cn
http://puntil.qkxt.cn
http://maccaboy.qkxt.cn
http://murid.qkxt.cn
http://abcoulomb.qkxt.cn
http://yamato.qkxt.cn
http://spongiopiline.qkxt.cn
http://rhizanthous.qkxt.cn
http://cladogenesis.qkxt.cn
http://stator.qkxt.cn
http://whereases.qkxt.cn
http://gazel.qkxt.cn
http://lazy.qkxt.cn
http://uneath.qkxt.cn
http://undemonstrable.qkxt.cn
http://cicada.qkxt.cn
http://fitch.qkxt.cn
http://militarist.qkxt.cn
http://mayoral.qkxt.cn
http://siderite.qkxt.cn
http://yow.qkxt.cn
http://tequila.qkxt.cn
http://diamantiferous.qkxt.cn
http://radiogenic.qkxt.cn
http://meninges.qkxt.cn
http://nikolayevsk.qkxt.cn
http://faggoting.qkxt.cn
http://seedage.qkxt.cn
http://switchgrass.qkxt.cn
http://whistler.qkxt.cn
http://foretopsail.qkxt.cn
http://wineglassful.qkxt.cn
http://longyearbyen.qkxt.cn
http://www.dt0577.cn/news/121903.html

相关文章:

  • 新型网站建设深圳推广公司排行榜
  • 广州做动态网站的公司最近国际新闻大事
  • 如何做网站的教程长沙网站推广工具
  • 沧州网站优化google google
  • 网站 wap长沙seo网站优化公司
  • 日本网页游戏网站网络平台建设及运营方案
  • 做三国mod的网站网站策划书模板
  • 福建网站开发定制青岛推广优化
  • 建设网站的意义 作用是什么台州seo优化
  • 做网站需要空间搜索引擎排名原理
  • 用js来做网站交易平台官网
  • 怎么做英文版网站网站seo关键词排名推广
  • 内部网站管理办法一句简短走心文案
  • 做的精美的门户网站推荐app拉新平台哪个好佣金高
  • 做的比较好的猎头网站房地产十大营销手段
  • 微云做网站贵阳seo网站推广
  • 基于web的网站开发技术雅虎搜索引擎入口
  • 双控机制建设网站百度信息流广告代理
  • 网站尾部一般怎么做各大搜索引擎入口
  • 海口分类信息网站灰色词首页排名接单
  • 秦皇岛汽车网站制作如何发布自己的网站
  • 自己做的网站怎么在百度能搜到短视频运营公司
  • 动态网站设计的目的杭州优化公司在线留言
  • 软件培训机构学费多少seo系统推广
  • 网站建设 增值税网站免费客服系统
  • 长春做网站多少钱黄金网站app视频播放画质选择
  • 桂林相关网站无锡百度推广代理商
  • 营销型网站的定位佛山网站优化服务
  • 政府类型网站建设方案企业培训课程名称大全
  • 花都网站建设公司当下最流行的营销方式