Open-source project-website performance tuning-page state persistence

Source: Internet
Author: User
Tags dnn dotnetnuke website performance
ArticleDirectory
    • Performance setting options provided by open-source website Systems
    • What is page sate persistence?
    • How to use this new feature in the open-source project dnn Architecture
    • How to Implement
    • What is the impact on performance?

The reason why I use this question is that many people often ask me how to study open-source projects. Some open-source projects do not seem to have a head in the eyes of others. They are some projects with no practical value. In fact, open-source projects are often the experimental field of new technologies, and they are excellent in the world.ProgramThe collection of developer wisdom. if you carefully study these open-source projects, you will not only get the functions of this project, but also observe new technologies and learn the wisdom of more intelligent than you.

Dotnetnuke (dnn for short), an open-source project that I have been studying, maybe you don't know what system it is, but you don't have to worry about it, because I want to discuss the technology in any ASP. net websites may be used, but this open-source project is used for demonstration. However, you will see how the rigid technical instructions in msdn are cleverly used in open-source projects.

Performance setting options provided by open-source website Systems

In the dnn open-source project, some settings can be used to tune the website performance, including the following:

I will spend several articles one by one to discuss the impact of these options on website performance and provide some technical analysis. In this article, I will discuss the page state persistence setting.

What is page sate persistence?

I think you must be familiar with viewstate. viewstate is a powerful new feature of ASP. NET compared with ASP. It can save the state of a page. Simply put, it is to save the value of the time when you submit the page, such as the text box, drop-down list, and so on, so that the content you fill in will not pass the test, you can use this information to restore the previous state (of course, more useful ). People who know viewsate generally know that viewsate uses a hidden input to save the page state information. There are many such articles on the Internet. You can Google them. So what is the relationship between viewsate and performance? Because this hidden input is to be passed back and forth between the client and the server following the PostBack information, if this value is too large, it will naturally affect the performance. Of course, in addition to the size, there is also the location of viewstate storage. If we do not pass the value of viewstate back and forth, it is stored at the server end, it will naturally reduce the time used for viewstate transmission over the network, but at the same time it increases the burden on the server.

Many people may not know that a new feature of ASP. NET 2.0 is that you can reload the pagestatepersister class to customize the storage location of page viewstate. In ASP. NET v1.x, viewstate can only be stored in the hidden input element mentioned earlier. In ASP. NET 2.0, the newly added sessionpagestatepersister class provides the function of storing viewstate in the session.

How to use this new feature in the open-source project dnn Architecture

This open-source project overrides the base class of the page. In its own base class, there is a pagestatepersister read-only attribute, which will return a system according to the user's settings. web. UI. the pagestatepersister object uses this object to store viewstate on the page that runs later. This allows users to choose where to store viewstate and adjust the performance.

CodeAs follows:

Protected Overrides Readonly PropertyPagestatepersister ()AsSystem. Web. UI. pagestatepersister
 
Get
 
'Set viewstate persister to default (as defined in base class)
 
Dim_ PersisterAsPagestatepersister =Mybase. Pagestatepersister
 
If NotDotnetnuke. Common. globals. hostsettings ("Pagestatepersister")Is Nothing Then
Select Case Directcast(Dotnetnuke. Common. globals. hostsettings ("Pagestatepersister"),String)
 
Case "M"
 
_ Persister =NewCachepagestatepersister (Me)
 
Case "D"
 
_ Persister =NewDiskpagestatepersister (Me)
 
Case "S"
 
_ Persister =NewSessionpagestatepersister (Me)
 
End Select
End If
 
Return_ Persister
 
End Get
 
End Property 

From the code, we can see that if you select "page", you can use the default hidden input element to store viewstate on the page (in code processing, is to determine other situations where the "page" item is not selected, so you cannot find the case "p" sentence); if the user chooses "Memory ", in case "M", a cachepagestatepersister is returned, which is a custom viewstate processing class of this open-source system. Then, the dnn open-source system uses cache to store viewstate information on the server, that is, it is stored in the "Memory" of the server.

If it is "D", it is stored in the server file, so it is "disk", but the system seems to have kept this setting, without providing the interface.

If it is "S", it is stored in the session. This implementation is provided by ASP. NET 2.0.

How to Implement

In fact, it is relatively simple. In ASP. NET 2.0, the abstract class pagestatepersister implements the basic function of storing page State to other places. You only need to override the load and save methods provided by this abstract class.

Let's take a look at a cachepagestatepersister implemented by the dnn open-source project:

 
ImportsSystem. Text
 
 
 
NamespaceDotnetnuke. Framework
 
 
 
'''-----------------------------------------------------------------------------
'''Namespace: dotnetnuke. Framework
 
'''Project: dotnetnuke
 
'''Class: cachepagestatepersister
 
'''-----------------------------------------------------------------------------
 
''' <Summary>
 
'''Cachepagestatepersister provides a cache based page state peristence mechanic
 
''' </Summary>
 
''' <History>
 
''' [Cnurse] 11/30/2006 appsented
 
''' </History>
'''-----------------------------------------------------------------------------
 
Public ClassCachepagestatepersister
 
InheritsPagestatepersister
 
 
 
Private ConstView_state_cachekeyAs String="_ Viewstate_cachekey"
 
 
 
'''-----------------------------------------------------------------------------
 
''' <Summary>
 
'''Creates the cachepagestatepersister
''' </Summary>
 
''' <History>
 
''' [Cnurse] 11/30/2006 appsented
 
''' </History>
 
'''-----------------------------------------------------------------------------
 
Public Sub New(ByvalPageAsPage)
 
Mybase.New(Page)
 
End Sub
 
'''-----------------------------------------------------------------------------
''' <Summary>
 
'''Loads the page state from the cache
 
''' </Summary>
 
''' <History>
 
''' [Cnurse] 11/30/2006 appsented
 
''' </History>
 
'''-----------------------------------------------------------------------------
 
Public Overrides SubLoad ()
 
 
 
'Get the cache key from the web form data
DimKeyAs String=Trycast(Page. Request. Params (view_state_cachekey ),String)
 
 
 
'Abort if cache key is not available or valid
 
If String. Isnullorempty (key)Or NotKey. startswith ("_")Then
 
Throw NewApplicationexception ("Missing valid"+ View_state_cachekey)
 
End If
DimStateAsPair =Trycast(Datacache. getpersistentcacheitem (key,GetType(Pair), pair)
 
 
 
If NotStateIs Nothing Then
 
'Set view State and control state
 
Viewstate = state. First
 
ControlState = state. Second
 
End If
 
'Remove this viewstate from the cache as it has served its purpose
 
Datacache. removepersistentcacheitem (key)
 
 
End Sub
 
'''-----------------------------------------------------------------------------
 
''' <Summary>
 
'''Saves the page state to the cache
 
''' </Summary>
 
''' <History>
 
''' [Cnurse] 11/30/2006 appsented
 
''' </History>
 
'''-----------------------------------------------------------------------------
 
Public Overrides SubSave ()
 
 
'No processing needed if no States available
 
IfViewstateIs Nothing AndControlStateIs Nothing Then
 
Exit Sub
 
End If
 
'Generate a unique cache key
 
DimKeyAs NewStringbuilder ()
 
WithKey
 
. Append ("_")
. Append (IIF (page. SessionIs Nothing, Guid. newguid (). tostring (), page. session. sessionid ))
 
. Append ("_")
 
. Append (datetime. Now. ticks. tostring ())
 
End With
 
'Save view State and control state separately
 
DimStateAs NewPair (viewstate, controlState)
 
 
 
'Add view State and control state to cache
Datacache. setcache (key. tostring (), state,Nothing, Datetime. Now. addminutes (page. session. Timeout), system. Web. caching. cache. noslidingexpiration,True)
 
 
 
'Register hidden field to store cache key in
 
Page. clientscript. registerhiddenfield (view_state_cachekey, key. tostring ())
 
End Sub
 
End Class
 
EndNamespace
What is the impact on performance?

Performance is always a game problem. There is no absolute question, but how to balance it. Placing viewstate in the hidden input of page will naturally increase the processing time and network transmission time of the client. Placing viewstate on the server will naturally consume the server's memory (hard disk) and CPU. Therefore, there is no best setting, but only the most suitable one.

 
Based on my understanding, I provide the following instructions:
    • If you use an exclusive server with a high configuration, you can use the "Memory" mode to speed up your website.
    • For general applications, use the default viewstate setting, that is, use hidden input. For this open-source system, select "page ".
    • If you find that the viewstate of some pages is very large and consumes a lot of network transmission time, you can use the memory mode to put the viewstate in the cache of the server.
    • Obviously, if you use disk, it will be slower than Using Cache. This method may be used on pages with Low Reflection speed requirements and large viewstate.
    • For the implementation method stored in the session, the information previously stored in the hidden input will be passed in the session through base64 encoding. It is estimated that the performance is slower than the direct use of hidden input by default, after all, you still need to convert the encoding, but it may be helpful for security, just my speculation. I hope some experts can point out the error.
 
 
 
Reference:
 
Scottgu's blog: pagestatepersister extensibility with ASP. NET 2.0
  pagestatepersister class in 2.0-to persist state on page or session or even on database.  

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.