示例#1
0
        public void Add_Two_Test_Call_remove()
        {
            var called = false;

            _metadataProvider.Expect(x => x.GetEarliestAccessedItem("d")).Return(new CacheItemMetadata
            {
                Domain = "d",
                Size   = 5
            });
            _mockRepository.ReplayAll();

            var manager = new CacheStoreQuotaManager(_metadataProvider, i => called = true, _settings);

            manager.ItemAdded(new CacheItemMetadata {
                Domain = "d", Size = 5
            });
            manager.ItemAdded(new CacheItemMetadata {
                Domain = "d", Size = 6
            });
            Thread.Sleep(1000);

            Assert.AreEqual(true, called);
            Assert.AreEqual(6, manager.GrandTotal);
            Assert.AreEqual(1, manager.StorageMetadata.Count);
        }
示例#2
0
        public void Add_Test_GrandTotal_remove()
        {
            bool called = false;

            _metadataProvider.Expect(x => x.GetEarliestAccessedItem()).Return(new CacheItemMetadata()
            {
                Domain = "c",
                Size   = 3
            });
            _mockRepository.ReplayAll();

            var manager = new CacheStoreQuotaManager(_metadataProvider, (i) => called = true, _settings);

            manager.ItemAdded(new CacheItemMetadata()
            {
                Domain = "a", Size = 9
            });
            manager.ItemAdded(new CacheItemMetadata()
            {
                Domain = "b", Size = 9
            });
            manager.ItemAdded(new CacheItemMetadata()
            {
                Domain = "c", Size = 3
            });
            Thread.Sleep(1000);

            Assert.AreEqual(true, called);
            Assert.AreEqual(18, manager.GrandTotal);
            Assert.AreEqual(3, manager.StorageMetadata.Count);
        }
示例#3
0
        public void Add_Two_Test_NotRemove()
        {
            var called = false;

            _mockRepository.ReplayAll();
            var manager = new CacheStoreQuotaManager(_metadataProvider, i => called = true, _settings);

            manager.ItemAdded(new CacheItemMetadata {
                Domain = "d", Size = 5
            });
            manager.ItemAdded(new CacheItemMetadata {
                Domain = "d", Size = 5
            });
            Assert.AreEqual(false, called);
        }
示例#4
0
        public void Add_Test()
        {
            _mockRepository.ReplayAll();
            var manager = new CacheStoreQuotaManager(_metadataProvider, null, _settings);

            manager.ItemAdded(new CacheItemMetadata {
                Domain = "d", Size = 5
            });
            Assert.AreEqual(5, manager.GrandTotal);
            Assert.AreEqual(1, manager.StorageMetadata.Count);
        }
示例#5
0
        public void AddOrUpdate(CacheKey key, HttpResponseMessage response)
        {
            var fileName = key.EnsureFolderAndGetFileName(_dataRoot);

            if (File.Exists(fileName))
            {
                TraceWriter.WriteLine("Must remove file", TraceLevel.Verbose);
                TryRemove(key);
            }

            var ms = new MemoryStream();

            _serializer.SerializeAsync(TaskHelpers.FromResult(response), ms).Wait();
            ms.Position = 0;
            using (var fs = GetFile(fileName, FileMode.Create))
            {
                TraceWriter.WriteLine("Before serialise", TraceLevel.Verbose);
                ms.CopyTo(fs);
                TraceWriter.WriteLine("After serialise", TraceLevel.Verbose);
            }


            var info = new FileInfo(fileName);

            // Update database
            _database.Cache
            .Insert(new CacheItem
            {
                Domain       = key.Domain,
                Hash         = Convert.ToBase64String(key.Hash),
                LastAccessed = DateTime.Now,
                LastUpdated  = (response.Content != null) && response.Content.Headers.LastModified.HasValue
                        ? response.Content.Headers.LastModified.Value.UtcDateTime
                        : DateTime.Now
                ,
                Size = info.Length
            });
            TraceWriter.WriteLine("After db update", TraceLevel.Verbose);


            // tell quota manager
            _quotaManager.ItemAdded(new CacheItemMetadata
            {
                Domain       = key.Domain,
                Key          = key.Hash,
                LastAccessed = DateTime.Now,
                Size         = info.Length
            });
        }