public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { bool usingCachedNavs = true; if (actionExecutedContext.Request.Properties.TryGetValue(NAVS_CACHED_PROP_NAME, out object temp)) { usingCachedNavs = (bool)temp; } var localPath = actionExecutedContext.Request.RequestUri.LocalPath.ToLowerInvariant(); IGXPageLevelCache pageLevelCache = IGXPageLevelCache.Get(TriggerFile); //add page level caching information, first request to the same page will always executing. 2nd request will set the cachability pageLevelCache.CheckPageCache(localPath, null, usingCachedNavs); if (Duration > 0) { ObjectCache cache = MemoryCache.Default; CacheItemPolicy policy = new CacheItemPolicy(); List <string> filePaths = new List <string>(); filePaths.Add(TriggerFile); policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(Duration); policy.ChangeMonitors.Add(new HostFileChangeMonitor(filePaths)); var path = actionExecutedContext.Request.RequestUri.LocalPath.ToLowerInvariant(); var contentValue = actionExecutedContext.Response.Content.ReadAsStringAsync().Result; cache.Set($"{MEM_CACHE_PREFIX}{path}", contentValue, policy); } base.OnActionExecuted(actionExecutedContext); }
public static IGXPageLevelCache Get(string triggerFile) { var pageLevelCache = HttpRuntime.Cache[M_PageLevelCache] as IGXPageLevelCache; if (pageLevelCache == null) { pageLevelCache = new IGXPageLevelCache(); HttpRuntime.Cache.Insert(M_PageLevelCache, pageLevelCache, new CacheDependency(triggerFile), System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);; } return(pageLevelCache); }
public override void OnActionExecuting(HttpActionContext actionContext) { var sitePath = CMSPageFactory.GetSitePath(); Settings settings = Settings.Get(new FileInfo(Path.Combine(sitePath, "settings/settings.xml"))); var path = actionContext.Request.RequestUri.LocalPath.ToLowerInvariant(); ObjectCache cache = MemoryCache.Default; var cachedContentObject = cache.Get($"{MEM_CACHE_PREFIX}{path}"); if (cachedContentObject != null) { if (cachedContentObject is string cachedContent) { var response = actionContext.Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(cachedContent); actionContext.Response = response; return; } } IGXPageLevelCache pageLevelCache = IGXPageLevelCache.Get(TriggerFile); PageCache pageCache = pageLevelCache.GetPageCacheSettings(path); UseCache = settings.GetSetting <bool>("RuntimeCache", "UseRuntimeCache") && pageCache != null && pageCache.Cache; if (!UseCache) { Duration = 0; } else { int durationSettings = pageCache.CacheTime > 0 ? pageCache.CacheTime : settings.GetSetting <int>("RuntimeCache", "ExpireTime"); Duration = durationSettings; } base.OnActionExecuting(actionContext); }
/// <summary> /// Load settings from settings file and override several parameters /// </summary> /// <param name="filterContext"></param> public override void OnActionExecuting(ActionExecutingContext filterContext) { //check trigger file, if updated, clear all cache var controller = filterContext.Controller; if (controller is CMSPageDefaultController) { //if design time, not caching CMSPageDefaultController dssController = controller as CMSPageDefaultController; if (dssController.IsDesignTime) { Duration = 0; return; } string sitePath = dssController._SitePath; Settings settings = Settings.Get(new FileInfo(Path.Combine(sitePath, "settings/settings.xml"))); TriggerFile = System.IO.Path.Combine(sitePath, settings.GetSetting <string>("RuntimeCache", "TriggerFile")); IGXPageLevelCache pageLevelCache = IGXPageLevelCache.Get(TriggerFile); //page level setting trump system settings PageCache pageCache = pageLevelCache.GetPageCacheSettings(filterContext.HttpContext.Request.Path.ToLowerInvariant()); //when pageCache is doesn't exist yet, don't do caching, since the first actual request through controller action //will set it. This means the caching kicks after 2nd request if (pageCache == null) { Duration = 0; //set duration to 0 to stop caching } else { //override cache settings //get runtimecache settings, only use 2 of them UseCache = !Reference.Reference.IsDesignTime(sitePath) && settings.GetSetting <bool>("RuntimeCache", "UseRuntimeCache") && pageCache.Cache; int durationSettings = pageCache.CacheTime > 0 ? pageCache.CacheTime : settings.GetSetting <int>("RuntimeCache", "ExpireTime"); //use default duration specified in attribute parameter if no settings is found if (durationSettings > 0) { Duration = durationSettings; } else if (durationSettings == 0) { //never expire it by set a large value (24 hours is good enough for now) Duration = (int)Math.Round(TimeSpan.FromDays(1).TotalSeconds); } else { Duration = 0; } Location = System.Web.UI.OutputCacheLocation.Server; if (UseCache && Duration != 0) { if (System.IO.File.Exists(TriggerFile)) { filterContext.HttpContext.Response.AddFileDependency(TriggerFile); } base.OnActionExecuting(filterContext); } else { Duration = 0; //set duration to 0 to stop caching } } } base.OnActionExecuting(filterContext); }