Esempio n. 1
0
        public void TestWhenLockedAnotherLockRequestIsRejected()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsFalse(locked, "lock taken, it shouldn't be possible");
            dlm.Unlock(lockObject);
        }
Esempio n. 2
0
        public void TestThatSequenceLockedUnlockedAndLockedAgainIsSuccessfull()
        {
            var dlm = new Redlock(ConnectionMultiplexer.Connect("127.0.0.1:6379"));

            Lock lockObject;
            Lock newLockObject;
            var locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out lockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(lockObject);
            locked = dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out newLockObject);
            Assert.IsTrue(locked, "Unable to get lock");
            dlm.Unlock(newLockObject);
        }
Esempio n. 3
0
        private static void Main(string[] args)
        {
            //127.0.0.1:6379:IP,端口
            //password:Redis密码
            //connectTimeout:连接超时时间,这里设置的是1000毫秒
            //connectRetry:重试连接次数
            //syncTimeout:同步操作默认超时时间
            //defaultDatabase:默认数据库
            //<add key="RedisTest"  WriteServer="127.0.0.1:6379,password=123456,connectTimeout=1000,connectRetry=1,syncTimeout=1000,defaultDatabase=15"/>
            var resourceName = "key";
            var dlm          = new Redlock.CSharp.Redlock(
                ConnectionMultiplexer.Connect("127.0.0.1:6379,connectTimeout=1000,connectRetry=1,syncTimeout=1000,defaultDatabase=15"),
                ConnectionMultiplexer.Connect("192.168.100.142:6379,connectTimeout=1000,connectRetry=1,syncTimeout=1000,defaultDatabase=15"));

            Lock locked = null;

            try
            {
                locked = dlm.Lock(
                    resourceName,
                    new TimeSpan(0, 30, 10)
                    );
            }
            finally
            {
                if (locked != null && locked.IsLock)
                {
                    dlm.Unlock(locked);
                }
            }
        }
Esempio n. 4
0
        public override bool Lock(string resourceName, int retryCount, TimeSpan retryDelay)
        {
            if (retryCount != 0)
            {
                _dlm = new Redlock.CSharp.Redlock(retryCount, retryDelay, _redisStrategy._client);
            }
            else if (_dlm == null)
            {
                _dlm = new Redlock.CSharp.Redlock(_redisStrategy._client);
            }

            var successfull = _dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out _lockObject);

            return(successfull);
        }
Esempio n. 5
0
        public override ICacheLock Lock()
        {
            if (_retryCount != 0)
            {
                _dlm = new Redlock.CSharp.Redlock(_retryCount, _retryDelay, _redisStrategy.Client);
            }
            else if (_dlm == null)
            {
                _dlm = new Redlock.CSharp.Redlock(_redisStrategy.Client);
            }

            var ttl = base.GetTotalTtl(_retryCount, _retryDelay);

            base.LockSuccessful = _dlm.Lock(_resourceName, TimeSpan.FromMilliseconds(ttl), out _lockObject);
            return(this);
        }
Esempio n. 6
0
        public override bool Lock(string resourceName, int retryCount, TimeSpan retryDelay)
        {
            if (retryCount != 0)
            {
                _dlm = new Redlock.CSharp.Redlock(retryCount, retryDelay, _redisStrategy.Client);
            }
            else if (_dlm == null)
            {
                _dlm = new Redlock.CSharp.Redlock(_redisStrategy.Client);
            }

            var ttl = (retryDelay.TotalMilliseconds > 0 ? retryDelay.TotalMilliseconds : 10)
                      *
                      (retryCount > 0 ? retryCount : 10);


            var successfull = _dlm.Lock(resourceName, TimeSpan.FromMilliseconds(ttl), out _lockObject);

            return(successfull);
        }
Esempio n. 7
0
        public bool CheckLock(string key, out Lock locker)
        {
            var lockKey = GetLockToken(key);

            return(_redlock.Lock(lockKey, new TimeSpan(0, 0, 10), out locker));
        }