Exemple #1
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);
                }
            }
        }
        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);
        }
Exemple #3
0
        internal RedisData(string key, IDatabase db, Redlock.CSharp.Redlock redlock)
        {
            _key     = key;
            Db       = db;
            _redlock = redlock;

            while (!CheckLock(key, out _locker))
            {
                // blocking
                Thread.Sleep(1);
            }
        }
        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);
        }
        public override bool Lock(string resourceName, int retryCount, TimeSpan retryDelay)
        {
            if (retryCount != 0)
            {
                _dlm = new Redlock.CSharp.Redlock(retryCount, retryDelay, _redisStragegy._client);
            }
            else if (_dlm == null)
            {
                _dlm = new Redlock.CSharp.Redlock(_redisStragegy._client);
            }

            var successfull = _dlm.Lock(resourceName, new TimeSpan(0, 0, 10), out _lockObject);
            return successfull;
        }
        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);
        }
Exemple #7
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);
        }
Exemple #8
0
        public override async Task <ICacheLock> LockAsync()
        {
            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);

            Tuple <bool, Lock> result = await _dlm.LockAsync(_resourceName, TimeSpan.FromMilliseconds(ttl)).ConfigureAwait(false);

            base.LockSuccessful = result.Item1;
            _lockObject         = result.Item2;
            return(this);
        }
        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);
        }
Exemple #10
0
 public MemoryDataModule()
 {
     _redis   = ConnectionMultiplexer.Connect("127.0.0.1");
     _redlock = new Redlock.CSharp.Redlock(_redis);
     Db       = _redis.GetDatabase();
 }