当前位置: 首页 > news >正文

营销团队网站建设暴风seo论坛

营销团队网站建设,暴风seo论坛,电子商务考研最佳方向,大型免费网站制作C# 实现Lru缓存 LRU 算法全称是最近最少使用算法(Least Recently Use),是一种简单的缓存策略。 通常用在对象池等需要频繁获取但是又需要释放不用的地方。 代码实现的基本原理就是使用链表,当某个元素被访问时(Get或…

C# 实现Lru缓存

LRU 算法全称是最近最少使用算法(Least Recently Use),是一种简单的缓存策略。
通常用在对象池等需要频繁获取但是又需要释放不用的地方。

代码实现的基本原理就是使用链表,当某个元素被访问时(Get或Set)就将该元素放到链表的头部或者尾部(根据用户自己定义规则即可)当达到了缓存的最大容量时对最不常使用的元素进行移除(移除的时候可以定义一系列的规则,用于判读如何移除,是否移除)

下面直接贴出来代码供大家参考

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;namespace ET
{public class LruCache<TKey, TValue>: IEnumerable<KeyValuePair<TKey, TValue>>{private const int DEFAULT_CAPACITY = 255;private int capacity;private ReaderWriterLockSlim locker;private Dictionary<TKey, TValue> dictionary;private LinkedList<TKey> linkedList;private Func<TKey, TValue, bool> checkCanPopFunc;private Action<TKey, TValue> popCb;public LruCache(): this(DEFAULT_CAPACITY){}public LruCache(int capacity){this.locker = new ReaderWriterLockSlim();this.capacity = capacity > 0? capacity : DEFAULT_CAPACITY;this.dictionary = new Dictionary<TKey, TValue>(DEFAULT_CAPACITY);this.linkedList = new LinkedList<TKey>();}/// <summary>/// 设置检测什么条件可以释放/// </summary>/// <param name="func"></param>public void SetCheckCanPopCallBack(Func<TKey, TValue, bool> func){this.checkCanPopFunc = func;}/// <summary>/// 设置如何释放/// </summary>/// <param name="action"></param>public void SetPopCallBack(Action<TKey, TValue> action){this.popCb = action;}public TValue this[TKey t]{get{if (TryGet(t, out TValue val))return val;throw new ArgumentException();}set{Set(t,value);}}public bool TryGet(TKey key, out TValue value){this.locker.EnterUpgradeableReadLock();try{bool b = this.dictionary.TryGetValue(key, out value);if (b){this.locker.EnterWriteLock();try{this.linkedList.Remove(key);this.linkedList.AddFirst(key);}finally{this.locker.ExitWriteLock();}}return b;}finally{this.locker.ExitUpgradeableReadLock();}}public void Set(TKey key, TValue value){this.locker.EnterWriteLock();try{if (this.checkCanPopFunc != null)this.MakeFreeSpace();this.dictionary[key] = value;this.linkedList.Remove(key);this.linkedList.AddFirst(key);//没有设置检测释放条件的话,容量超载了就移除if (this.checkCanPopFunc == null && this.linkedList.Count > this.capacity){this.dictionary.Remove(this.linkedList.Last.Value);this.linkedList.RemoveLast();}}finally{this.locker.ExitWriteLock();}}public Dictionary<TKey, TValue> GetAll(){return this.dictionary;}public void Remove(TKey key){this.locker.EnterWriteLock();try{this.dictionary.Remove(key);this.linkedList.Remove(key);}finally{this.locker.ExitWriteLock();}}public bool TryOnlyGet(TKey key, out TValue value){bool b = this.dictionary.TryGetValue(key, out value);return b;}public bool ContainsKey(TKey key){this.locker.EnterReadLock();try{return this.dictionary.ContainsKey(key);}finally{this.locker.ExitReadLock();}}public int Count{get{this.locker.EnterReadLock();try{return this.dictionary.Count;}finally{this.locker.ExitReadLock();}}}public int Capacity{get{this.locker.EnterReadLock();try{return this.capacity;}finally{this.locker.ExitReadLock();}}set{this.locker.EnterUpgradeableReadLock();try{if (value > 0 && this.capacity != value){this.locker.EnterWriteLock();try{this.capacity = value;while (this.linkedList.Count > this.capacity){this.linkedList.RemoveLast();}}finally{this.locker.ExitWriteLock();}}}finally{this.locker.ExitUpgradeableReadLock();}}}public ICollection<TKey> Keys{get{this.locker.EnterReadLock();try{return this.dictionary.Keys;}finally{this.locker.ExitReadLock();}}}public ICollection<TValue> Values{get{this.locker.EnterReadLock();try{return this.dictionary.Values;}finally{this.locker.ExitReadLock();}}}public void Clear(){this.dictionary.Clear();this.linkedList.Clear();}private void MakeFreeSpace(){LinkedListNode<TKey> node = this.linkedList.Last;//检测最不常用的10个int max_check_free_times = 10; //最大检测空闲次数int cur_check_free_time = 0; //当前检测空闲次数while (this.linkedList.Count + 1 > this.capacity){if (node == null)break;LinkedListNode<TKey> tuple_prev = node.Previous;if (this.checkCanPopFunc == null || this.checkCanPopFunc(node.Value, this.dictionary[node.Value])){//可以释放var value = this.dictionary[node.Value];this.dictionary.Remove(node.Value);this.linkedList.RemoveLast();this.popCb?.Invoke(node.Value, value);}else{cur_check_free_time++;if (cur_check_free_time >= max_check_free_times){break;}}node = tuple_prev;}}public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator(){foreach (var item in this.dictionary){yield return item;}}IEnumerator IEnumerable.GetEnumerator(){foreach (var item in this.dictionary){yield return item;}}}
}
http://www.hkea.cn/news/154617/

相关文章:

  • 锐捷网络公司排名seo技术介绍
  • 新圩做网站公司拼多多代运营一般多少钱
  • 免费网站可以做cpa?短视频营销的优势
  • b2b外贸营销型网站如何做电商赚钱
  • 建设无障碍网站seo分析报告怎么写
  • 电子商务网站开发进什么科目模板自助建站
  • 威海市住房和城乡建设局官方网站北京seo营销公司
  • 开网页卡优化关键词排名工具
  • wordpress右侧文章归档东莞公司seo优化
  • 个人网站建设需求说明书免费外链生成器
  • 湖南网站建设的公司排名网页制作网站制作
  • 公司网页网站建设 ppt模板app开发公司排行榜
  • 网站开发yuanmus联合早报 即时消息
  • 为什么只有中国人怕疫情seo 页面
  • 网站开发总结报告十大门户网站
  • 临淄信息港发布信息临沂seo建站
  • 门户网站系统介绍企业推广哪个平台好
  • 免费网站建站排行榜网站策划报告
  • 网站设计的评估最近发生的热点新闻
  • 设建网站广告投放渠道
  • 日ip5000的网站怎么做如何提高网站在搜索引擎中的排名
  • 网站描文本链接怎么做深圳互联网营销
  • 一个服务器做两个网站自己做网站
  • 百草味网站建设的活动方案营销型企业网站有哪些
  • 论文课程网站 建设背景项目推广方式有哪些
  • 内部网站建设关键词优化推广策略
  • 一个公司可以做几个网站备案贵阳网络推广排名
  • 武汉高端网站建设免费广告网
  • 大理建网站常用于网站推广的营销手段是
  • js怎么做网站跨境电商网站