日期:2014-05-17  浏览次数:20917 次

Asp.net 服务端缓存引发的一点思考

我们知道asp.net要经历以下事件

BeginRequest
AuthenticateRequest
PostAuthenticateRequest
AuthorizeRequest
PostAuthorizeRequest
ResolveRequestCache
PostResolveRequestCache
PostMapRequestHandler
AcquireRequestState
PostAcquireRequestState
PreRequestHandlerExecute
....IHttpHandler 类的 ProcessRequest 方法,真正处理请求的地方时一个比较耗时的处理
PostRequestHandlerExecute
eleaseRequestState
PostReleaseRequestState 事件。
UpdateRequestCache
PostUpdateRequestCache
EndRequest

  而在服务端我们缓存经常会用到OutputCache,之所以能用它只要是因为OutputCacheModule中有如下代码:

  void IHttpModule.Init(HttpApplication app)
    {
        if (RuntimeConfig.GetAppConfig().OutputCache.EnableOutputCache)
        {
            app.ResolveRequestCache += new EventHandler(this.OnEnter);
            app.UpdateRequestCache += new EventHandler(this.OnLeave);
        }
    }
同样只要看过OutputCacheModule源码的人都知道这里面面的代码比较难懂,为了提高性能,我们能否把读取缓存的时间往前推推到BeginRequest,一旦有缓存这个时候我们就返回数据结束输出流。那么什么时候设置缓存了?我们可以选着在ReleaseRequestState事件中处理,在这个处理中我们需要截获输出流的内容。

在本例中只是提出这种缓存的思想,在项目中运用需要注意的事情还很多。

首先我们需要实现缓存机制,这里我们用字典来存储数据永不过期。

    public class Cache
    {
        static ConcurrentDictionary<string, string> dict = new ConcurrentDictionary<string, string>();
        public static void Add(string key, string value)
        {
            if (dict.ContainsKey(key))
            {
                 dict[key] = value;
                 
            }
            else
            {
                 dict.TryAdd(key, value);
            }
        }
        public static string Get(string key)
        {
            string result = string.Empty;
            dict.TryGetValue(key, out result);
            return result;
        }
    }

代码很简单吧,同样需要截获输出流的代码


    public class PageFilter : Stream
    {
        Stream responseStream;
        long position;
        StringBuilder responseHtml;

        public PageFilter(Stream inputStream)
        {
            responseStream = inputStream;
            responseHtml = new StringBuilder();
        }

        #region Filter overrides
        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        public override void Close()
        {
            responseStream.Close();
        }

        public override void Flush()
        {
            responseStream.Flush();
        }

        public override long Length
        {
            get { return 0; }
        }

        public override long Position
        {
            get { return position; }
            set { position = value; }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return responseStream.Seek(offset, origin);
        }

        public override void SetLength(long length)
        {
            responseStream.SetLength(length);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return responseStream.Read(buffer, offset, count);
        }
        #endregion

        #region Dirty work
        public override void Write(byte[] buffer, int offset, int count)
        {
            HttpResponse response = HttpContext.Current.Response;
            string charset = response.Charset ?? "utf-8";
            string finalHtml = Encoding.GetEncoding(charset).GetString(buffer, offset, count);
          

            int index = finalHtml.IndexOf("</html>");
            if (index<1)
            {
                respo