之前有写过一篇介绍c#操作redis的文章 http://www.cnblogs.com/axel10/p/8459434.html ,这篇文章中的案例使用了StringIncrement来实现了高并发情况下key值的稳定增加,但如果要用锁的方式而不是StringIncrement方法,那该怎么做呢?
LockTake涉及到三个参数:key,token和TimeSpan,分别表示redis数据库中该锁的名称、锁的持有者标识和有效时间。下面将用一个多线程增加key值的案例来演示LockTake/LockRelease的用法。
using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Core.Configuration;
using StackExchange.Redis.Extensions.Newtonsoft;
using System;
using System.Threading;namespace RedisTest
{
class Program
{
static RedisValue Token = Environment.MachineName;
static RedisKey Key = "lock"; static void Ins()
{
Thread thread = new Thread(() =>
{
for (int i = ; i < ; i++)
{ if (client.Database.LockTake(Key, Token, TimeSpan.MaxValue)) //key表示的是redis数据库中该锁的名称,不可重复。 Token用来标识谁拥有该锁并用来释放锁。TimeSpan表示该锁的有效时间。
{
try
{
int key = client.Get<int>("key"); client.Add("key", key + );
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
client.Database.LockRelease(Key, Token);
}
}
else
{
Console.WriteLine("locking");
while (!TryAgain())
{
Thread.Sleep();
}
}
}
});
thread.Start();
} private static StackExchangeRedisCacheClient client; static void Main(string[] args)
{
var redisConfiguration = new RedisConfiguration() //配置
{
Hosts = new RedisHost[]
{
new RedisHost() {Host = "127.0.0.1", Port = }
}
}; client = new StackExchangeRedisCacheClient(new NewtonsoftSerializer(), redisConfiguration);
client.Add("key", ); for (int j = ; j < ; j++)
{
Ins();
} Thread.Sleep(); int i = client.Get<int>("key");
Console.WriteLine(i);
Console.ReadKey();
} static bool TryAgain(int value)
{
if (client.Database.LockTake(Key, Token, TimeSpan.MaxValue))
{
try
{
int key = client.Get<int>("key"); client.Add("key", key + value);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
client.Database.LockRelease(Key, Token);
} return true;
}
else
{
return false;
}
}
}
}