Пример #1
0
        public async Task <ActionResult> News(bool?refresh)
        {
            var blob = _blobService.GetBlobReference("content", NewsBlobNameHtml);
            var html = await _cacheAccessor.GetAsync <string>(NewsBlobNameHtml);

            var etag = await _cacheAccessor.GetAsync <string>(NewsBlobETag);

            if (refresh.GetValueOrDefault())
            {
                await _cacheAccessor.DeleteAsync(ArchiveBlobNameHtml);
            }

            if ((html == null && await blob.ExistsAsync()) || refresh.GetValueOrDefault())
            {
                await blob.FetchAttributesAsync();

                html = await blob.DownloadTextAsync();

                etag = CleanETag(blob.Properties.ETag);

                await _cacheAccessor.SetAsync(NewsBlobNameHtml, html, TimeSpan.FromHours(1));

                await _cacheAccessor.SetAsync(NewsBlobETag, etag, TimeSpan.FromHours(1));
            }

            Response.Cookies.Set(new HttpCookie(NewsBlobETag, etag)
            {
                Expires = DateTime.MaxValue, SameSite = SameSiteMode.Strict
            });

            return(View(new MvcHtmlString(html)));
        }
        public async Task CacheExtension_SetAndGetMethods_ReturnActualValuesCorrectly()
        {
            // ARRANGE
            var key = "Employee25";
            await _cacheThatsConnected.SetAsync(key, new Employee(25, "Clayton Gragg"));

            // ACT
            var value = await _cacheThatsConnected.GetAsync <Employee>(key); // should return null, as the connection

            bool deletionResult = await _cacheThatsConnected.DeleteAsync(key);

            Assert.That(value.Id, Is.EqualTo(25));
            Assert.IsTrue(deletionResult);
        }
Пример #3
0
        public async Task <IEnumerable <Product> > GetAll()
        {
            var cacheKey = $"{nameof(Product)}:{nameof(GetAll)}";
            var models   = await _cacheAccessor.GetAsync <IEnumerable <Product> >(cacheKey);

            if (models == null)
            {
                using (var context = _contextFactory.CreateInstance())
                {
                    models = await context.Products
                             .ToListAsync();
                }

                await _cacheAccessor.SetAsync(models, cacheKey, TimeSpan.FromSeconds(CACHE_SECONDS));
            }

            return(models);
        }
        public async Task <ActionResult> SitemapIndex(bool?refresh)
        {
            if (refresh.GetValueOrDefault())
            {
                await CleanSitemapCache();
            }

            var cacheName    = $"{_cacheTag}_index";
            var responseCopy = await _cacheAccessor.GetAsync <string>(cacheName);

            if (responseCopy.IsNullOrEmpty())
            {
                responseCopy = await GetSitemapIndex();

                await _cacheAccessor.SetAsync(cacheName, responseCopy, TimeSpan.FromDays(_cacheDays));
            }

            return(this.Content(responseCopy, MediaTypeNames.Text.Xml, Encoding.UTF8));
        }
        private async Task <string> GetHtmlBlob(string name)
        {
            var blob = _blobService.GetBlobReference("content", name);
            var html = await _cacheAccessor.GetAsync <string>(name);

            if (html == null && await blob.ExistsAsync())
            {
                html = await blob.DownloadTextAsync();

                await _cacheAccessor.SetAsync(name, html, TimeSpan.FromHours(1));
            }
            return(html);
        }