You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace RedisStuy
{
/// <summary>
/// Redis 做锁
/// </summary>
public class RedisLockStudy
{
#region 初始化
private IDatabase redisDatabase;
public RedisLockStudy()
{
redisDatabase = RedisHelper.GetRedisDatabase();
}
#endregion
public void ReidsLock()
{
string lockKey = "MyTestRedisKey";
string lockVlaue = Guid.NewGuid().ToString();
bool getLock = redisDatabase.LockTake(lockKey, lockVlaue, TimeSpan.FromSeconds(20), CommandFlags.None);
if (getLock)
{
try
{
//do other thing
}
finally
{
bool releaseLock = redisDatabase.LockRelease(lockKey, lockVlaue, CommandFlags.None);
if (releaseLock == false)
{
}
}
}
}
}
}