Skip to content

Commit

Permalink
add redis lock~
Browse files Browse the repository at this point in the history
  • Loading branch information
yswenli committed Aug 21, 2017
1 parent 5f583de commit a99d4db
Show file tree
Hide file tree
Showing 22 changed files with 28 additions and 41 deletions.
Binary file modified DriveTest/bin/Debug/DriveTest.exe
Binary file not shown.
Binary file modified DriveTest/bin/Debug/DriveTest.pdb
Binary file not shown.
Binary file modified DriveTest/bin/Debug/Wenli.Drive.Redis.dll
Binary file not shown.
Binary file modified DriveTest/bin/Debug/Wenli.Drive.Redis.pdb
Binary file not shown.
15 changes: 6 additions & 9 deletions DriveTest/bin/Debug/Wenli.Drive.Redis.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file modified DriveTest/obj/Debug/DriveTest.exe
Binary file not shown.
Binary file modified DriveTest/obj/Debug/DriveTest.pdb
Binary file not shown.
Binary file modified RedisDriveTest/bin/Debug/RedisDriveTest.exe
Binary file not shown.
Binary file modified RedisDriveTest/bin/Debug/RedisDriveTest.pdb
Binary file not shown.
Binary file modified RedisDriveTest/bin/Debug/Wenli.Drive.Redis.dll
Binary file not shown.
Binary file modified RedisDriveTest/bin/Debug/Wenli.Drive.Redis.pdb
Binary file not shown.
15 changes: 6 additions & 9 deletions RedisDriveTest/bin/Debug/Wenli.Drive.Redis.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file modified RedisDriveTest/obj/Debug/RedisDriveTest.exe
Binary file not shown.
Binary file modified RedisDriveTest/obj/Debug/RedisDriveTest.pdb
Binary file not shown.
24 changes: 10 additions & 14 deletions Wenli.Drive.Redis/Core/SERedisLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Wenli.Drive.Redis.Core
{
public partial class SERedisOperation : IRedisOperation
{

/// <summary>
/// 锁超时时间,防止线程在入锁以后,无限的执行等待
/// </summary>
Expand All @@ -17,35 +18,30 @@ public partial class SERedisOperation : IRedisOperation


private static readonly string _prex = "lock_";
/**
* 获得 lock.
* 实现思路: 主要是使用了redis 的setnx命令,缓存了锁.
* reids缓存的key是锁的key,所有的共享, value是锁的到期时间(注意:这里把过期时间放在value了,没有时间上设置其超时时间)
* 执行过程:
* 1.通过setnx尝试设置某个key的值,成功(当前没有这个锁)则返回,成功获得锁
* 2.锁已经存在则获取锁的到期时间,和当前时间比较,超时的话,则设置新的值
*
* @return true if lock is acquired, false acquire timeouted
* @throws InterruptedException in case of thread interruption
*/

/// <summary>
/// 利用StringSetIfNotExists实现锁
/// </summary>
/// <param name="key"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public bool Lock(string key, int timeout = 60 * 1000)
{
var lockKey = _prex + key;

while (timeout > 0)
{
long expires = DateTime.Now.Millisecond + _expireMsecs + 1;
long expires = DateTime.Now.AddMilliseconds(_expireMsecs).Ticks;
String expiresStr = expires.ToString();
if (this.StringSetIfNotExists(lockKey, expiresStr))
{
return true;
}
String currentValueStr = this.StringGet(lockKey);
if (currentValueStr != null && long.Parse(currentValueStr) < DateTime.Now.Millisecond)
if (currentValueStr != null && long.Parse(currentValueStr) <= DateTime.Now.Ticks)
{
String oldValueStr = this.StringGetSet(lockKey, expiresStr);
if (oldValueStr != null && long.Parse(oldValueStr) <= long.Parse(currentValueStr))
if (oldValueStr != null && oldValueStr == currentValueStr)
{
return true;
}
Expand Down
15 changes: 6 additions & 9 deletions Wenli.Drive.Redis/bin/Debug/Wenli.Drive.Redis.XML

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Wenli.Drive.Redis/bin/Debug/Wenli.Drive.Redis.dll
Binary file not shown.
Binary file modified Wenli.Drive.Redis/bin/Debug/Wenli.Drive.Redis.pdb
Binary file not shown.
Binary file modified Wenli.Drive.Redis/obj/Debug/Wenli.Drive.Redis.dll
Binary file not shown.
Binary file modified Wenli.Drive.Redis/obj/Debug/Wenli.Drive.Redis.pdb
Binary file not shown.

0 comments on commit a99d4db

Please sign in to comment.