Development of the content publishing system

Source: Internet
Author: User
When the site traffic is high, in order to improve the system performance and reduce the system response time, we often consider making the site static and releasing it using the background publishing system. Static pages have many advantages in performance. However, static pages are not flexible enough and have poor scalability, which may be difficult to maintain in the future. Next, let's talk about how to generate these static sites based on my experience.
Generally, static pages are generated by creating Static Page Templates, reading data from the data source, generating html code blocks, replacing tags in the templates, and generating static files. For example, the template section of the article page is as follows:
  

<Table border = "1" style = "BORDER-COLLAPSE: collapse" width = "100%" cellpadding = "2" cellspacing = "2">
<Tr>
<Td>
<A href = "(# ArticleUrl #)"> (# Title #) </a>
<Br> Author (# Author #) at 13:38:00
</Td>
</Tr>
<Tr>
<Td>
Abstract: (# Description #)
</Td>
</Tr>
<Tr>
<Td>
(# Content #)
</Td>
</Tr>
</Table>

You can use the following method to read the template content:
 

/// <Summary>
/// Read the file content
/// </Summary>
/// <Param name = "strFilePath"> file path </param>
/// <Returns> file content string </returns>
/// <Param name = "strEncodingName"> encoding name (GB2312, UTF-8, etc.) </param>
Public string ReadFile (string strFilePath, string strEncodingName)
{
String strFile = string. Empty;
StreamReader sr = new StreamReader (strFilePath, System. Text. UnicodeEncoding. GetEncoding (strEncodingName ));
Try
{
StrFile = sr. ReadToEnd ();
}
Catch (Exception e)
{
}
Finally
{
Sr. Close ();
}
Return strFile;
}

Assume that the returned string is strTemplate. The title of the document read from the database is strTitle, the author is strAuthro, the publishing time is strPostTime, the description is strDescription, And the content is strContent, we can use the string Replace method to Replace the tag with the actual content to be displayed. strArticle = strTemplate. replace ("(# Title #)", strTitle );
StrArticle = strArticle. Replace ("(# Author #)", strAuthor );
..........................................

In this way, strArticle is to display the html code of the page, and then use the following method to write the file // <summary>
/// Write an Html file
/// </Summary>
/// <Param name = "strHtml"> written string </param>
/// <Param name = "strDestinationFilePath"> destination file path </param>
/// <Param name = "strEncodingName"> encoding name (GB2312, UTF-8, etc.) </param>
Public void WriteFile (string strHtml, string strDestinationFilePath, string strEncodingName)
{
StreamWriter sw = new StreamWriter (strDestinationFilePath, false, System. Text. UnicodeEncoding. GetEncoding (strEncodingName ));
Try
{
Sw. Write (strHtml );
Sw. Flush ();
}
Catch (Exception e)
{
StrErrorMessage = e. Message. ToString ();
}
Finally
{
Sw. Close ();
}
}

Note that the Replace method sometimes fails, for example, <a href = "http: // (# UserID #) .it.com.cn "> one </a> here (# UserID #) cannot be replaced by the Replace method. You can Replace strArticle = Regex. replace (Article, "\ (# UserID # \)", strUserID );
Now we can use the method to generate static files. Let's take a look at some personalized techniques to provide users with various pages of different styles.
Currently, the div + css method is generally used to provide different pages for each user. That is to say, the html code structure of each user page is the same, but the external css is different. In this way, when writing a page, we can hard encode the basic html code into our program, reduced Complexity. Css can control the page layout, which is easy to implement. In this method, there is only one template for the same page of all styles, and one style corresponds to one css file. In this way, it can meet General requirements. Many blogs use this method, such as blogcn and douban.
However, this method also has a flaw, because html code is hard-coded to the background code, and there is only one template for all styles of the same page, so page personalization is also limited. For example, if I want a style article list to be in the form of a table or another method, it cannot be implemented because we have written the specific content in the background code, only the referenced css files are different. As long as we make some improvements, we can customize the page as we like. My method is as follows:
One template for each style, one css. For example, another style template on the document page is as follows: <div>
<A href = "(# ArticleUrl #)"> (# Title #) </a>
<Br> Author (# Author #) at 13:38:00
<Hr>
Abstract: (# Description #)
<Br> (# Content #)
<Div>

Similarly, we can use the alternative method above to generate a page, but each time we need to select different template files based on the template selected by the user, instead of selecting the same one for all users. Here, you may ask, what if the list is used? Previously, the html code of the list was directly written in the background code. Now? In this case, we can make the following template. <Table>
<! -- ArticlesList Start -->
<! -- Article (# ArticleID #) Start -->
<Tr>
<Td height = "20"> </td>
</Tr>
<Tr>
<Td>
<Table border = "1" style = "BORDER-COLLAPSE: collapse" width = "100%" cellpadding = "2" cellspacing = "2" ID = "Table2">
<Tr>
<Td>
<A href = "(# ArticleUrl #)"> (# Title #) </a>
</Td>
</Tr>
<Tr>
<Td>
Abstract: (# Description #)
</Td>
</Tr>
<Tr>
<Td>
Posted on (# ReleaseTime #) & nbsp; | & nbsp; comment (# CommentCount #) & nbsp; | & nbsp; Access (# VisitCount #))
</Td>
</Tr>
</Table>
</Td>
</Tr>
<! -- Article (# ArticleID #) End -->
<! -- ArticlesList End -->
</Table>

In the template above <! -- Article (# ArticleID #) Start --> and <! The content between -- Article (# ArticleID #) End --> is the code of an Article in the Article list. We only need to use a regular expression to find this part, replace the tag to get the html code of an article in the list, connect the code of all articles to get the code of the article list, and then replace the code in the template. <! -- ArticlesList Start --> and <! The content of -- ArticlesList End --> gets the code that is finally written to the page. Although there are more steps than the previous div + css method, this is indeed effective and feasible. You may find that the template above also provides a basis for updating pages without reading the database. For example, if you want to delete an article with ID 100 from the article list, we only need to delete <! -- Article 100 Start --> and <! -- Article 100 End -->. You do not need to read the database any more. However, this will also pose a risk. If a file operation fails, it will always fail and cannot be synchronized with the database content. Of course, you can solve this problem by providing other functions.
Since we publish pages as static pages for performance purposes, we should look at how to improve the performance of static pages.
1. Extract the public parts of all pages and place them in external files for reference.
For example, the navigation part of all pages is the same, and these parts are often updated based on user operations. If you write them completely in each page, the update is very costly, each page must be overwritten. We can plug-in the content to js. when updating the content, we can update the corresponding js file. Once updated, the whole site will be updated. Here, you must note that special characters in Javascript should be escaped, such as; 'and other special characters must be appended with/before escape.
2. Partial update instead of Overall Update
We can use <! -- ArticlesList Start --> and <! -- ArticlesList End --> this flag marks the article list. when updating the article page, we only need to generate the html code of the article list to replace the original page (non-template) the content between the two tags is enough. In this way, the number of reads to the database can be greatly reduced, and the performance will naturally increase a lot. The more pages with different content, the more obvious the performance advantage.
3. Use a layered tree structure to store files
This is actually an optimization of read performance. We should not generate a large number of files in the same root directory, so that the server traversal will affect the performance. We can use a hierarchical tree structure to generate files, such as organizing file directories by year, month, or day.
Using the above methods in a comprehensive and purposeful manner can improve the performance of the publishing system and maximize user interface personalization. Sometimes, we can use static and dynamic methods to improve system performance. For example, the first page of the article list generates static data, and the later part uses dynamic data. In short, the higher the Access frequency, the more static pages are generated, and the higher the update frequency, the more dynamic the page is. Therefore, we need to balance the two and make specific choices.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.