简体   繁体   English

该算法是LRU还是MRU?

[英]Is this algorithm implementation LRU or MRU?

I am working on implementing a MRU(Most Recently Used) cache in my project using C#. 我正在使用C#在我的项目中实现MRU(最近使用的)缓存。

I googled some conceptions and implementations about MRU, and its contrary, LRU(Least Recently Used), and found this article http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=626 that describes the implementation of MRU collection in C#. 我搜索了一些关于MRU的概念和实现,相反,LRU(最近最少使用),并发现这篇文章http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=626描述了在C#中收集MRU。

To confuse me is that I think this implementation is LRU rather than MRU. 令我困惑的是,我认为这个实现是LRU而不是MRU。 Could anyone help me to confirm this collection class is MRU or not? 有人可以帮我确认这个集合类是MRU吗?

Following code block is the whole MRUCollection class. 以下代码块是整个MRUCollection类。 Thanks. 谢谢。

class MruDictionary<TKey, TValue>
{
    private LinkedList<MruItem> items;
    private Dictionary<TKey, LinkedListNode<MruItem>> itemIndex;
    private int maxCapacity;
    public MruDictionary(int cap)
    {
        maxCapacity = cap;
        items = new LinkedList<MruItem>();
        itemIndex = new Dictionary<TKey, LinkedListNode<MruItem>>(maxCapacity);
    }
    public void Add(TKey key, TValue value)
    {
        if (itemIndex.ContainsKey(key))
        {
            throw new ArgumentException("An item with the same key already exists.");
        }
        if (itemIndex.Count == maxCapacity)
        {
            LinkedListNode<MruItem> node = items.Last;
            items.RemoveLast(); //Why do we move the last than first here? The node accessed recently is moved to the front of list.
            itemIndex.Remove(node.Value.Key);
        }
        LinkedListNode<MruItem> newNode = new LinkedListNode<MruItem>(new MruItem(key, value));
        items.AddFirst(newNode);
        itemIndex.Add(key, newNode);
    }
    public bool TryGetValue(TKey key, out TValue value)
    {
        LinkedListNode<MruItem> node;
        if (itemIndex.TryGetValue(key, out node))
        {
            value = node.Value.Value;
            items.Remove(node);
            items.AddFirst(node);
            return true;
        }
        value = default(TValue);
        return false;
    }
}

class MruItem
{
    private TKey _key;
    private TValue _value;
    public MruItem(TKey k, TValue v)
    {
        _key = key;
        _value = v;
    }
    public TKey Key
    {
        get { return _key; }
    }
    public TValue Value
    {
        get { return _value; }
    }
}


http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used
Most Recently Used (MRU): discards, in contrast to LRU, the most recently used items first. 最近使用的(MRU):与LRU相比,丢弃最先使用的项目。

According my understanding, as the node accessed recently is moved to the front of list, if the cache is full, we should remove the first node of list rather than last. 根据我的理解,由于最近访问的节点被移动到列表的前面,如果缓存已满,我们应该删除列表的第一个节点而不是最后一个节点。

It looks to me like an MRU implementation. 它看起来像MRU实现。 Notice how searches start from the beginning of the linked list and go back, and whenever a node is accessed it's moved to the front of the list. 请注意搜索如何从链表的开头开始并返回,每当访问节点时,它都会移动到列表的前面。 In Add(), the node is added using AddFirst(), and in TryGetValue(), it removes the node and adds it to the front of the list. 在Add()中,使用AddFirst()添加节点,在TryGetValue()中,它删除节点并将其添加到列表的前面。

Based on what is documented here: http://en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used 基于此处记录的内容: http//en.wikipedia.org/wiki/Cache_algorithms#Most_Recently_Used

It's LRU. 这是LRU。 Think about the items being a "ordered" list. 想想这些items是“有序”列表。

The most recently used item is at the "front". 最近使用的项目位于“前端”。 When a new item is added they call items.AddFirst(newNode); 添加新项目时,他们调用items.AddFirst(newNode); which adds it to the front of the list. 它将它添加到列表的前面。 When an item is "touched", they move it to the front of the list using these calls: 当项目被“触摸”时,他们会使用以下调用将其移动到列表的前面:

items.Remove(node);
items.AddFirst(node);

When the list is full, it pushes the "last" / "oldest" item from the list using items.RemoveLast(); 当列表已满时,它会使用items.RemoveLast();从列表中推送“last”/“最旧”项items.RemoveLast();

The cache is removing the "least recently used" items first when it hits capacity. 缓存在达到容量时首先删除“最近最少使用”的项目。

Microsoft's "MRU" lists correctly use an LRU cache replacement algorithm. Microsoft的“MRU”列表正确使用LRU缓存替换算法。

Note that Microsoft in this case uses different terminology for MRU lists than the cache community. 请注意,在这种情况下,Microsoft使用不同于MRU列表的术语而不是缓存社区。

The cache community uses MRU / LRU to talk about replacement (or eviction) strategies. 缓存社区使用MRU / LRU来讨论替换 (或逐出)策略。 When your cache is full, and you need to put a new item in the list, which item should be removed from the list? 当您的缓存已满,并且您需要在列表中放置一个新项目时,应从列表中删除哪个项目?

Microsoft provides tools for getting the most recently used items, like for a drop down or a recent documents list. Microsoft提供了用于获取最近使用的项目的工具,例如下拉列表或最近的文档列表。

This means that to correctly implement an MRU list, you need to implement an LRU Cache eviction strategy. 这意味着要正确实现MRU列表,您需要实施LRU Cache驱逐策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM