zoukankan      html  css  js  c++  java
  • LRUCache c#

    LRUCache是Least Recently Used 近期最少使用算法的缓存,是android提供的一个缓存工具类。可以以两种排序方式来输出缓存,一种是按插入顺序输出,一种是按最近最少方式输出,最近使用的放在队首,使用频率低的,间隔时间最长的放在队尾。
    下面是实现

    using System;
    using System.Collections.Generic;
    namespace LY.Helper
    {
    public class LRUCache<T>
    {
        private Dictionary<string, T> dict;
        private LinkedList<T> list;
        private int size = 0;
        private bool isSequence = false;
    
        public LRUCache(int sz):this(sz,false)
        {
            
        }
    
        public LRUCache(int sz, bool isSq)
        {
            isSequence = isSq;
            size = sz < 10 ? 10 : sz;
            dict = new Dictionary<string, T>(size);
            list = new LinkedList<T>();
        }
        
    
        public int Size
        {
            get { return size; }
            set { size = value < 10 ? 10 : value; }
        }
        
        public void Put(string key, T item)
        {
            T node;
            if(dict.TryGetValue(key, out node))
            {
                list.Remove(node);
    
                dict[key] = item;
                list.AddFirst(item);
            }
            else
            {
                if(list.Count == size)
                    list.RemoveLast();
                dict[key] = item;
                list.AddFirst(item);
            }
        }
        
        public T Get(string key)
        {
            T node;
            if(dict.TryGetValue(key, out node))
            {
                list.Remove(node);
                list.AddFirst(node);
                return node;
            }
            return default(T);
        }
    
        public ICollection<T> Values
        {
            get
            {
                if (isSequence)
                {
                    return dict.Values;
                }
                else
                {
                    return list;
                }
            }
        }
    }
    }

    构造函数中传入缓存大小和输出缓存顺序。
    我们在调用Put方法时,当缓存长度超过我们构造函数中传入的大小时,会将队尾的移除。将新传入的对象放在队首。
    我们从LRUCache中获取对象时,在Get方法中,会将对象移除,并置于队首。
    下面我们来进行测试

    private void btnTest_Click(object sender, EventArgs e)
    {
    LRUCache<int> lruCache = new LRUCache<int>(10);
    lruCache.Put("1", 1);
    lruCache.Put("2", 2);
    lruCache.Put("3", 3);
    lruCache.Put("4", 4);
    lruCache.Put("5", 5);
            lruCache.Get("2");
            lruCache.Get("3");
    
            Console.WriteLine("最近最少方式Test...");
            foreach (var item in lruCache.Values)
            {
                Console.WriteLine(item.ToString());
            }
    
            LRUCache<int> lruCache1 = new LRUCache<int>(10, true);
            lruCache1.Put("1", 1);
            lruCache1.Put("2", 2);
            lruCache1.Put("3", 3);
            lruCache1.Put("4", 4);
            lruCache1.Put("5", 5);
    
            lruCache1.Get("2");
            lruCache1.Get("3");
    
            Console.WriteLine("顺序方式Test...");
            foreach (var item in lruCache1.Values)
            {
                Console.WriteLine(item.ToString());
            }
        }
    View Code

    我们来看下输出结果

     
  • 相关阅读:
    109. 有序链表转换二叉搜索树
    108. 将有序数组转换为二叉搜索树
    235. 二叉搜索树的最近公共祖先
    538. 把二叉搜索树转换为累加树
    230. 二叉搜索树中第K小的元素
    669. 修剪二叉搜索树
    513. 找树左下角的值
    637. 二叉树的层平均值
    671. 二叉树中第二小的节点
    DDL-Oracle中的5种约束总结(未完待续)
  • 原文地址:https://www.cnblogs.com/czly/p/9138944.html
Copyright © 2011-2022 走看看