今天就跟大家聊聊有關怎么在C#中通過LRU實現(xiàn)通用高效的超時連接探測,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
網(wǎng)站設計、網(wǎng)站制作的關注點不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給創(chuàng)新互聯(lián)建站一個展示的機會來證明自己,這并不會花費您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。
什么是LRU
在這里還是要大概介紹一下LRU,LRU算法的設計原則是:如果一個數(shù)據(jù)在最近一段時間沒有被訪問到,那么在將來它被訪問的可能性也很小.也就是說,當限定的空間已存滿數(shù)據(jù)時,應當把最久沒有被訪問到的數(shù)據(jù)淘汰.當然在這里并不需要使用到自動淘汰機制,只需要把未位到達超時的連接清除即可。
在C#中如何實現(xiàn)LRU
C#并不存在這樣的數(shù)據(jù)結構,不過有一個結構很適合實現(xiàn)LRU,這個結構就是LinkedList雙向鏈表,通過以下結構圖就容易理解通過LinkedList實現(xiàn)LRU
通過LinkedList的功能我們可以把活越項先移出來,然后再把項移到頭部。在這里需要注意LinkedList的Remove方法,它有兩個重載版本,兩個版本的復雜度不一樣。一個是O(n)一個是O(1)所以使用上一定要注意,否則在數(shù)據(jù)多的情況下效率差別巨大(這些細節(jié)都可以通過源代碼來查看)!
代碼實現(xiàn)
前面已經(jīng)大概講述的原理,接下來要做的就是代碼實現(xiàn)了。第一步需要制訂一個基礎可控測對象規(guī)則接口,這樣就可以讓現(xiàn)有的已經(jīng)實現(xiàn)的功能實現(xiàn)它并可得到相關功能的支持。
public interface IDetector { double ActiveTime { get; set; } LinkedListNode<IDetector> DetectorNode { get; set; } }
接口定義了兩個屬性,一個是最近活越時間,另一個就是LinkedListNode<IDetector>
這個屬性比交關鍵,通過LinkedListNode<IDetector>
可以讓LinkedList在Remove時復雜度為O(1).接下來就要針對基于LRU算法處理超時制定一個應用規(guī)則
public interface ILRUDetector { void Update(IDetector item); void Detection(int timeout); double GetTime(); Action<IList<IDetector>> Timeout { get; set; } }
規(guī)則也是比較簡單,Update用于更新跟蹤對象,一般在處理接受ping或pong包后進行調用;Detection方法是探測超出指定時間的對象,時間當位是毫秒,如果存在有超時的對象則觸發(fā)Timeout事件;GetTime是獲取探測器已經(jīng)運行的時間單位毫秒!規(guī)則定好了那接著要做的事實就是要實現(xiàn)它:
class LRUDetector : ILRUDetector, IDisposable { public LRUDetector() { mTimeWatch = new System.Diagnostics.Stopwatch(); mTimeWatch.Restart(); } private Buffers.XSpinLock xSpinLock = new Buffers.XSpinLock(); private System.Diagnostics.Stopwatch mTimeWatch; private LinkedList<IDetector> mItems = new LinkedList<IDetector>(); public Action<IList<IDetector>> Timeout { get; set; } public void Detection(int timeout) { double time = GetTime(); List<IDetector> result = new List<IDetector>(); using (xSpinLock.Enter()) { LinkedListNode<IDetector> last = mItems.Last; while (last != null && (time - last.Value.ActiveTime) > timeout) { mItems.Remove(last); result.Add(last.Value); last.Value.DetectorNode = null; last = mItems.Last; } } if (Timeout != null && result.Count > 0) Timeout(result); } public void Update(IDetector item) { using (xSpinLock.Enter()) { if (item.DetectorNode == null) item.DetectorNode = new LinkedListNode<IDetector>(item); item.ActiveTime = GetTime(); if (item.DetectorNode.List == mItems) mItems.Remove(item.DetectorNode); mItems.AddFirst(item); } } public void Dispose() { mItems.Clear(); } public double GetTime() { return mTimeWatch.Elapsed.TotalMilliseconds; } }
代碼并不復雜,相信不用過多解釋也能看懂相關操作原理。
測試
既然功能已經(jīng)實現(xiàn),接下來就要對代碼進行測試看運行效果。測試代碼比較簡單首先開啟一個Timer定時執(zhí)行Detection,另外開一個線程去調用Update方法
class Program { public class TestDetector : IDetector { public double ActiveTime { get; set; } public string Name { get; set; } public LinkedListNode<IDetector> DetectorNode { get; set; } } static void Main(string[] args) { LRUDetector lRUDetector = new LRUDetector(); lRUDetector.Timeout = (items) => { foreach (TestDetector item in items) Console.WriteLine($"{(item.Name)} timeout {lRUDetector.GetTime() - item.ActiveTime}ms"); }; System.Threading.Timer timer = null; timer = new System.Threading.Timer(o => { timer.Change(-1, -1); lRUDetector.Detection(5000); timer.Change(5000, 5000); }, null, 5000, 5000); System.Threading.ThreadPool.QueueUserWorkItem(o => { int i = 0; while (true) { System.Threading.Thread.Sleep(500); i++; TestDetector testDetector = new TestDetector(); testDetector.Name = "my name is " + i; lRUDetector.Update(testDetector); } }); Console.Read(); } }
運行效果:
看完上述內(nèi)容,你們對怎么在C#中通過LRU實現(xiàn)通用高效的超時連接探測有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
文章標題:怎么在C#中通過LRU實現(xiàn)通用高效的超時連接探測
瀏覽地址:http://www.chinadenli.net/article26/gjcijg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、網(wǎng)站收錄、自適應網(wǎng)站、服務器托管、做網(wǎng)站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)