国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

LRU算法在Vue內置組件keep-alive中的使用

瀏覽:5日期:2022-09-29 09:48:36
目錄vue的keep-alive內置組件的使用也是使用了改算法,源碼如下:實現一個自己的LRU算法另一種vue的keep-alive內置組件的使用也是使用了改算法,源碼如下:

export default { name: 'keep-alive', // 抽象組件屬性 ,它在組件實例建立父子關系的時候會被忽略,發生在 initLifecycle 的過程中 abstract: true, props: { // 被緩存組件 include: patternTypes, // 不被緩存組件 exclude: patternTypes, // 指定緩存大小 max: [String, Number] }, created() { // 初始化用于存儲緩存的 cache 對象 this.cache = Object.create(null); // 初始化用于存儲VNode key值的 keys 數組 this.keys = []; }, destroyed() { for (const key in this.cache) { // 刪除所有緩存 pruneCacheEntry(this.cache, key, this.keys); } }, mounted() { // 監聽緩存(include)/不緩存(exclude)組件的變化 // 在變化時,重新調整 cache // pruneCache:遍歷 cache,如果緩存的節點名稱與傳入的規則沒有匹配上的話,就把這個節點從緩存中移除 this.$watch('include', val => { pruneCache(this, name => matches(val, name)); }); this.$watch('exclude', val => { pruneCache(this, name => !matches(val, name)); }); }, render() { // 獲取第一個子元素的 vnode const slot = this.$slots.default; const vnode: VNode = getFirstComponentChild(slot); const componentOptions: ?VNodeComponentOptions = vnode && vnode.componentOptions; if (componentOptions) { // name 不在 inlcude 中或者在 exlude 中則直接返回 vnode,否則繼續進行下一步 // check pattern const name: ?string = getComponentName(componentOptions); const { include, exclude } = this; if (// not included(include && (!name || !matches(include, name))) ||// excluded(exclude && name && matches(exclude, name)) ) {return vnode; } const { cache, keys } = this; // 獲取鍵,優先獲取組件的 name 字段,否則是組件的 tag const key: ?string =vnode.key == null ? // same constructor may get registered as different local components // so cid alone is not enough (#3269) componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '') : vnode.key; // -------------------------------------------------- // 下面就是 LRU 算法了, // 如果在緩存里有則調整, // 沒有則放入(長度超過 max,則淘汰最近沒有訪問的) // -------------------------------------------------- // 如果命中緩存,則從緩存中獲取 vnode 的組件實例,并且調整 key 的順序放入 keys 數組的末尾 if (cache[key]) {vnode.componentInstance = cache[key].componentInstance;// make current key freshestremove(keys, key);keys.push(key); } // 如果沒有命中緩存,就把 vnode 放進緩存 else {cache[key] = vnode;keys.push(key);// prune oldest entry// 如果配置了 max 并且緩存的長度超過了 this.max,還要從緩存中刪除第一個if (this.max && keys.length > parseInt(this.max)) { pruneCacheEntry(cache, keys[0], keys, this._vnode);} } // keepAlive標記位 vnode.data.keepAlive = true; } return vnode || (slot && slot[0]); }};// 移除 key 緩存function pruneCacheEntry ( cache: VNodeCache, key: string, keys: Array<string>, current?: VNode) { const cached = cache[key] if (cached && (!current || cached.tag !== current.tag)) { cached.componentInstance.$destroy() } cache[key] = null remove(keys, key)}// remove 方法(shared/util.js)/** * Remove an item from an array. */export function remove (arr: Array<any>, item: any): Array<any> | void { if (arr.length) { const index = arr.indexOf(item) if (index > -1) { return arr.splice(index, 1) } }}實現一個自己的LRU算法

lru算法 的核心api(put get)和一個size最大容器值,本質是類似隊列 put實現思路 1 是否存在,存在就先刪除,再添加到隊頭 2 不存在,容量是否滿了,刪除最后一個隊尾,再添加隊頭 get實現思路: 1.有就返回,同時插入隊頭 2.沒有返回-1 時間復雜度O(1)

class LRU { constructor(size) { this.cache = new Map() this.size = size } put (key, val) { //存在 if (this.cache.has(key)) { //刪除 this.cache.delete(key) } else { //不存在,容量是否滿了 if (this.size === this.cache.size) {//刪除最后一個this.cache.delete(this.cache.keys().next().value) //拿到隊尾的元素 } } //插在隊頭 this.cache.set(key, val) } get (key) { let val = this.cache.get(key) if (!val) { return -1 } //訪問了就需要放在隊頭 this.put(key, val) return val }}另一種

//定義節點類class Node { constructor(pre, next, value, key){this.pre = pre;this.next = next;this.value = value;this.key = key; }}//定義雙向鏈表class DoubleList { constructor(head, tail){this.head = head;this.tail = tail; }}class LRUCache { //構造函數,傳入緩存容量 constructor(max){this.max = max;this.map = new Map();let node = new Node(null, null, null, null);this.doubleList = new DoubleList(node, node); }/** * 獲取緩存值 * 不存在返回-1,存在返回對應value值,并將此節點移到尾巴 * @param {*} key key值 */ get(key){let node = this.map.get(key)if(!node){ return -1;}else{ this.moveNode2Tail(key, node); return node.value;} } /** * 插入緩存 * 1.不存在對應key值,加到尾巴 * 2.存在對應key值,更新此key值對應value并提到尾巴 * 3.超出容量的話,去掉頭部數據 * @param {*} key key值 * @param {*} value value */ put(key, value) {let node = this.map.get(key);if(node){ if(!node.next){node.value = value;return; } node.pre.next = node.next; node.next.pre = node.pre;}let newNode = new Node(null, null, value, key);newNode.pre = this.doubleList.tail;this.doubleList.tail.next = newNode;this.doubleList.tail = newNode;this.map.set(key, newNode);if(this.map.size > this.max){ this.map.delete(this.doubleList.head.next.key); this.doubleList.head.next = this.doubleList.head.next.next; this.doubleList.head.next.pre = this.doubleList.head; } }//將節點移到尾巴 moveNode2Tail(key,node){ if(!node.next){ return;}//刪除節點 node.pre.next = node.next;node.next.pre = node.pre;this.map.delete(key)//新增尾巴節點let newNode = new Node(null, null, node.value, key);newNode.pre = this.doubleList.tail;this.doubleList.tail.next = newNode;this.doubleList.tail = newNode;this.map.set(key, newNode); }}

以上就是LRU算法在Vue內置組件keep-alive中的使用的詳細內容,更多關于Vue LRU算法的資料請關注好吧啦網其它相關文章!

標簽: Vue
相關文章:
主站蜘蛛池模板: 国产成人mv在线观看入口视频 | 一级a级国产不卡毛片 | 国产精品极品美女自在线看免费一区二区 | 国产夫妻视频 | 普通话对白国产情侣自啪 | 免费人成年短视频在线观看免费网站 | 女人被男人躁得好爽免费文 | 日本美女黄色一级片 | 日韩美女一级片 | 欧美成人高清手机在线视频 | a级毛片免费观看网站 | 韩国精品一区二区三区四区五区 | 国产精品国产三级在线高清观看 | 国产一区二区久久 | 亚洲视频日韩视频 | 久久影院视频 | 亚洲人成在线免费观看 | 99热久久精品免费精品 | 亚欧精品一区二区三区 | 孕妇孕妇aaaaa级毛片视频 | 国产丶欧美丶日韩丶不卡影视 | 日韩在线视频一区二区三区 | 久久精品国产99精品最新 | 欧美国产成人一区二区三区 | 99久久综合国产精品免费 | 日韩欧美综合在线二区三区 | 欧美日本道免费一区二区三区 | a级成人高清毛片 | 国产精品福利午夜h视频 | 在线日韩三级 | 日韩一区二区三区在线 | 国产三级久久 | 在线欧美一级毛片免费观看 | 不卡一区二区在线观看 | 国内成人精品视频 | 亚洲成a人片在线观看精品 亚洲成a人片在线观看中 | 99在线观看巨臀大臀视频 | 欧美日韩人成在线观看 | 日本三级在线观看中文字 | 日韩成人中文字幕 | 国产一区二区久久精品 |