主函数:
class Program
{
public static List<Thread> threads = new List<Thread>();
public static int count = 0;
public static List<P> plist = new List<P>();
static void Main(string[] args)
{
//数据生成
//for (int i = 0; i < 10; i++)
//{
// plist.Add(new P() { id = i, name = "p" + i, count = ((i + 1) * 10) });
// RedisHelper.SetEntryInHash("p", "p" + i, ((i + 1) * 10).ToString());
//}
//创建模拟线程
for (int i = 0; i < 11; i++)
{
Thread thread = new Thread(SecKill);
thread.IsBackground = true;
thread.Start(i);
}
Console.ReadKey();
}
线程调用函数
public static void SecKill(object obj) {
bool issuccess = false;
int indexthread = (int)obj;
if (plist.Count == 0)
{
Dictionary<string, string> keyValuePairs = RedisHelper.GetAllEntriesFromHash("p");
foreach (var item in keyValuePairs.Keys)
{
P p = new P() { id = Convert.ToInt32(item.Remove(0, 1)), count = Convert.ToInt32(keyValuePairs[item]), name = item };
p.isSure = p.count > 0;
plist.Add(p);
}
}
for (int i = 0; i < 1; i++)
{
// RedisHelper.SetEntryInHash("p", "p" + i, ((i + 1) * 10).ToString());//redis键名称,字典(字典键,字典值)
issuccess = RedisHelper.SecKill(i, ref plist);
Console.WriteLine(string.Format("indexthread:"+ indexthread+"号线程:" + i + ":" + (issuccess ? "抢到了!!!!!" : "没用抢到!!!!!!!")));
}
}
servcestack.redis3.9.7,帮助类
using ServiceStack.Common;
using ServiceStack.Redis;
using ServiceStack.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CsharpTest
{
//servcestack.redis3.9.7
public class RedisHelper
{
private readonly static string RedisPath = "123654@127.0.0.1:6379";
private readonly static PooledRedisClientManager _pool = null;
public static IRedisClient redisClient = null;
static RedisHelper()
{
if (redisClient == null)
{
_pool = new PooledRedisClientManager(new string[] { RedisPath }, new string[] { RedisPath }, new RedisClientManagerConfig() { MaxReadPoolSize = 50, MaxWritePoolSize = 50, AutoStart = true });
//redisClient = _pool.GetClient();
}
}
//修改hash的值
public static long IncrementValueInHash(string hashId, string key, int incrementBy) {
long count = 0;
using (IRedisClient r = _pool.GetClient()) {
count=r.IncrementValueInHash(hashId, key, incrementBy);
}
return count;
}
//设置hash表
public static bool SetEntryInHash(string hashId, string key, string value) {
bool issuccess = false;
using (IRedisClient r = _pool.GetClient())
{
issuccess = r.SetEntryInHash(hashId, key, value);
}
return issuccess;
}
/// <summary>
/// 模拟秒杀
/// </summary>
/// <returns></returns>
public static bool SecKill(int i,ref List<P> ps) {
bool issuccess = false;
using (IRedisClient r = _pool.GetClient())
{
using (IDisposable d=r.AcquireLock("lock"))
{
try
{
if (ps[i].isSure)
{
ps[i].isSure = r.IncrementValueInHash("p", ps[i].name, -1) > 0;
issuccess = true;
}
else
{
issuccess = false;
}
}
catch (Exception ex)
{
d.Dispose();
}
finally {
}
}
}
return issuccess;
}
public static Dictionary<string, string> GetAllEntriesFromHash(string hashId) {
Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
using (IRedisClient r = _pool.GetClient())
{
keyValuePairs= r.GetAllEntriesFromHash(hashId);
}
return keyValuePairs;
}
}
}
模拟类
public class P
{
public int id { get; set; }
public string name { get; set; }
public bool isSure { get; set; }
public int count { get; set; }
}