A web api phoenix to support auto refresh for webApi

It will create instance of WebApi controller and invoke the ActionMethod with cached arguments. It will not work if the controller required QueryString, Headers or anything outside these action method parameters But you can override the Phoenix MethodInfo, Arguments or method Phoenix.GetTargetInstance, Phoenix.InvokeAndGetBareResult, or completely change the way the Phoenix reborn by overriding method , Phoenix.Reborn, Idealy, keep Controller thin and use proper Model binding instead of dodgy access the Request object.

상속: Flatwhite.Hot.Phoenix
예제 #1
0
        /// <summary>
        /// Create the phoenix object which can refresh the cache itself if StaleWhileRevalidate > 0
        /// </summary>
        /// <param name="invocation"></param>
        /// <param name="cacheItem"></param>
        /// <param name="request"></param>
        /// <param name="mediaTypeFormatter">The formater that was used to create the reasponse at the first invocation</param>
        /// <returns></returns>
        private void CreatePhoenix(_IInvocation invocation, WebApiCacheItem cacheItem, HttpRequestMessage request, MediaTypeFormatter mediaTypeFormatter)
        {
            var cacheInfo = new CacheInfo
            {
                CacheKey = cacheItem.Key,
                CacheStoreId = cacheItem.StoreId,
                CacheDuration = MaxAge,
                StaleWhileRevalidate = StaleWhileRevalidate,
                AutoRefresh = AutoRefresh
            };

            var phoenix = new WebApiPhoenix(invocation, cacheInfo, cacheItem, request, mediaTypeFormatter);
            if (Global.Cache.PhoenixFireCage.ContainsKey(cacheItem.Key))
            {
                Global.Cache.PhoenixFireCage[cacheItem.Key].Dispose();
            }
            Global.Cache.PhoenixFireCage[cacheItem.Key] = phoenix;
        }
예제 #2
0
        public void should_execute_the_controller_method_and_return_CacheItem(string actionMethodName, int contentLength)
        {
            // Arrange
            var currentCacheItem = new WebApiCacheItem();
            var invocation = Substitute.For<_IInvocation>();
            invocation.Arguments.Returns(new object[0]);
            invocation.Method.Returns(controllerType.GetMethod(actionMethodName, BindingFlags.Instance | BindingFlags.Public));

            var phoenix = new WebApiPhoenix(invocation, CacheInfo, currentCacheItem, new HttpRequestMessage(), new JsonMediaTypeFormatter());

            // Action
            MethodInfo dynMethod = typeof(WebApiPhoenix).GetMethod("InvokeAndGetBareResult", BindingFlags.NonPublic | BindingFlags.Instance);
            var result = dynMethod.Invoke(phoenix, new object[] { _controllerIntance });

            dynMethod = typeof(WebApiPhoenix).GetMethod("GetCacheItem", BindingFlags.NonPublic | BindingFlags.Instance);
            var cacheItem = (WebApiCacheItem)dynMethod.Invoke(phoenix, new[] { result });

            // Assert
            if (result == null)
            {
                Assert.IsTrue(actionMethodName == "Void" || actionMethodName == "VoidAsync");
            }
            else
            {
                Assert.AreEqual(contentLength, cacheItem.Content.Length);
            }
        }