public PageCache CheckPageCache(string path, XElement pageEle, bool usingNavCache = false)
        {
            int  cacheSeconds = 0;
            bool toCache      = !usingNavCache && PageOutputCache.CheckPageCache(pageEle, out cacheSeconds);

            PageCache pageCache = new PageCache()
            {
                Cache     = toCache,
                CacheTime = cacheSeconds
            };

            lock (loc)
            {
                pageCacheSettings[path] = pageCache;
            }

            return(pageCache);
        }
Exemplo n.º 2
0
        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);
        }