队列+多线程+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 > )
{
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();
}
catch
{ }
}
} } public class DoManageSomething
{
public TestResponse Manage(Queueparam cpara)
{
//TODO 具体处理业务
TestResponse resp = new TestResponse();
resp.Code = ;
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();
}
}
catch
{
Thread.Sleep();
}
}
sw.Stop();
sw = null;
return resp;
});
}
}
}

调用处:

                                ulong c = CouchBaseProvider.Increment("TAG_CLAIMCOUNT_USER",userId.ToString(), , , new TimeSpan(, , ));//couchbase自增函数if (c > )//couchebase判断是否领取过
{
resp.Code = -;
resp.Message = "您已在队列里面"; }
else //couchebase判断是否领完
{
int nums = Convert.ToInt32(CouchBaseProvider.Increment(Constants.TAG_CLAIMCOUNT, userId.ToString()));
if (nums > Allnums)
{
resp.Code = -;
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 = ;
TestQueue queue = new TestQueue();
resp = await MonitorThread.WaitForReslut(p.UID, maxWait);
}
}

最新文章

  1. SQLSERVER 获取datetime日期的查询语句
  2. Web获取客户端物理MAC地址(ocx插件)
  3. Linux Oracle删除归档日志
  4. Linux的phpstudy mysql登录
  5. 优测优社区干货精选|老司机乱谈编辑器之神——vim
  6. Innode引擎监控的开启的方法
  7. 关于C51内的code,idata,xdata
  8. Core Animation系列之CADisplayLink(转)
  9. 关于EditText组件在android4.4W中出现黄色感叹号的问题?
  10. ASP.NET不通过添加web引用的方式调用web service接口
  11. CSS3中only-child伪类选择器
  12. 前端测试时,常用SQL
  13. [转]SDN与OpenFlow技术简介
  14. angular 用拦截器统一处理http请求和响应 比如加token
  15. 关于php-fpm方式和apache配合使用的几点记录
  16. smbpasswd 和 pdbedit 的区别
  17. python第四十五课——继承性之多重继承
  18. FIDDLER的使用方法及技巧总结(连载五)FIDDLER的一些故障排除
  19. 洛谷P3168 [CQOI2015]任务查询系统 [主席树,差分]
  20. SonarQube Scanner for MSBuild

热门文章

  1. Beamer模板
  2. Python-S9——Day84-ORM项目实战之权限、form以及modelform
  3. Jmeter随笔一
  4. 【转】tomcat与apache,tomcat与servlet的区别
  5. PAT1028
  6. iOS中常见的自定义宏
  7. 【bzoj4448】[Scoi2015]情报传递 主席树
  8. Codeforces #990E Post Lamp
  9. zabbix3.4 源码部署
  10. 基于SSM3框架FreeMarker自定义指令(标签)实现