Example #1
0
        /// <summary>
        /// Occurs when ASP.NET completes an authorization event to let the caching modules
        /// serve requests from the cache, bypassing execution of the event handler (for
        /// example, a page or an XML Web service).
        /// </summary>
        /// <param name="source"></param>
        /// <param name="ea"></param>
        private void ResolveRequestCache(object o, EventArgs args)
        {
            HttpApplication application  = (HttpApplication)o;
            HttpContext     context      = application.Context;
            string          key          = context.Request.FilePath.ToLower();
            PageSettings    pageSettings = _reader.GetPageSettings(key);

            if (pageSettings == null)
            {
                return;
            }
            if (this._cache == null)
            {
                return;
            }
            if (!pageSettings.CachingEnabled)
            {
                return;
            }
            if (!pageSettings.Get && context.Request.HttpMethod == "GET")
            {
                return;
            }
            else if (!pageSettings.Post && context.Request.HttpMethod == "POST")
            {
                return;
            }

            try
            {
                string varyingKey = key + this.CreateVaryingKey(pageSettings, context);
                NItem  item       = this._cache.Get(varyingKey, pageSettings) as NItem;

                if (item == null)
                {
                    NFilter nFilter = new NFilter(context.Response.Filter);
                    context.Response.Filter = nFilter;

                    context.Items[_filterKey] = nFilter;
                    return;
                }

                context.Response.ClearContent();
                context.Response.BinaryWrite(item.Buffer);

                application.CompleteRequest();
            }
            catch (Exception e)
            {
                RaiseException(e);
            }
        }
Example #2
0
        /// <summary>
        /// Occurs when ASP.NET finishes executing an event handler in order to let caching
        /// modules store responses that will be used to serve subsequent requests from
        /// the cache.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="args"></param>
        private void UpdateRequestCache(object o, EventArgs args)
        {
            HttpApplication application  = (HttpApplication)o;
            HttpContext     context      = application.Context;
            string          key          = context.Request.FilePath.ToLower();
            PageSettings    pageSettings = _reader.GetPageSettings(key);

            if (pageSettings == null)
            {
                return;
            }
            if (this._cache == null)
            {
                return;
            }
            if (!pageSettings.CachingEnabled)
            {
                return;
            }
            if (!pageSettings.Get && context.Request.HttpMethod == "GET")
            {
                return;
            }
            else if (!pageSettings.Post && context.Request.HttpMethod == "POST")
            {
                return;
            }

            try
            {
                if (context.Response.StatusCode == 200)
                {
                    NFilter nFilter = context.Items[_filterKey] as NFilter;
                    if (nFilter != null)
                    {
                        context.Items.Remove(_filterKey);

                        NItem item = new NItem(PageHash(key), nFilter.ToArray());
                        this._cache.Insert(key + TagUtil.CreateTaggedCacheItem(this.CreateVaryingKey(pageSettings, context)), item, null, DateTime.Now.AddSeconds(pageSettings.ExpirationTime), Alachisoft.NCache.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }
            }
            catch (Exception e)
            {
                RaiseException(e);
            }
        }
Example #3
0
        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="application">An <see cref="System.Web.HttpApplication"/> object that provides
        /// references to the intrinsic server objects (for example,
        /// Request, Response, Session, and Server) used to service HTTP requests.
        /// </param>
        void IHttpModule.Init(HttpApplication application)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            this._onResolveRequestCache = new EventHandler(ResolveRequestCache);
            this._onUpdaterequestCache  = new EventHandler(UpdateRequestCache);
            this._onDispose             = new EventHandler(Dispose);

            application.ResolveRequestCache += this._onResolveRequestCache;
            application.UpdateRequestCache  += this._onUpdaterequestCache;
            application.Disposed            += this._onDispose;

            if (_reader == null)
            {
                lock (s_mutex)
                {
                    if (_reader == null)
                    {
                        NItem.RegisterTypeWithCompactFramework();
                        _reader   = new OutPutCacheConfigReader();
                        _settings = _reader.LoadCacheSettings();
                        _reader.LoadPageSettings();
                    }
                }
            }

            try
            {
                if (IsNullOrEmpty(_settings.CacheName))
                {
                    return;
                }

                if (_settings.EnableLogs || _settings.EnableDetailedLogs)
                {
                    _ncacheLog = new NCacheLogger();
                    _ncacheLog.Initialize(LoggerNames.OutputCache, _settings.CacheName);

                    if (_settings.EnableDetailedLogs)
                    {
                        NCacheLog.SetLevel("all");
                    }
                    else
                    {
                        if (_settings.EnableLogs)
                        {
                            NCacheLog.SetLevel("info");
                        }
                    }
                }

                lock (s_mutex)
                {
                    this._cache = CacheContainer.GetCacheInstance(_settings);
                }

                if (NCacheLog != null)
                {
                    NCacheLog.Info("NOutputCache initialized");
                }
            }
            catch (Exception e)
            {
                RaiseException(e);
            }
        }