/// <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; }
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); } }