示例#1
0
        public void Get_SimpleObject_ReturnedObjectIsIdentical(CosmosCache.InsertMode mode)
        {
            // Arrange
            var cache = CreateCache <SimpleObject>(mode);
            var value = SimpleObject.Create();

            cache.Set(new CacheItem <SimpleObject>(CacheKey, value, TimeSpan.FromSeconds(5)));

            // Act
            var result = cache.Get(CacheKey);

            // Assert
            Assert.False(object.ReferenceEquals(result, value));
            Assert.Equal(result, value);
        }
示例#2
0
        private CosmosCache <T> CreateCache <T>(CosmosCache.InsertMode mode, string cacheName = null)
        {
            switch (mode)
            {
            case CosmosCache.InsertMode.Document:
                return((CosmosCache <T>)docDirectCacheFactory.Create <T>(cacheName ?? typeof(T).Name));

            case CosmosCache.InsertMode.JSON:
                return(string.IsNullOrEmpty(cacheName)
                        ? (CosmosCache <T>)cacheFactory.CreateDefault <T>()
                        : (CosmosCache <T>)cacheFactory.Create <T>(cacheName));

            default:
                throw new NotImplementedException($"Cache creator for InsertMode='{mode}' is not implemented!");
            }
        }
示例#3
0
        public async Task GetAsync_ReturnMultipleItems_WhenMultipleCacheKeysRequestedWithDefinedInsertMode(CosmosCache.InsertMode insertMode)
        {
            // Arrange
            var cosmosCache = CreateCache <SimpleObject>(insertMode);

            var keys = new List <string>();

            Enumerable.Range(0, 10).ToList().ForEach(i =>
            {
                var key = CacheKey + i;
                keys.Add(key);
                var cacheValue = new SimpleObject {
                    Foo = key, Value = i
                };
                cosmosCache.Set(new CacheItem <SimpleObject>(key, cacheValue, TimeSpan.FromSeconds(5)));
            });

            // Act
            var result = (await cosmosCache.GetAsync(keys)).ToArray();

            // Assert
            Assert.NotNull(result);
            Assert.True(result.Select(x => x.Key).All(x => keys.Contains(x)));
            Assert.True(result.Select(x => x.Value).All(x => keys.Contains(x.Foo)));
        }