Пример #1
0
        public bool AcquireLock(string key, int timeout)
        {
            var locker = GetLocker(key);

            lockTaken = System.Threading.Monitor.TryEnter(locker, timeout);
            //进程锁获取失败
            if (!lockTaken)
            {
                return(false);
            }

            try
            {
                redisClient = RedisClientProvider.CreateRedisClient(connName);
                redisLocker = new InnerRedisLock(redisClient, key, TimeSpan.FromMilliseconds(timeout));
                return(true);
            }
            catch (Exception ex)
            {
                System.Threading.Monitor.Exit(locker);

                if (redisLocker != null)
                {
                    redisLocker.Dispose();
                }
            }
            return(false);
        }
        public async Task RedisValidationCacheDataRepository_AddMultipleValuesInTheSameKeyAndRemoveThem_ShouldWork()
        {
            // Arrange
            const int NumberOfItems  = 100;
            var       clientProvider = new RedisClientProvider();
            var       repository     = new RedisValidationCacheDataRepository(clientProvider);
            var       key            = Guid.NewGuid().ToString();

            var values = new List <IValidationCacheEntity>(NumberOfItems);

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            for (int i = 0; i < NumberOfItems; ++i)
            {
                var now     = DateTime.Now + TimeSpan.FromHours(i);
                var content = $"{key} {i} {now.ToLongTimeString()}";
                var value   = new ValidationCacheEntity
                {
                    Content    = content,
                    LastUpdate = now,
                    Status     = (ValidationStatus)(i % 3)
                };

                values.Add(value);
                await repository.Add(key, value);
            }

            values.TrimExcess();
            await repository.SaveChangesAsync();

            // Act: Get
            var valuesFromDb = repository.GetAll(key).ToList();

            // Assert: Get
            Assert.AreEqual(NumberOfItems, values.Count);
            Assert.AreEqual(NumberOfItems, valuesFromDb.Count);

            foreach (var value in values)
            {
                var valueFromDb = valuesFromDb.Single(v => v.Content == value.Content);
                Assert.IsNotNull(valueFromDb);

                Assert.AreEqual(value.Status, valueFromDb.Status);
                Assert.AreEqual(value.LastUpdate.ToLongTimeString(), valueFromDb.LastUpdate.ToLongTimeString());
                Assert.AreEqual(value.Content, valueFromDb.Content);
            }

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Пример #3
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_AddMultipleValuesInTheSameKeyAndRemoveThem_ShouldWork()
        {
            // Arrange
            const int NumberOfItems  = 1000;
            var       clientProvider = new RedisClientProvider();
            var       repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var       key            = Guid.NewGuid().ToString();

            var values = new List <ITweet>(NumberOfItems);

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            for (int i = 0; i < NumberOfItems; ++i)
            {
                var now     = DateTime.Now + TimeSpan.FromHours(i);
                var content = $"{key} {i} {now.ToLongTimeString()}";
                var value   = new Tweet
                {
                    Id       = i,
                    PostedOn = now,
                    Content  = content
                };

                values.Add(value);
                await repository.Add(key, value);
            }

            values.TrimExcess();
            await repository.SaveChangesAsync();

            // Act: Get
            var valuesFromDb = repository.GetAll(key).ToList();

            // Assert: Get
            Assert.AreEqual(NumberOfItems, values.Count);
            Assert.AreEqual(NumberOfItems, valuesFromDb.Count);

            foreach (var value in values)
            {
                var valueFromDb = valuesFromDb.Single(v => v.Id == value.Id);
                Assert.IsNotNull(valueFromDb);

                Assert.AreEqual(value.Id, valueFromDb.Id);
                Assert.AreEqual(value.PostedOn.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());
                Assert.AreEqual(value.Content, valueFromDb.Content);
            }

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Пример #4
0
        static RedisOperation()
        {
#if WEB_API
            redisClient = RedisClientProvider.Connect(Global.Instance.ConfigManager.RedisConstr, Global.Instance.ConfigManager.RedisDbId);
#else
            var config = SystemCommonConfig.Instance;
            redisClient = RedisClientProvider.Connect(config.RedisConstr, config.RedisDbId);
#endif
            redisClient.ConnectInitCompleted       += RedisClient_ConnectInitCompleted;
            _redisReceiveMessageQueue.MessageComing = _redisReceiveMessageQueue_MessageComing;
            redisClient.Satrt();
        }
        public void RedisValidationCacheDataRepository_AddNewValidKeyValuePairAndThenGetAllAndRemoveIt_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisValidationCacheDataRepository(clientProvider);
            var key            = Guid.NewGuid().ToString();
            var value          = new ValidationCacheEntity
            {
                Content    = Guid.NewGuid().ToString(),
                LastUpdate = DateTime.UtcNow,
                Status     = ValidationStatus.Invalid
            };

            // Act: Add
            var added = repository.Add(key, value);

            // Assert: Add
            Assert.That(async() => await added, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Act: Get
            var valuesFromDb = repository.GetAll(key);
            var valueFromDb  = valuesFromDb.Single();

            // Assert: Get
            Assert.AreEqual(1, valuesFromDb.Count());

            Assert.IsNotNull(valueFromDb);

            Assert.AreEqual(value.Status, valueFromDb.Status);
            Assert.AreEqual(value.Content, valueFromDb.Content);
            Assert.AreEqual(value.LastUpdate.ToLongDateString(), valueFromDb.LastUpdate.ToLongDateString());
            Assert.AreEqual(value.LastUpdate.ToLongTimeString(), valueFromDb.LastUpdate.ToLongTimeString());

            // Act: Remove value
            var removedValue = repository.Remove(key, value);

            // Assert: Remove value
            Assert.That(async() => await removedValue, Is.EqualTo(true));

            // Act: Remove
            var removed = repository.Remove(key);

            // Assert: Remove
            Assert.That(async() => await removed, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            // Expected internal catch of "ServiceStack.Redis.RedisResponseException : Background save already in progress"
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(1L));
        }
Пример #6
0
        public void RedisKeyCollectionValuePairsRepositoryOfTweet_AddNewValidKeyValuePairAndThenGetAllAndRemoveIt_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var key            = Guid.NewGuid().ToString();
            var value          = new Tweet
            {
                Id       = 0,
                Content  = Guid.NewGuid().ToString(),
                PostedOn = DateTime.UtcNow
            };

            // Act: Add
            var added = repository.Add(key, value);

            // Assert: Add
            Assert.That(async() => await added, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Act: Get
            var valuesFromDb = repository.GetAll(key);
            var valueFromDb  = valuesFromDb.Single();

            // Assert: Get
            Assert.AreEqual(1, valuesFromDb.Count());

            Assert.IsNotNull(valueFromDb);

            Assert.AreEqual(value.Id, valueFromDb.Id);
            Assert.AreEqual(value.Content, valueFromDb.Content);
            Assert.AreEqual(value.PostedOn.ToLongDateString(), valueFromDb.PostedOn.ToLongDateString());
            Assert.AreEqual(value.PostedOn.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());

            // Act: Remove value
            var removedValue = repository.Remove(key, value);

            // Assert: Remove value
            Assert.That(async() => await removedValue, Is.EqualTo(true));

            // Act: Remove
            var removed = repository.Remove(key);

            // Assert: Remove
            Assert.That(async() => await removed, Is.EqualTo(true));

            // Act + Assert: SaveChanges
            // Expected internal catch of "ServiceStack.Redis.RedisResponseException : Background save already in progress"
            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(1L));
        }
Пример #7
0
        public async Task RedisKeyValuePairsRepositoryOfTweet_GetKeys_ShouldWork()
        {
            // Arrange
            const int NumberOfKeys = 10;
            var       listOfKeys   = Enumerable.Range(0, NumberOfKeys)
                                     .Select(i => Guid.NewGuid().ToString() + i)
                                     .ToList();

            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyValuePairsRepository <ITweet>(clientProvider);

            var value = new Tweet
            {
                Id       = 0,
                Content  = Guid.NewGuid().ToString(),
                PostedOn = DateTime.UtcNow
            };

            // Act: Add
            foreach (var key in listOfKeys)
            {
                await repository.Add(key, value);
            }

            // Act: Get Keys
            var keysAfterAdd = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(NumberOfKeys, keysAfterAdd.Count, $"Number of keys after add should be {NumberOfKeys}.");

            foreach (var key in listOfKeys)
            {
                var keyFromDb = keysAfterAdd.Single(k => k == key);
                Assert.AreEqual(key, keyFromDb);
            }

            // Act: Remove
            foreach (var key in listOfKeys)
            {
                await repository.Remove(key);
            }

            // Act: Get Keys
            var keysAfterRemove = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(0, keysAfterRemove.Count, $"Number of keys after insert should be 0.");
        }
        public async Task RedisValidationCacheDataRepository_GetKeys_ShouldWork()
        {
            // Arrange
            const int NumberOfKeys = 10;
            var       listOfKeys   = Enumerable.Range(0, NumberOfKeys)
                                     .Select(i => Guid.NewGuid().ToString() + i)
                                     .ToList();

            var clientProvider = new RedisClientProvider();
            var repository     = new RedisValidationCacheDataRepository(clientProvider);

            var value = new ValidationCacheEntity
            {
                Content    = Guid.NewGuid().ToString(),
                LastUpdate = DateTime.UtcNow,
                Status     = ValidationStatus.Valid
            };

            // Act: Add
            foreach (var key in listOfKeys)
            {
                await repository.Add(key, value);
            }

            // Act: Get Keys
            var keysAfterAdd = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(NumberOfKeys, keysAfterAdd.Count, $"Number of keys after add should be {NumberOfKeys}.");

            foreach (var key in listOfKeys)
            {
                var keyFromDb = keysAfterAdd.Single(k => k == key);
                Assert.AreEqual(key, keyFromDb);
            }

            // Act: Remove
            foreach (var key in listOfKeys)
            {
                await repository.Remove(key);
            }

            // Act: Get Keys
            var keysAfterRemove = repository.Keys.ToList();

            // Assert: Get Keys
            Assert.AreEqual(0, keysAfterRemove.Count, $"Number of keys after insert should be 0.");
        }
Пример #9
0
        public void RedisKeyValuePairsRepositoryOfTweet_WithValidClientProviderInConstructor_ShouldBeInitializedCorrectly()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();

            // Act + Assert
            var repository = new RedisKeyValuePairsRepository <ITweet>(clientProvider);

            Assert.IsNotNull(repository);

            var providerField = PrivateField.GetInstanceField(
                typeof(RedisKeyValuePairsRepository <ITweet>).BaseType,
                repository,
                Constants.ClientProviderFieldName);

            Assert.AreSame(clientProvider, providerField);
        }
        public async Task RedisValidationCacheDataRepository_AddAndRemoveKeyValuePair_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisValidationCacheDataRepository(clientProvider);
            var key            = Guid.NewGuid().ToString();

            var now   = DateTime.Now;
            var value = new ValidationCacheEntity
            {
                Content    = $"{key} {now.ToLongTimeString()}",
                LastUpdate = now,
                Status     = ValidationStatus.Valid
            };

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            await repository.Add(key, value);

            await repository.SaveChangesAsync();

            // Act + Assert: Retrieve data
            var valuesFromDb = repository.GetAll(key).ToList();

            Assert.AreEqual(1, valuesFromDb.Count);

            var valueFromDb = valuesFromDb.Single();

            Assert.AreEqual(ValidationStatus.Valid, valueFromDb.Status);
            Assert.AreEqual(now.ToLongTimeString(), valueFromDb.LastUpdate.ToLongTimeString());
            Assert.AreEqual(value.Content, valueFromDb.Content);

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Пример #11
0
        public async Task RedisKeyCollectionValuePairsRepositoryOfTweet_AddAndRemoveKeyValuePair_ShouldWork()
        {
            // Arrange
            var clientProvider = new RedisClientProvider();
            var repository     = new RedisKeyCollectionValuePairsRepository <ITweet>(clientProvider);
            var key            = Guid.NewGuid().ToString();

            var now   = DateTime.Now;
            var value = new Tweet
            {
                Id       = 1,
                PostedOn = now,
                Content  = $"{key} {now.ToLongTimeString()}"
            };

            // Act: Clean-up database
            await repository.Remove(key);

            await repository.SaveChangesAsync();

            // Act: Add
            await repository.Add(key, value);

            await repository.SaveChangesAsync();

            // Act + Assert: Retrieve data
            var valuesFromDb = repository.GetAll(key).ToList();

            Assert.AreEqual(1, valuesFromDb.Count);

            var valueFromDb = valuesFromDb.Single();

            Assert.AreEqual(1, valueFromDb.Id);
            Assert.AreEqual(now.ToLongTimeString(), valueFromDb.PostedOn.ToLongTimeString());
            Assert.AreEqual(value.Content, valueFromDb.Content);

            // Act: Remove
            await repository.Remove(key);

            Assert.That(async() => await repository.SaveChangesAsync(), Is.EqualTo(0L).After(2000));

            // Assert: Remove
            Assert.AreEqual(0, repository.GetAll(key).ToList().Count, "All values in the list should be removed.");
        }
Пример #12
0
 IRedisClient CreateRedisClient()
 {
     return(RedisClientProvider.CreateRedisClient(connName));
 }
 IRedisClient CreateRedisClient()
 {
     return(RedisClientProvider.CreateRedisClient("redisconn"));
 }