mounted: functionmounted() { varthis$1 = this; // 遍历 cache,如果缓存的节点名称与传入的规则没有匹配上的话,就把这个节点从缓存中移除 this.$watch('include', function (val) { pruneCache(this$1, function (name) { return matches(val, name); }); }); this.$watch('exclude', function (val) { pruneCache(this$1, function (name) { return !matches(val, name); }); }); },
render: functionrender() { var slot = this.$slots.default; var vnode = getFirstComponentChild(slot); var componentOptions = vnode && vnode.componentOptions; if (componentOptions) { var name = getComponentName(componentOptions); var ref = this; var include = ref.include; var exclude = ref.exclude; if ( // not included (include && (!name || !matches(include, name))) || // excluded (exclude && name && matches(exclude, name)) ) { return vnode }
var ref$1 = this; // 当前组件实例 VueComponent var cache = ref$1.cache; var keys = ref$1.keys; var key = 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核心 // 如果命中缓存,则从缓存中获取 vnode 的组件实例,并且调整 key 的顺序放入 keys 数组的末尾 if (cache[key]) { vnode.componentInstance = cache[key].componentInstance; // make current key freshest remove(keys, key); keys.push(key); } else { // 如果没有命中缓存就把vnode放进缓存里 cache[key] = vnode; keys.push(key); // prune oldest entry // 如果配置了max,且有key有值大于max,则移除第一个缓存 if (this.max && keys.length > parseInt(this.max)) { pruneCacheEntry(cache, keys[0], keys, this._vnode); } } vnode.data.keepAlive = true; } return vnode || (slot && slot[0]) } };
get(key: number): number { if (this.cache.has(key)) { let val = this.cache.get(key) this.cache.delete(key) this.cache.set(key, val) return val } return-1 } put(key: number, value: number): void { if (this.capacity === 0) return // 存在, 移到最底下的位置,同时移除顶部的项 // 不存在,则需要看是否超出capacity,如果没有超出,则塞到最底下 如果超出,仍然需要移除顶部的项 if (this.cache.has(key)) { this.cache.delete(key) } else { if (this.capacity <= this.cache.size ) { let oldKey = this.cache.keys().next().value this.cache.delete(oldKey) } } this.cache.set(key, value) } }
/** * Your LRUCache object will be instantiated and called as such: * var obj = new LRUCache(capacity) * var param_1 = obj.get(key) * obj.put(key,value) */