缓存ViewState减少网络传输

在维护 asp.net webform系统时,某些系统将控件及页面数据都存储在viewstate中,导致在频宽不够时,影响页面加载速度,此时可将viewstate 存储在服务端,减少网络传输。

 

重写 LoadPageStateFromPersistenceMedium SavePageStateToPersistenceMedium 即可,但是在实际使用中,如用户并发量太大不建议这样使用,会影响整个系统的运行状况,可有选择的选择某些重要的页面集成基类,处理viewstate 。

 

public class ViewStateCache
    {
        public string filename { set; get; }
        public string Key { set; get; }
        public string Value { set; get; }

        public static Queue<ViewStateCache> ViewStateQueue = new Queue<ViewStateCache>();

        public static void RegisterViewStateQueue()
        {   
            ThreadPool.QueueUserWorkItem(o =>
            {
                while (true)
                {
                    try
                    {
                        if (ViewStateQueue.Count > 0)
                        {
                            ViewStateCache vcache = ViewStateQueue.Dequeue();
                            if (vcache != null)
                            {
                                File.WriteAllText(vcache.filename, vcache.Value);
                            }
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Witer(ex.ToString());
                    }
                }
            });
        }
    }

    

    public class CachePageModel : System.Web.UI.Page
    {
        protected override object LoadPageStateFromPersistenceMedium()
        {
            if (LocalBaseInfoService.IsPriorityCacheViewState())
            {
                string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;


                var stateObject = Cache[viewStateID];
                if (stateObject != null)
                {
                    RedisHelper.Hash_SetExpire(viewStateID, DateTime.Now.AddSeconds(5));
                    Cache.Remove(viewStateID);
                    return stateObject;
                }

                string stateStr = "";

                string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);
                stateStr = File.ReadAllText(fn);

                if (!string.IsNullOrEmpty(stateStr))
                {
                    return new ObjectStateFormatter().Deserialize(stateStr);
                }
                else
                {
                    return base.LoadPageStateFromPersistenceMedium();
                }

            }
            else
            {
                return base.LoadPageStateFromPersistenceMedium();
            }
        }

        protected override void SavePageStateToPersistenceMedium(object state)
        {
            if (LocalBaseInfoService.IsPriorityCacheViewState())
            {
                string viewStateID = "viewstate" + Request.Url.AbsolutePath.ToString().Replace("\\", "-").Replace("/", "-") + Guid.NewGuid().ToString();

                Cache.Insert(viewStateID, state);
                string value = new ObjectStateFormatter().Serialize(state);

                string fn = Path.Combine(this.Request.PhysicalApplicationPath, @"App_Data/ViewState/" + viewStateID);

                ViewStateCache.ViewStateQueue.Enqueue(new ViewStateCache() { Key = viewStateID, Value = value, filename = fn });

                base.SavePageStateToPersistenceMedium(viewStateID);

            }
            else
            {
                base.SavePageStateToPersistenceMedium(state);
            }
        }
    }

  

你可能感兴趣的:(缓存ViewState减少网络传输)