linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > asp.net >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·NetBPM工作流的一个示例:请假
·Office Web Components(OWC)绘
·asp.net正则表达式语法
·asp.net 2.0 ajax中使用PopupC
·Ado.Net读取Excel常见问题总结
·数据源为空时如何让GridView显
·如何让UpdatePanel支持文件上传
·C#.Net的常见面试试题和参考答
·asp.net ajax客户端编程+jquer
·Brettle.Web.NeatUpload.dll支
·ASP.NET使用Cookie
·ASP.NET DEMO 15: 同时支持行单
·如何使IE的后退按钮无效
·如何在ASP.NET中用OWC绘制图表
·asp.net:正确判断当前用户角色
最新文章
·Ajax Control Toolkit Animati
·讨论一下类似BlogEngine内一次
·使用CSS+SiteMap+UserControl+
·Asp.net中多彩下拉框的实现
·浅谈ASP.NET的Postback
·分清ASP.NET AJAX中的Extender
·Tip:在使用AjaxControlTookit
·有关注册DataItem的一些可能被
·IIRF(Ionic's Isapi Rewrite
·asp.net 客户端回调功能的实现
·关于控件部分的看法--读Progra
·为什么在vista上做开发
·如何封装JS和CSS文件为服务器端
·岂今我看过的最强的排序算法
·设计模式学习笔记之单件模式
Google
 
讨论一下类似BlogEngine内一次性加载所有Post是否可取
[ 作者:  加入时间:2007-12-28 12:27:22  来自:Linux联盟收集整理 ]
试验了一下给BlogEngine生成10000个post 程序第一次运行时等待的时间让我想自杀(白屏了近2分钟) DxYLinux联盟
  看了一下代码,发现BlogEngine在第一次运行时候加载所有Post(从数据库)到List内,类似(Early initialization) DxYLinux联盟
  当新添加post时,给数据库(xml/database)内加入该post同时给List内追加该post DxYLinux联盟
  删除一个post时候从数据库(xml/database)内删除并从List内remove该post DxYLinux联盟
   1 DxYLinux联盟
   2private string _Content; DxYLinux联盟
   3/**//// <summary> DxYLinux联盟
   4/// Gets or sets the Content or the post. DxYLinux联盟
   5/// </summary> DxYLinux联盟
   6public string Content DxYLinux联盟
   7{ DxYLinux联盟
   8 get DxYLinux联盟
   9 { DxYLinux联盟
   10 if ( _Content == null ) DxYLinux联盟
   11 { DxYLinux联盟
   12 _Content = LoadPostContent( this.Id ); DxYLinux联盟
   13 } DxYLinux联盟
   14 return _Content; DxYLinux联盟
   15 } DxYLinux联盟
   16 set DxYLinux联盟
   17 { DxYLinux联盟
   18 if ( _Content != value ) DxYLinux联盟
   19 MarkDirty( "Content" ); DxYLinux联盟
   20 _Content = value; DxYLinux联盟
   21 } DxYLinux联盟
   22} DxYLinux联盟
   23private string LoadPostContent(Guid id) DxYLinux联盟
   24{ DxYLinux联盟
   25 string content = null; DxYLinux联盟
   26 DxYLinux联盟
   27 string key = string.Format("Be:Content:{0}",id); DxYLinux联盟
   28 DxYLinux联盟
   29 // if there is no content cached DxYLinux联盟
   30 object obj = HttpContext.Current.Cache.Get(key); DxYLinux联盟
   31 if(obj == null) DxYLinux联盟
   32 { DxYLinux联盟
   33 // load the post's content from provider here DxYLinux联盟
   34 content = BlogService.LoadPostContent( id ); DxYLinux联盟
   35 DxYLinux联盟
   36 HttpContext.Current.Cache.Insert(key, content, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero); DxYLinux联盟
   37 DxYLinux联盟
   38 // if use xml store the data DxYLinux联盟
   39 // 更丑陋点的 new CacheDependency(_Folder + "posts\\" + id.ToString() + ".xml") in the Cache.Insert Method DxYLinux联盟
   40 } DxYLinux联盟
   41 DxYLinux联盟
   42} DxYLinux联盟
   43 DxYLinux联盟
   44private static object _SyncRoot = new object(); DxYLinux联盟
   45private static List<Post> _Posts; DxYLinux联盟
   46/**//// <summary> DxYLinux联盟
   47/// A sorted collection of all posts in the blog. DxYLinux联盟
   48/// Sorted by date. DxYLinux联盟
   49/// </summary> DxYLinux联盟
   50public static List<Post> Posts DxYLinux联盟
   51{ DxYLinux联盟
   52 get DxYLinux联盟
   53 { DxYLinux联盟
   54 lock (_SyncRoot) DxYLinux联盟
   55 { DxYLinux联盟
   56 if (_Posts == null) DxYLinux联盟
   57 { DxYLinux联盟
   58 //in provider the 'FillPosts' method' dose not return the 'real' content' per post; DxYLinux联盟
   59 DxYLinux联盟
   60 _Posts = BlogService.FillPosts( ); DxYLinux联盟
   61 } DxYLinux联盟
   62 return _Posts; DxYLinux联盟
   63 } DxYLinux联盟
   64 } DxYLinux联盟
   65} DxYLinux联盟
   66 DxYLinux联盟
   67in XmlBlogProvider DxYLinux联盟
   68 DxYLinux联盟
   69/**//// <summary> DxYLinux联盟
   70/// Retrieves a post based on the specified Id. DxYLinux联盟
   71/// </summary> DxYLinux联盟
   72public override Post SelectPost(Guid id) DxYLinux联盟
   73{ DxYLinux联盟
   74 string fileName = _Folder + "posts\\" + id.ToString() + ".xml"; DxYLinux联盟
   75 Post post = new Post(); DxYLinux联盟
   76 XmlDocument doc = new XmlDocument(); DxYLinux联盟
   77 doc.Load(fileName); DxYLinux联盟
   78 DxYLinux联盟
   79 post.Title = doc.SelectSingleNode("post/title").InnerText; DxYLinux联盟
   80 post.Description = doc.SelectSingleNode("post/description").InnerText; DxYLinux联盟
   81 DxYLinux联盟
   82 post.Content = null; // dose not return the 'real' content' DxYLinux联盟
   83 DxYLinux联盟
   84 post.DateCreated = DateTime.Parse(doc.SelectSingleNode("post/pubDate").InnerText); DxYLinux联盟
   85 post.DateModified = DateTime.Parse(doc.SelectSingleNode("post/lastModified").InnerText); DxYLinux联盟
   86 DxYLinux联盟
   87 // setting other filed DxYLinux联盟
   88 DxYLinux联盟
   89 return post; DxYLinux联盟
   90} DxYLinux联盟
   91Post class in Business object layer DxYLinux联盟
   92 DxYLinux联盟
   93private string _Content; DxYLinux联盟
   94/**//// <summary> DxYLinux联盟
   95/// Gets or sets the Content or the post. DxYLinux联盟
   96/// </summary> DxYLinux联盟
   97public string Content DxYLinux联盟
   98{ DxYLinux联盟
   99 get DxYLinux联盟
  100 { DxYLinux联盟
  101 if ( _Content == null ) DxYLinux联盟
  102 { DxYLinux联盟
  103 _Content = LoadPostContent( this.Id ); DxYLinux联盟
  104 } DxYLinux联盟
  105 return _Content; DxYLinux联盟
  106 } DxYLinux联盟
  107 set DxYLinux联盟
  108 { DxYLinux联盟
  109 if ( _Content != value ) DxYLinux联盟
  110 MarkDirty( "Content" ); DxYLinux联盟
  111 _Content = value; DxYLinux联盟
  112 } DxYLinux联盟
  113} DxYLinux联盟
  114private string LoadPostContent(Guid id) DxYLinux联盟
  115{ DxYLinux联盟
  116 string key = string.Format("Be:Content:{0}",id); DxYLinux联盟
  117 string content = null; DxYLinux联盟
  118 // if there is no content cached by id DxYLinux联盟
  119 object obj = HttpContext.Current.Cache.Get(key); DxYLinux联盟
  120 if(obj == null) DxYLinux联盟
  121 { DxYLinux联盟
  122 // load the post's content from provider here DxYLinux联盟
  123 content = BlogService.LoadPostContent( id ); DxYLinux联盟
  124 DxYLinux联盟
  125 HttpContext.Current.Cache.Insert(key, content, null, DateTime.Now.AddMinutes(2), TimeSpan.Zero); DxYLinux联盟
  126 DxYLinux联盟
  127 // if use xml store the data DxYLinux联盟
  128 // we can use a CacheDependency like new CacheDependency(_Folder + "posts\\" + id.ToString() + ".xml") in the Cache.Insert Method DxYLinux联盟
  129 DxYLinux联盟
  130 return content; DxYLinux联盟
  131 } DxYLinux联盟
  132 return (strong)obj; DxYLinux联盟
  133 DxYLinux联盟
  134} DxYLinux联盟
  135 DxYLinux联盟
  136private static object _SyncRoot = new object(); DxYLinux联盟
  137private static List<Post> _Posts; DxYLinux联盟
  138/**//// <summary> DxYLinux联盟
  139/// A sorted collection of all posts in the blog. DxYLinux联盟
  140/// Sorted by date. DxYLinux联盟
  141/// </summary> DxYLinux联盟
  142public static List<Post> Posts DxYLinux联盟
  143{ DxYLinux联盟
  144 get DxYLinux联盟
  145 { DxYLinux联盟
  146 lock (_SyncRoot) DxYLinux联盟
  147 { DxYLinux联盟
  148 if (_Posts == null) DxYLinux联盟
  149 { DxYLinux联盟
  150 //in provider the 'FillPosts' method' dose not return the 'real' content' per post; DxYLinux联盟
  151 DxYLinux联盟
  152 _Posts = BlogService.FillPosts( ); DxYLinux联盟
  153 } DxYLinux联盟
  154 return _Posts; DxYLinux联盟
  155 } DxYLinux联盟
  156 } DxYLinux联盟
  157} DxYLinux联盟
  158 DxYLinux联盟
  159in XmlBlogProvider DxYLinux联盟
  160 DxYLinux联盟
  161/**//// <summary> DxYLinux联盟
  162/// Retrieves a post based on the specified Id. DxYLinux联盟
  163/// </summary> DxYLinux联盟
  164public override Post SelectPost(Guid id) DxYLinux联盟
  165{ DxYLinux联盟
  166 string fileName = _Folder + "posts\\" + id.ToString() + ".xml"; DxYLinux联盟
  167 Post post = new Post(); DxYLinux联盟
  168 XmlDocument doc = new XmlDocument(); DxYLinux联盟
  169 doc.Load(fileName); DxYLinux联盟
  170 DxYLinux联盟
  171 post.Title = doc.SelectSingleNode("post/title").InnerText; DxYLinux联盟
  172 post.Description = doc.SelectSingleNode("post/description").InnerText; DxYLinux联盟
  173 DxYLinux联盟
  174 post.Content = null; // dose not return the 'real' content' DxYLinux联盟
  175 DxYLinux联盟
  176 post.DateCreated = DateTime.Parse(doc.SelectSingleNode("post/pubDate").InnerText); DxYLinux联盟
  177 post.DateModified = DateTime.Parse(doc.SelectSingleNode("post/lastModified").InnerText); DxYLinux联盟
  178 DxYLinux联盟
  179 // setting other fileds DxYLinux联盟
  180 DxYLinux联盟
  181 return post; DxYLinux联盟
  182} DxYLinux联盟
   DxYLinux联盟
  个人觉得 是否应该对于Content、Comment这种占用大量内存的字段是否该采用类似Lazy Initialization 的方式 DxYLinux联盟
  说明:第一次加载所有post时候 post list内的item不带真实的comtent和comment等 DxYLinux联盟
  然后在用到的时候再从数据库读取,然后放入缓存,下次备用 DxYLinux联盟
  这样Posts内的item都变的瘦多了,类似于延迟初始化(Lazy Initialization ) DxYLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·浅谈ASP.NET的Postback  (2007-12-28 12:24:07)
 ·如何从网络上登录其它计算机的PostgreSQL  (2007-12-20 10:30:35)
 ·在Ubuntu和Debian系统下安装PostgreSQL  (2007-12-20 10:25:35)
 ·POSTFIX上的邮件日志分析工具(pflogsumm)安装与配置  (2007-11-06 17:00:56)
 ·postgresql安装笔记  (2007-09-19 11:14:48)
 ·使用Postfix构建基于FreeBSD的邮件系统  (2007-08-30 11:22:23)
 ·给postfix安装amavis+clamav  (2007-08-29 12:14:20)
 ·Postgres 客户端常用的命令  (2007-08-20 11:48:54)
 ·RHEL5安装Postfix+CyrusSASL+dovecot  (2007-06-22 13:19:19)
 ·postfix支持mysql+cyrus-sasl  (2007-06-11 17:32:19)