日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

asp.net怎样解决高并发问题

發布時間:2025/3/15 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 asp.net怎样解决高并发问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

隊列+多線程+couchbase緩存 ,解決高并發問題。

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using GStreamCloud.Common; using MyGStreamcloud.Common.Utils;namespace RedPacketWebAPI.Models {[Serializable]public class Queueparam {public int uId { get; set; }public string Name { get; set; }}[Serializable]public class TestResponse{public int Code{get;set;}public string Message{get;set;}}[Serializable]public class TestQueue{public static Dictionary<string, TestResponse> output = new Dictionary<string, TestResponse>();public static Thread t;public TestQueue(){t = new Thread(new DoQueueProcess().ThreadDo);}public static Queue<Queueparam> queuelist = new Queue<Queueparam>();public static object lockqueue;public static void Enqueue(Queueparam p){lock (lockqueue){queuelist.Enqueue(p);}}public static Queueparam Dequeue(){lock (lockqueue){if (queuelist.Count > 0){return queuelist.Dequeue();}else{return null;}}}public static TestResponse popValue(string key){lock (lockqueue){if (output.ContainsKey(key)){TestResponse resp = output[key];output.Remove(key);return resp;}else{return null; }}}}public class DoQueueProcess{public bool stop = false;public void ThreadDo(){while (!stop){try{Queueparam param=TestQueue.Dequeue();if (param != null){DoManageSomething todo = new DoManageSomething();TestResponse resp= todo.Manage(param.Name);TestQueue.output.Add(param.uId.ToString(), resp);}}catch{ }try{Thread.Sleep(10);}catch{ }}}}public class DoManageSomething{public TestResponse Manage(Queueparam cpara){//TODO 具體處理業務TestResponse resp = new TestResponse();resp.Code = 0;resp.Message = cpara.Name + "處理成功";disposeCoucheBase(cpara);return resp;}public void disposeCoucheBase(Queueparam cpara){ulong num = CouchBaseProvider.Decrement(Constants.TAG_CLAIMCOUNT, cpara.uId.ToString());ulong userNum = CouchBaseProvider.Decrement(Constants.TAG_CLAIMCOUNT_USER, cpara.uId.ToString());}}public class MonitorThread{public static async Task<TestResponse> WaitForReslut(string UID, int timeOut){return await Task<TestResponse>.Run(() =>{TestResponse resp = new TestResponse();Stopwatch sw = new Stopwatch();sw.Start();while (sw.ElapsedMilliseconds > timeOut){try{if (TestQueue.output[UID] != null){return TestQueue.popValue(UID);}else{Thread.Sleep(10);}}catch{Thread.Sleep(10);}}sw.Stop();sw = null;return resp;});}} }

?

調用處:

ulong c = CouchBaseProvider.Increment("TAG_CLAIMCOUNT_USER",userId.ToString(), 1, 1, new TimeSpan(2, 0, 0));//couchbase自增函數if (c > 1)//couchebase判斷是否領取過 {resp.Code = -1010;resp.Message = "您已在隊列里面";}else //couchebase判斷是否領完 {int nums = Convert.ToInt32(CouchBaseProvider.Increment(Constants.TAG_CLAIMCOUNT, userId.ToString()));if (nums > Allnums){resp.Code = -1011;resp.Message = "人數已滿";}else{//TODO 進入隊列Queueparam p= new Queueparam();p.Name= userName;p.UID = System.Guid.NewGuid().ToString();p.redOrder = redOrder;TestQueue.Enqueue(p);int maxWait = 30000;TestQueue queue = new TestQueue();resp = await MonitorThread.WaitForReslut(p.UID, maxWait);}}

?

轉載于:https://www.cnblogs.com/MissQing/p/6425498.html

總結

以上是生活随笔為你收集整理的asp.net怎样解决高并发问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。