|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| 讨论一下类似BlogEngine内一次性加载所有Post是否可取 |
|
试验了一下给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论坛讨论 |
|
|
|
|
|