public T Get <T>(string key, Func <T> func, TimeSpan time, CacheExpiryType cacheExpiryType) { key = InternalCachePrefix + key; object o = null; if (time > TimeSpan.Zero) { o = _cache[key]; } if (o != null) { return(o.To <T>()); } o = func.Invoke(); if (o != null) { if (time > TimeSpan.Zero) { var absoluteExpiration = cacheExpiryType == CacheExpiryType.Absolute ? DateTime.UtcNow.Add(time) : Cache.NoAbsoluteExpiration; var slidingExpiration = cacheExpiryType == CacheExpiryType.Sliding ? (time) : Cache.NoSlidingExpiration; _cache.Add(key, o, absoluteExpiration, slidingExpiration, CacheItemPriority.AboveNormal); } return(o.To <T>()); } return((T)(object)null); }
public bool Handle(RequestContext context) { var path = context.HttpContext.Request.Url.AbsolutePath; var extension = Path.GetExtension(path); if (_webExtensions.Get.Any(x => x == extension)) { _errorHandler.HandleError(context, 404, new HttpException(404, string.Format("Cannot find {0}", context.RouteData.Values["data"]))); return(true); } if (!string.IsNullOrWhiteSpace(extension)) { var controller = _controllerManager.ControllerFactory.CreateController(context, ControllerName) as ErrorController; controller.ControllerContext = new ControllerContext(context, controller) { RouteData = context.RouteData }; var routeValueDictionary = new RouteValueDictionary(); routeValueDictionary["controller"] = ControllerName; routeValueDictionary["action"] = ActionName; routeValueDictionary["url"] = context.HttpContext.Request.Url; controller.RouteData.Values.Merge(routeValueDictionary); var result = controller.GetHtmlHelper().Action(ActionName, ControllerName, routeValueDictionary).ToString(); _cacheWrapper.Add(GetMissingFileCacheKey(context.HttpContext.Request), result, DateTime.UtcNow.Add(TimeSpan.FromMinutes(5)), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal); context.HttpContext.Response.Clear(); context.HttpContext.Response.StatusCode = 404; context.HttpContext.Response.TrySkipIisCustomErrors = true; context.HttpContext.Response.Write(result); context.HttpContext.ApplicationInstance.CompleteRequest(); return(true); } return(false); }
public bool Handle(RequestContext context) { string path = context.HttpContext.Request.Url.AbsolutePath; string extension = Path.GetExtension(path); if (_siteSettings.WebExtensionsToRoute.Any(x => x == extension)) { _errorHandler.HandleError(context, 404, new HttpException(404, string.Format("Cannot find {0}", context.RouteData.Values["data"]))); return(true); } if (!string.IsNullOrWhiteSpace(extension)) { _cacheWrapper.Add(GetMissingFileCacheKey(context.HttpContext.Request), new object(), DateTime.UtcNow.Add(TimeSpan.FromMinutes(5)), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal); context.HttpContext.Response.Clear(); context.HttpContext.Response.StatusCode = 404; context.HttpContext.Response.TrySkipIisCustomErrors = true; context.HttpContext.ApplicationInstance.CompleteRequest(); return(true); } return(false); }