LRU最近最少使用缓存集合
生活随笔
收集整理的這篇文章主要介紹了
LRU最近最少使用缓存集合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ?編碼中涉及到資源管理就會經常使用到最近最少使用淘汰原理(LRU),比如說最近打開的文章列表管理、或者游戲中動態加載地圖、音樂一樣。使用LRU可以提高效率。本文實現了一個完整功能的LRU集合,可用于各種諸如此類需要緩存機制的地方。
/// <summary>/// 使用最近最少算法進行淘汰的緩存集合,用于緩存數據./// 提供了多線程安全訪問功能/// </summary>/// <remarks> /// 1、調用方在每使用一個數據之后,調用UpdateRecentlyUsed函數將該數據/// 置為最近訪問。函數會將該條目的讀取權值設為最大值。/// 2、在使用Add或者Insert添加條目時,如果緩存數目已達到設定的CacheSize值,/// 則會根據刪除掉緩存列表中讀取權值最小的條目。再進行添加/// </remarks>public class LRUCacheList<T> : IList<T>{#region Property/// <summary>/// 實際存放緩存條目的列表/// </summary>private List<T> m_listData;/// <summary>/// 存放與m_listData依次對應條目的最近讀取記錄/// </summary>private List<uint> m_listReadWeight;/// <summary>/// 當前最大的讀取記錄權值,用來計算下一個讀取值/// </summary>private uint m_iMaxWeight;/// <summary>/// 一個無符號整數的最大值/// </summary>private const uint c_MaxWeight = 0xffffffff;private Object m_lockObj = new object();/// <summary>/// 讀取和設置緩存大小,指定最多緩存的對象數目/// </summary>public int CacheSize { get; set; }#endregion#region Method/// <summary>/// /// </summary>/// <param name="cacheSize">緩存數目</param>public LRUCacheList(int cacheSize){m_listData = new List<T>(cacheSize);m_listReadWeight = new List<uint>(cacheSize);CacheSize = cacheSize;}/// <summary>/// 移除掉最近最少使用的條目/// </summary>protected void RemoveLeastRecentlyUsed(){lock (m_lockObj){uint min = c_MaxWeight;int minIndex = -1;for (int i = 0; i < m_listReadWeight.Count; i++){if (m_listReadWeight[i] < min){min = m_listReadWeight[i];minIndex = i;}}if (minIndex != -1){m_listReadWeight.RemoveAt(minIndex);m_listData.RemoveAt(minIndex);}}}/// <summary>/// 更新指定位置的權重值為最近訪問/// </summary>/// <param name="index"></param>public void UpdateRecentlyUsed(T item){lock (m_lockObj){int index = m_listData.IndexOf(item);if (index != -1)UpdateRecentlyUsed(index);}}/// <summary>/// 更新指定位置的權重值為最近訪問/// </summary>/// <param name="index"></param>public void UpdateRecentlyUsed(int index){m_listReadWeight[index] = NextMaxWeight();}/// <summary>/// 清除掉所有緩存的內容/// </summary>public void Clear(){lock (m_lockObj){if (m_listData != null)m_listData.Clear();if (m_listReadWeight != null)m_listReadWeight.Clear();m_iMaxWeight = 0;}}/// <summary>/// 獲取并設置下一個最大的權重值/// </summary>/// <returns></returns>public uint NextMaxWeight(){lock (m_lockObj){//到達最大值重置if (m_iMaxWeight == c_MaxWeight - 1){m_iMaxWeight = 0;for (int i = 0; i < m_listReadWeight.Count; i++){m_listReadWeight[i] = 0;}}m_iMaxWeight++;return m_iMaxWeight;}}/// <summary>/// 添加一個條目并設置為最近訪問/// </summary>/// <param name="item"></param>public void AddRecently(T item){lock (m_lockObj){while (CacheSize > 0 && m_listData.Count >= CacheSize)RemoveLeastRecentlyUsed();m_listData.Add(item);m_listReadWeight.Add(NextMaxWeight());}}/// <summary>/// 插入一個條目并設置為最近訪問/// </summary>/// <param name="index"></param>/// <param name="item"></param>public void InsertRecently(int index, T item){lock (m_lockObj){while (CacheSize > 0 && m_listData.Count >= CacheSize)RemoveLeastRecentlyUsed();m_listData.Insert(index, item);m_listReadWeight.Insert(index, NextMaxWeight());}}#endregion#region 實現IList<T> 接口public void Add(T item){lock (m_lockObj){while (CacheSize > 0 && m_listData.Count >= CacheSize)RemoveLeastRecentlyUsed();m_listData.Add(item);m_listReadWeight.Add(0);}}/// <summary>/// /// </summary>/// <param name="item"></param>/// <returns></returns>public int IndexOf(T item){return m_listData.IndexOf(item);}public void Insert(int index, T item){lock (m_lockObj){while (CacheSize > 0 && m_listData.Count >= CacheSize)RemoveLeastRecentlyUsed();m_listData.Insert(index, item);m_listReadWeight.Insert(index, 0);}}public void RemoveAt(int index){lock (m_lockObj){m_listData.RemoveAt(index);m_listReadWeight.RemoveAt(index);}}public T this[int index]{get{return m_listData[index];}set{m_listData[index] = value;}}public bool Contains(T item){return m_listData.Contains(item);}public void CopyTo(T[] array, int arrayIndex){m_listData.CopyTo(array, arrayIndex);}public int Count{get { return m_listData.Count; }}public bool IsReadOnly{get { return false; }}public bool Remove(T item){lock (m_lockObj){int index = m_listData.IndexOf(item);if (index != -1)m_listReadWeight.RemoveAt(index);return m_listData.Remove(item);}}public IEnumerator<T> GetEnumerator(){return m_listData.GetEnumerator();}System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator(){return m_listData.GetEnumerator();}#endregion}
轉載于:https://www.cnblogs.com/Kecp/archive/2012/12/02/2798579.html
總結
以上是生活随笔為你收集整理的LRU最近最少使用缓存集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Wing IDE 4.1破解教程
- 下一篇: Vue 后台管理