zoukankan      html  css  js  c++  java
  • 缓存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);
                }
            }
        }
    

      

  • 相关阅读:
    Spring Security
    用过sessionid防钓鱼
    request获取json
    相对于Statement,PreparedStatement的优点是什么?
    jquery中$.get()提交和$.post()提交有区别吗?
    相对于Statement,PreparedStatement的优点是什么?
    什么是Redis?
    如何解决表单提交的中文乱码问题
    execute,executeQuery,executeUpdate的区别是什么?
    根据你以往的经验简单叙述一下MYSQL的优化
  • 原文地址:https://www.cnblogs.com/CoreXin/p/5056238.html
Copyright © 2011-2022 走看看