forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
demo/demo-win/CYQ.Data.Lock/DistributedLock/Redis.Idempotent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} |