使用redis解决分布式单例

    //获取分布式锁
            BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
                {
                    return false;
                else
                    DateTime expired = DateTime.Now.AddSeconds(seconds);
                    return true;
        }
        //释放分布式锁
        public static void ExitLock(String lockName)
            BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
        }
        public void TestRedisLock()
            string lockName = "lockTest";
            {
                //执行获得分布式Lock的操作
                ExitLock(lockName);
            using (IRedisClient RClient = basicRedisClientManager.GetClient())
                {
                    using (IRedisTransaction IRT = RClient.CreateTransaction())
                        IRT.QueueCommand(r => { if (r.ContainsKey("key")) { r.Add("key", 1); } });
            }
            BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
                {
                    using (redisClient.AcquireLock(lockName + "_redisLock"))
                        if (!redisClient.ContainsKey(lockName))
                            DateTime expired = DateTime.Now.AddSeconds(seconds);
                            return true;
                    }
                return false;
        {
            string lockName = "zhq_lock";
            if (EnterLock(lockName, 100))
                //创建data
                Console.Write("lockName");
            }
            else
                while (true)
                    //try 
                    Thread.Sleep(1000);
                    if (EnterLock(lockName, 100))
                        TestRedisLock();
            }
==============================
            BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(ConfigurationManager.AppSettings["RedisServerIP"].ToString());
redisClient.AcquireLock("testlock", TimeSpan.FromSeconds(10));{}
lock=wait+ single
        public static bool EnterLock(string lockName, int seconds)
        {
            using (var redisClient = basicRedisClientManager.GetClient())
            {
                if (redisClient.ContainsKey(lockName))
                }
                {
                    redisClient.Add(lockName, "1", expired);
                }
            }
        {
            using (var redisClient = basicRedisClientManager.GetClient())
            {
                redisClient.Delete(lockName);
            }
        {
            if (EnterLock(lockName, 10))
            }
        }
            {
                if (!RClient.ContainsKey("key"))
                    {
                        IRT.Commit(); // 提交事务
                    }
                }
        {
            using (var redisClient = basicRedisClientManager.GetClient())
            {
                if (!redisClient.ContainsKey(lockName))
                    {
                        {
                            redisClient.Add(lockName, "1", expired);
                        }
                }
            }
        }
            {
                //执行获得分布式Lock的操作
                ExitLock(lockName);
            {
                {
                    {
                    }
                }
        }


        public static bool EnterLock(string lockName, int seconds)
=======================
using (redisClient.AcquireLock("_redisLock")){//逻辑}


 public static void TestRedisLock()


你可能感兴趣的:(java)