使用redis实现分布式锁,可以解决集群中需要单例的情况
2014-12-02 14:17
453 查看
//获取分布式锁
public static bool EnterLock(string lockName, int seconds)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
if (redisClient.ContainsKey(lockName))
{
return false;
}
else
{
DateTime expired = DateTime.Now.AddSeconds(seconds);
redisClient.Add(lockName, "1", expired);
return true;
}
}
}
//释放分布式锁
public static void ExitLock(String lockName)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
redisClient.Delete(lockName);
}
}
public void TestRedisLock()
{
string lockName = "lockTest";
if (EnterLock(lockName, 10))
{
//执行获得分布式Lock的操作
ExitLock(lockName);
}
}
==============================
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (IRedisClient RClient = basicRedisClientManager.GetClient())
{
if (!RClient.ContainsKey("key"))
{
using (IRedisTransaction IRT = RClient.CreateTransaction())
{
IRT.QueueCommand(r => { if (r.ContainsKey("key")) { r.Add("key", 1); } });
IRT.Commit(); // 提交事务
}
}
}
public static bool EnterLock(string lockName, int seconds)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
if (!redisClient.ContainsKey(lockName))
{
using (redisClient.AcquireLock(lockName + "_redisLock"))
{
if (!redisClient.ContainsKey(lockName))
{
DateTime expired = DateTime.Now.AddSeconds(seconds);
redisClient.Add(lockName, "1", expired);
return true;
}
}
}
return false;
}
}
=======================
using (redisClient.AcquireLock("_redisLock")){//逻辑}
redisClient.AcquireLock("testlock", TimeSpan.FromSeconds(10));{}
public static void TestRedisLock()
{
string lockName = "zhq_lock";
if (EnterLock(lockName, 100))
{
//执行获得分布式Lock的操作
//创建data
Console.Write("lockName");
ExitLock(lockName);
}
else
{
while (true)
{
//try
Thread.Sleep(1000);
if (EnterLock(lockName, 100))
{
TestRedisLock();
}
}
}
}
lock=wait+ single
//获取分布式锁
public static bool EnterLock(string lockName, int seconds)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
if (redisClient.ContainsKey(lockName))
{
return false;
}
else
{
DateTime expired = DateTime.Now.AddSeconds(seconds);
redisClient.Add(lockName, "1", expired);
return true;
}
}
}
//释放分布式锁
public static void ExitLock(String lockName)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
redisClient.Delete(lockName);
}
}
public void TestRedisLock()
{
string lockName = "lockTest";
if (EnterLock(lockName, 10))
{
//执行获得分布式Lock的操作
ExitLock(lockName);
}
}
==============================
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (IRedisClient RClient = basicRedisClientManager.GetClient())
{
if (!RClient.ContainsKey("key"))
{
using (IRedisTransaction IRT = RClient.CreateTransaction())
{
IRT.QueueCommand(r => { if (r.ContainsKey("key")) { r.Add("key", 1); } });
IRT.Commit(); // 提交事务
}
}
}
public static bool EnterLock(string lockName, int seconds)
{
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
using (var redisClient = basicRedisClientManager.GetClient())
{
if (!redisClient.ContainsKey(lockName))
{
using (redisClient.AcquireLock(lockName + "_redisLock"))
{
if (!redisClient.ContainsKey(lockName))
{
DateTime expired = DateTime.Now.AddSeconds(seconds);
redisClient.Add(lockName, "1", expired);
return true;
}
}
}
return false;
}
}
=======================
using (redisClient.AcquireLock("_redisLock")){//逻辑}
redisClient.AcquireLock("testlock", TimeSpan.FromSeconds(10));{}
public static void TestRedisLock()
{
string lockName = "zhq_lock";
if (EnterLock(lockName, 100))
{
//执行获得分布式Lock的操作
//创建data
Console.Write("lockName");
ExitLock(lockName);
}
else
{
while (true)
{
//try
Thread.Sleep(1000);
if (EnterLock(lockName, 100))
{
TestRedisLock();
}
}
}
}
lock=wait+ single
相关文章推荐
- 分布式架构学习之:使用Redis3.0集群实现Tomcat集群的Session共享
- 这是一个秒杀系统,即大量用户抢有限的商品,先到先得 用户并发访问流量非常大,需要分布式的机器集群处理请求 系统实现使用Java
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- 分布式架构学习之:032--使用Redis3.0集群实现Tomcat集群的Session共享
- spring session和redis集群打造分布式会话使用方案及错误解决
- 分布式中使用Redis实现Session共享(二)
- 使用scrapy,redis, mongodb实现的一个分布式网络爬虫
- 使用 Redis 实现分布式锁
- 设置tomcat集群使用redis实现分布式session
- (转)分布式中使用Redis实现Session共享(二)
- iOS 闭包中的[weak self]在什么情况下需要使用,什么情况下可以不加?
- (转)分布式中使用Redis实现Session共享(一)
- 分享在Linux下使用OSGi.NET插件框架快速实现一个分布式服务集群的方法
- 创建存储过程,以部门编号为参数返回该部门的人数及平均工资(返回一个值可以通过函数来实现,但如果要返回多个值,需要使用out或in out模式参数实现)
- 使用 Redis 实现分布式锁
- 深入理解Spring Redis的使用 (九)、通过Redis 实现 分布式锁 的 BUG,以及和数据库加锁的性能测试
- 使用 Redis 实现分布式系统轻量级协调技术
- redis中使用java脚本实现分布式锁
- Hi!欢迎使用一个Dynamo风格的分布式Redis集群分发管理系统WheatRedis - V2EX
- spring data redis 集群(sentinel实现)和simple spring memcached分布式初使用