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-12-07 15:19:00

    响应式邮件的编写插件介绍mjml

    以前做项目碰到发邮件的需求,邮件模板的编辑就是一件头疼的事。因为虽说邮件是支持 HTML 的,但是确是 HTML 子集程度的支持,所以存在必须通过 <table> 排版的恶心之处,还有很多兼容性的坑。本质上是各家邮件商的标准有差异吧。

  • 2020-12-07 16:14:22

    nodejs队列实现amqplib,rabbitmq

    其中StartConsumer 会在项目启动时启动,在整个生命周期中一直保持监听状态,在程序结束时mq的链接关闭。需要注意的是 noAck 这个参数,当为false是表示消息出队后不会自动删除,如果设置成true,则无论消息处理成功与否此消息会被删除。注意到在消息不成功是,调用了ch.nack(msg)),此方法是将消息重新入队。

  • 2020-12-07 16:15:46

    RabbitMQ详解

    当前市面上mq的产品很多,比如RabbitMQ、Kafka、ActiveMQ、ZeroMQ和阿里巴巴捐献给Apache的RocketMQ。甚至连redis这种NoSQL都支持MQ的功能。 ActiveMQ ActiveMQ是apache出品,最流行的,能力强劲的开源消息总线,并且它一个完全支持JMS规范的消息中间件。其丰富的API、多种集群构建模式使得它成为业界老牌消息中间件,在中小型企业中应用广泛。

  • 2020-12-07 16:17:53

    nodejs用redis实现队列操作

    其实nodejs实现队列的方式又很多中,也有很多开源的插件和队列数据库可以使用,但是呢,如果我们一个简单的项目,完全可以使用redis来实现队列, 这样再不增加技术难度的同事,我们也就可以完美的实现一个队列

  • 2020-12-07 22:02:44

    intellij idea远程开发的几个想法

    我之前是用idea上面自带的stfp来做的本地开发同步到linux服务器编译,但是我发现这个如果多个客户端同时开发,或者多个同事一起开发,服务器上的就不能更新到本地。是不能增量更新到本地,必须全部下载,比对下载也行,但是工程量打了就特别慢。

  • 2020-12-07 22:06:13

    System Extension Blocked - warning

    After upgrading your macOS computer to High Sierra 10.13.4 or higher (starting in April 2018), you may see a message about a System Extension Blocked. At Williams we have seen this warning appear for these programs: