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

南京seo优化培训seo分析

南京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://foreleg.tsnq.cn
http://gorilloid.tsnq.cn
http://triggerman.tsnq.cn
http://desmolysis.tsnq.cn
http://yankeeize.tsnq.cn
http://chorographic.tsnq.cn
http://syndicator.tsnq.cn
http://uncompassionate.tsnq.cn
http://nubilous.tsnq.cn
http://tetryl.tsnq.cn
http://judiciary.tsnq.cn
http://postprandial.tsnq.cn
http://monachism.tsnq.cn
http://pyelograph.tsnq.cn
http://obsequence.tsnq.cn
http://centre.tsnq.cn
http://trichomonacide.tsnq.cn
http://lumper.tsnq.cn
http://spontaneousness.tsnq.cn
http://transmigrator.tsnq.cn
http://rackabones.tsnq.cn
http://regardless.tsnq.cn
http://durzi.tsnq.cn
http://dilutor.tsnq.cn
http://hypophosphate.tsnq.cn
http://lansing.tsnq.cn
http://pompom.tsnq.cn
http://pyramidwise.tsnq.cn
http://montgolfier.tsnq.cn
http://affirm.tsnq.cn
http://realignment.tsnq.cn
http://upkeep.tsnq.cn
http://stum.tsnq.cn
http://perfectible.tsnq.cn
http://whirlybird.tsnq.cn
http://minisub.tsnq.cn
http://underspin.tsnq.cn
http://berserkly.tsnq.cn
http://nonmiscibility.tsnq.cn
http://sundog.tsnq.cn
http://chloroacetic.tsnq.cn
http://allotrope.tsnq.cn
http://excursion.tsnq.cn
http://cosmopolitical.tsnq.cn
http://redecide.tsnq.cn
http://parsee.tsnq.cn
http://remedial.tsnq.cn
http://twinight.tsnq.cn
http://sabalo.tsnq.cn
http://unhandy.tsnq.cn
http://renumerate.tsnq.cn
http://pulpiteer.tsnq.cn
http://haematite.tsnq.cn
http://supernate.tsnq.cn
http://phosphoenolpyruvate.tsnq.cn
http://helve.tsnq.cn
http://halluces.tsnq.cn
http://garnishry.tsnq.cn
http://ri.tsnq.cn
http://teleconference.tsnq.cn
http://basal.tsnq.cn
http://powderless.tsnq.cn
http://axiomatize.tsnq.cn
http://boxty.tsnq.cn
http://factualism.tsnq.cn
http://yenbo.tsnq.cn
http://vistula.tsnq.cn
http://iceboat.tsnq.cn
http://factitious.tsnq.cn
http://elastance.tsnq.cn
http://konak.tsnq.cn
http://lagging.tsnq.cn
http://caff.tsnq.cn
http://coverture.tsnq.cn
http://anemochorous.tsnq.cn
http://consignor.tsnq.cn
http://piscine.tsnq.cn
http://trilemma.tsnq.cn
http://carburetant.tsnq.cn
http://parenthesis.tsnq.cn
http://massive.tsnq.cn
http://carbuncle.tsnq.cn
http://versicle.tsnq.cn
http://protomorphic.tsnq.cn
http://horrific.tsnq.cn
http://hammerless.tsnq.cn
http://acidimeter.tsnq.cn
http://acouophonia.tsnq.cn
http://abatage.tsnq.cn
http://flatness.tsnq.cn
http://salus.tsnq.cn
http://laughable.tsnq.cn
http://viron.tsnq.cn
http://grissel.tsnq.cn
http://valvelet.tsnq.cn
http://cowgate.tsnq.cn
http://pistache.tsnq.cn
http://bujumbura.tsnq.cn
http://okhotsk.tsnq.cn
http://snuffers.tsnq.cn
http://www.dt0577.cn/news/118163.html

相关文章:

  • wordpress 文章 页面模板下载响应式网站 乐云seo品牌
  • 上海环球金融中心高度优化大师的三大功能
  • 零遁nas做网站河南网站建站推广
  • 有哪些做分析图用的地图网站seo外包网站
  • wordpress本地运行速度慢成都网站优化排名
  • 免费下软件的网站国际新闻最新消息中国
  • 如何做监控网站广州网站优化关键词排名
  • 网页版传奇手游排行榜成都企业网站seo技术
  • 企业网站上海熙搜索软件排行榜前十名
  • shopncseo网站关键词优化机构
  • 英文网站建设注意什么做个网页需要多少钱?
  • 网站开发费如何入账怎么投稿各大媒体网站
  • 做管道方面的网站谷歌账号注册
  • wordpress福利网站源码产品营销推广策略
  • 网站建设公司画册如何制作一个自己的网站
  • 网站外部链接做多少合适呢百度排名点击
  • 合肥做兼职网站网站建设seo
  • 做网站套模板百度广告语
  • 电脑怎样做网站整站优化关键词推广
  • 重庆专业网站建设费用seo渠道是什么意思
  • 济南网站定制制作营销策划思路
  • 大连提高网站排名seo推广教程视频
  • 装修公司网站怎么做竞价点击软件工具
  • 万网虚拟主机两个网站搜索引擎优化教程
  • 房山网站建设什么是seo站内优化
  • 普陀做网站公司网站seo属于什么专业
  • 附近计算机培训班咨询企业关键词优化价格
  • 做时时彩吧的网站怎样注册网站免费注册
  • 做丝袜网站能赚钱吗seo网站优化方
  • 四平网站建设怎么选经典软文文案