Flatwhite.WebApi.OutputCacheAttribute.OnActionExecutingAsync C# (CSharp) Method

OnActionExecutingAsync() public method

Check CacheControl request, get CacheItem, build response and return if cache available
public OnActionExecutingAsync ( System.Web.Http.Controllers.HttpActionContext actionContext, CancellationToken cancellationToken ) : Task
actionContext System.Web.Http.Controllers.HttpActionContext
cancellationToken System.Threading.CancellationToken
return Task
        public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
        {
            /*
            cache-request-directive =
           "no-cache"                          ; Section 14.9.1
         | "no-store"                          ; Section 14.9.2
         | "max-age" "=" delta-seconds         ; Section 14.9.3, 14.9.4
         | "max-stale" [ "=" delta-seconds ]   ; Section 14.9.3
         | "min-fresh" "=" delta-seconds       ; Section 14.9.3
         | "no-transform"                      ; Section 14.9.5
         | "only-if-cached"                    ; Section 14.9.4
         | cache-extension                     ; Section 14.9.6    
            */
            var request = actionContext.Request;
            var cacheControl = request.Headers.CacheControl;
            if (ShouldIgnoreCache(cacheControl, actionContext.Request))
            {
                return;
            }

            var scope = request.GetDependencyScope();
            var invocation = GetInvocation(actionContext);
            var context = GetInvocationContext(actionContext);
            var strategy = GetCacheStrategy(scope, invocation, context);
            
            var cacheKey = strategy.CacheKeyProvider.GetCacheKey(invocation, context);
            var hashedKey = HashCacheKey(cacheKey);
            var cacheStore = strategy.GetAsyncCacheStore(invocation, context);
            var storedKey = $"fw-{cacheStore.StoreId}-{hashedKey}";
            var builder = GetCacheResponseBuilder(scope);

            request.Properties[Global.__flatwhite_outputcache_store] = cacheStore;
            request.Properties[Global.__flatwhite_outputcache_key] = storedKey;
            request.Properties[Global.__flatwhite_outputcache_strategy] = strategy;
            request.Properties[WebApiExtensions.__webApi_outputcache_response_builder] = builder;

            var cacheItem = await cacheStore.GetAsync(storedKey).ConfigureAwait(false) as WebApiCacheItem;
            if (cacheItem != null && cacheItem.IsStale())
            {
                if (!Global.Cache.PhoenixFireCage.ContainsKey(storedKey))
                {
                    CreatePhoenix(invocation, cacheItem, actionContext.Request);
                }
                RefreshCache(storedKey);
            }

            actionContext.Response = builder.GetResponse(cacheControl, cacheItem, actionContext.Request);
        }

Usage Example

コード例 #1
0
        public async Task Should_create_phoenix_and_try_refresh_cache_when_cache_item_is_stale()
        {
            // Arrange
            var store = Substitute.For <IAsyncCacheStore>();

            store.StoreId.Returns(1000);
            var objCacheItem = new WebApiCacheItem
            {
                MaxAge = 5,
                StaleWhileRevalidate = 5,
                StoreId     = 1000,
                CreatedTime = DateTime.UtcNow.AddSeconds(-5).AddMilliseconds(-1),
            };

            store.GetAsync(Arg.Any <string>()).Returns(c =>
            {
                objCacheItem.Key = c.Arg <string>();
                return(Task.FromResult((object)objCacheItem));
            });

            Global.CacheStoreProvider.RegisterAsyncStore(store);
            var att = new Flatwhite.WebApi.OutputCacheAttribute {
                MaxAge = 5, CacheStoreId = 1000, StaleWhileRevalidate = 5
            };

            // Action
            await att.OnActionExecutingAsync(_actionContext, CancellationToken.None);

            // Assert
            Assert.IsTrue(Global.Cache.PhoenixFireCage.ContainsKey(objCacheItem.Key));
        }
All Usage Examples Of Flatwhite.WebApi.OutputCacheAttribute::OnActionExecutingAsync