Skip to content

Commit

Permalink
增加幂等性demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyq1162 committed Jan 21, 2024
1 parent 7d190e9 commit 591a246
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="File.Lock.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Redis.Idempotent.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\src\CYQ.Data.csproj">
Expand Down
51 changes: 51 additions & 0 deletions demo/demo-win/CYQ.Data.Lock/DistributedLock/File.Lock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using CYQ.Data.Lock;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace DistributedLockTest
{
class FileLockDemo
{
public static void Start()
{
for (int i = 0; i < 10000; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(FileLock), i);
}
Console.Read();
}
static int ok = 0;
static int fail = 0;
static void FileLock(object i)
{
string key = "myLock";
bool isOK = false;
try
{

isOK = DistributedLock.File.Lock(key, 2000);
if (isOK)
{
ok++;
Console.WriteLine(ok + "OK");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁成功。");
}
else
{
fail++;
Console.WriteLine(fail + "Fail ----------------------------");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁失败!");
}
}
finally
{
if (isOK)
{
DistributedLock.File.UnLock(key);
}
}
}
}
}
37 changes: 2 additions & 35 deletions demo/demo-win/CYQ.Data.Lock/DistributedLock/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,9 @@ class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10000; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(FileLock), i);
}
RedisIdempotent.Start();
//FileLockDemo.Start();
Console.Read();
}
static int ok = 0;
static int fail = 0;
static void FileLock(object i)
{
string key = "myLock";
bool isOK = false;
try
{

isOK = DistributedLock.File.Lock(key, 2000);
if (isOK)
{
ok++;
Console.WriteLine(ok+"OK");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁成功。");
}
else
{
fail++;
Console.WriteLine(fail + "Fail ----------------------------");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁失败!");
}
}
finally
{
if (isOK)
{
DistributedLock.File.UnLock(key);
}
}
}
}
}
52 changes: 52 additions & 0 deletions demo/demo-win/CYQ.Data.Lock/DistributedLock/Redis.Idempotent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using CYQ.Data.Lock;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using CYQ.Data;
namespace DistributedLockTest
{
/// <summary>
/// Redis 幂等性
/// </summary>
class RedisIdempotent
{
public static void Start()
{
AppConfig.Redis.Servers = "127.0.0.1:6379";
for (int i = 0; i < 1000000; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Idempotent), i);
}
Console.Read();
}
static int ok = 0;
static int fail = 0;
static void Idempotent(object i)
{
string key = "myLock";
bool isOK = false;
try
{

isOK = DistributedLock.Redis.Idempotent(key, 2.0/60);
if (isOK)
{
ok++;
Console.WriteLine(ok + " - OK");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁成功。");
}
else
{
// fail++;
// Console.WriteLine(fail + "Fail ----------------------------");
//Console.WriteLine("数字:" + i + " -- 线程ID:" + Thread.CurrentThread.ManagedThreadId + " 获得锁失败!");
}
}
finally
{

}
}
}
}

0 comments on commit 591a246

Please sign in to comment.