LRU原理以及js实现

2020-06-09 08:50:28

参考地址 LRU缓存的js实现

LRU缓存原理:

LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”。

实现

最常见的实现是使用一个链表保存缓存数据,详细算法实现如下:
这里写图片描述

  1. 新数据插入到链表头部;

  2. 每当缓存命中(即缓存数据被访问),则将数据移到链表头部;

  3. 当链表满的时候,将链表尾部的数据丢弃。

代码

function Node(value, next) {  
    this.value = value;  
    this.next = next;  
}  

function LRUCache(initialCapacity, initialValue) {  
    this.size = 0;  
    this.head = null;  
    this.tail = null;  
    this.capacity = initialCapacity;  

    if (initialValue) {  
        var key = Object.keys(initialValue)[0];        //Object.keys()方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致 (两者的主要区别是 一个 for-in 循环还会枚举其原型链上的属性)。  
        var value = initialValue[key];  
        this.cache(key, value);  
    }  
}  

//是否为空  LRUCache.prototype.isEmpty = function() {  
    return this.head == null && this.tail == null;  
};  

//在头部做添加  LRUCache.prototype.addFirst = function(obj) {  
    var newNode = new Node(obj, null);  
    if (this.isEmpty()) {  
        this.head = this.tail = newNode;  
    } else {  
        this.head.next = newNode;  
        this.head = newNode;        //链表是反着的,head是最后一个节点  
    }  
    this.size++;  
    newNode = null;  
};  

//在尾部做删除  LRUCache.prototype.removeLast = function() {  
    if (!this.isEmpty()) {  
        var key = Object.keys(this.tail.value)[0];  
        delete this[key];  
        this.tail = this.tail.next;  
        if (this.tail == null) {  
            this.head = null;  
        }  
        this.size--;  
    }  
};  

//将节点向头部移动  LRUCache.prototype.moveForward = function(key) {  
    var node = this.search(key);  
    if (node && node.next) {  
        var next = node.next;  
        var temp = node.value;  
        node.value = next.value;  
        next.value = temp;        //位置交换了一下  
    }  
};  

//缓存元素  LRUCache.prototype.cache = function(key, value) {  
    //先查找是否已存在  
    var result = this.search(key);  
    //已存在采取覆盖value策略  
    if (result) {  
        result.value[key] = value;  
        this[key] = value;  
    } else {  
        var obj = {};  
        obj[key] = value;  
        this.addFirst(obj);  
        var that = this;  
        Object.defineProperty(this, key, {  
            set : function(x) {  
                that.moveForward(key);  
                value = x;  
            },  
            get : function() {  
                that.moveForward(key);  
                return value;  
            },  
            configurable : true,  
            enumerable : true  
        });  
        //超出容量的话,将最少使用的删除  
        if (this.size > this.capacity) {  
            this.removeLast();  
        }  
    }  
    //可以链式调用  
    return this;  
};  

//删除元素  LRUCache.prototype.del = function(key) {  
    if (this.search(key)) {  
        var previous = null;  
        var cur = this.tail;  
        for (; cur; cur = cur.next) {  
            if (cur.value.hasOwnProperty(key)) {  
                break;  
            }  
            previous = cur;  
        }  
        if (previous) {  
            if (previous.next) {            //待定,感觉有点问题,删除的是目标值的后一个节点  
                previous.next = previous.next.next;  
                this.size--;  
            }  
        } else {  
            this.tail = this.tail.next;  
            this.size--;  
        }  
    }  
    if (this.hasOwnProperty(key)) {  
        return (delete this[key]);  
    } else {  
        return (delete LRUCache.prototype[key]);  
    }  
};  

//查找元素  LRUCache.prototype.search = function(key) {  
    for (var e = this.tail; e; e = e.next) {  
        if (key in e.value) {  
            return e;  
        }  
    }  
    return null;  
};  123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135

这里写图片描述

参考:
http://flychao88.iteye.com/blog/1977653
https://blog.csdn.net/esir82/article/details/72674274#commentBox


  • 2020-03-14 23:15:25

    icomoon使用详细介绍

    此篇博文讲述如何利用icomoon导入图标,从而把自己想要的都通过icomoon方式进行,大家都知道,网站以及移动端,用图标还是尽量选择这种。因为直接用image有些图标会失真,从而也是前端开发之中,需求去掌握的一项,很简单的就几个步骤。

  • 2020-03-14 23:39:59

    vuetify和@nuxt/vuetify icon 之我见

    vuetify中v-icon,貌似默认支持 Material Design Icons, Material Icons, Font Awesome 4 and Font Awesome 5, 我自己单独引入了vuetify 用哪一个图标都没有问题。但是用了@nuxt/vuetify只能用mdi-home这样的。不知道因为啥。肯定是封装后,封装成一个了。 但是我修改vuetify的设置,哪一个图标也都能用。哎,不过多研究了。

  • 2020-03-16 15:57:53

    nuxtjs中单独引入Message组件的问题

    // 引入elementUIimport { Message } from 'element-ui';//由于Message组件并没有install 方法供Vue来操作的,是直接返回的,因此按照官方文档单独引入的方法是//会报错的,需要给 Message 添加 install 方法Message.install = function (Vue, options) {Vue.prototype.$message = Message}Vue.use(Message )//消息提示

  • 2020-03-16 16:03:20

    css的var()函数

     随着sass,less预编译的流行,css也随即推出了变量定义var函数。var()函数,就如同sass和less等预编译软件一样,可以定义变量并且进行对应的使用。

  • 2020-03-16 16:52:05

    对icomoon的误解,以及最快速的使用

    此时需要注意顶部第一个选项,Quick Usage,一定要打开,Enable Quick Usage,谁让咱英语不好呢,这个时候会出现一个css连接,直接引用就好了,就可以随意使用图标了,引入这一个css就能实现我们的功能,省区引入太多文件的烦恼,你可以在浏览器打开这个css,可以看到里面把我们所用的文件整成base64了。所以挺好用的。

  • 2020-03-17 09:47:05

    video标签视频不自动播放的问题

    添加 muted 属性,就可以通过地址栏进入网页的时候自动播放了,手机端还是有的有限制的,比如iphone浏览器,就不行,苹果手机为了保护用户的流量和用户的意愿,是禁止自动播放的,必须有手动触发。

  • 2020-03-17 14:21:31

    nuxt+pm2 自动化部署及打包后文件自动上传阿里云 oss(精华)

    部署nuxtjs,这一篇文章就够了,pm2 代码自动发布依赖于 git 工具,先将 ssh 密钥配置再你的代码仓库(github 或者 gitLab),具体操作自行 google 或者点击github 配置 ssh。 使用 ssh 密钥链接服务器 s $ ssh-copy-id root@1.2.3.4 # 把本机的 SSH 秘钥添加至服务器,配置成功后,以后就不需要再执行这条 SSH 命令了