Introduction to the cache function in ASP. NET

Source: Internet
Author: User
Tags connectionstrings
Introduction to the cache function in ASP. NET

 

ASP. NET performance

ASP is executed by script interpretation.

The ASP. net engine ensures high-performance one-time compilation.

 

ASP. NET performance

The ASP. NET engine guarantees high performance in principle

CLR just-in-time Compiler

Good support for multiple CPUs

Optimize compilation during runtime

Engine Optimization cannot completely solve performance problems

The Code logic is not optimized.

Potential Performance Optimization points that cannot be controlled by the engine

 

Performance problems and Optimization Principles

Performance Parameters

Throughput: Most tests use throughput as the standard. In the evaluation, we consider the number of user connections.

Response time

Execution time

Scalability

 

Basic Optimization Principles

Reduce Unnecessary resource consumption CPU and memory

Performance improvement skills

Avoid unnecessary operations

Page_load and ispostback

Page_load

Properties_change

Action

 

The order of requests executed by the Asp.net server segment. Among them, the properties_change event includes (for example, the textchanged event of textbox. If this event is frequently triggered, it will increase the load on the server. In Asp.net, this event is generally sent to the server together with the Action event ), action event (such as the button click event)

 

 

Void page_load (Object sender, eventargs E)

{

//... Set up a connection and command here...

If (! Page. ispostback)

{

String query = "select * from authors where firstname like '% Justin % '";

Mycommand. Fill (DS, "Authors ");

Mydatagrid. databind ();

}

}

 

 

Void button_click (Object sender, eventargs E)

{

String query = "select * from authors where firstname like '% Brad % '";

Mycommand. Fill (DS, "Authors ");

Mydatagrid. databind ();

}

 

Disable unnecessary session Status

<% @ Page enablesessionstate = "false" %>

Use Server Control

You do not need to use server control if necessary.

You can disable viewstate if not necessary.

<Asp: DataGrid enableviewstate = "false" runat = "server"/>

<% @ Page enableviewstate = "false" %>

Do not use exception to control program processes

Try

{

Result = 100/num;

}

Catch (exception E)

{

Result = 0;

}

Such execution is time-consuming and can be simply applied to the following

If (num! = 0)

Result = 100/num;

Else

Result = 0;

 

Disable VB and JScript Dynamic Data Types

<% @ Page Language = "VB" strict = "true" %>

Access using Stored Procedure data

Do not use dataset for read-only data access

Use sqldatareader instead of Dataset

Sqldatareader is read-only, forward-only

Disable ASP. NET debug mode

Use ASP. net output cache to buffer data

ASP. NET output buffer

Page Buffering

<% @ Outputcache %>

Duration

Varybyparam: different input parameters make each outputcache version different.

<% @ Outputcache duration = 60 varybyparam = "Textbox (Control name)" %>

Fragment Buffer

Varybycontrol

The above page buffering is for the whole page. In more cases, we make the control to be buffered into a user control and mark it with outputcache. However, there is a serious situation at this time. If there are different implementation objects in the same user space on the page, when the page is loaded, when one implementation of the user control is complete, the second user control applies the first cache. In this case, we need to set varybycontrol.

ASP. NET output buffer

Data Buffer

Expiration dependency Condition

ASP. NET also provides cache objects. For details, refer to msdn

Cache. insert ("mydata", source, new cachedependency (server. mappath ("authors. xml ")));

Cache. insert ("mydata", source, null, datetime. Now. addhours (1), timespan. Zero );

// When the cache is not overwritten, it will expire in 20 minutes. If it is overwritten, the time will start again.

Cache. insert ("mydata", source, null, datetime. maxvalue, timespan. fromminutes (20 ));

 

ASP. NET 1.0 Cache Mechanism

1. caching has the greatest impact on Application Performance

Cache early and frequently

Cache can prevent many mistakes

2. ASP. NET provides the primary form of Cache

Page cache (outputcache command)

Clip cache (user control output cache)

3. Data expiration, especially for database-driven applications, during database data changes.

Cache function added in ASP. NET 2.0

L new datasource Control

Sqldatasource accessdatasource objectdatasource

Use the datasource control to cache data

By default, sqldatasource uses an absolute expiration Policy to cache data. In addition, you can also choose to use a variable expiration policy. You can select cacheexpirationpolicy as the attribute to change the expiration policy. If it is sliding, the data is refreshed without the cacheduration time. The default value is absolute.

[Reference Code]

<Asp: dropdownlist id = "dropdownlist1" datasourceid = "sqldatasource1" datatextfield = "title" runat = "server"/>

// Cache expiration time, which indicates refreshing data every 300 seconds

<Asp: sqldatasource id = "sqldatasource1" enablecaching = "true" cacheduration = "300" connectionstring = "<% $ connectionstrings: pubsconnectionstring %>"

Selectcommand = "select * from [titles]" runat = "server"/>

L substitution Control

L SQL cache invalidation

Solve the problem of data expiration in ASP. net1.0

Cache function added in ASP. NET 2.0

L use post-Cache substitution

ASP. net1.0 uses the user control cache.

Our requirement is to dynamically add content to the cache page content.

L substitution control: Use the substitution control to insert dynamic content into the cached page.

 

// The content cached on the page will not change when the page expires

<P> time: <% = datetime. Now. tostring () %> </P>

// Dynamically cached content, which is updated in time without refreshing the content once

<B> Real Time:

<Asp: substitution id = "substitution1" runat = "server" methodname = "getcurrentdate"/>

</B>

<SCRIPT runat = "server">

Shared function getcurrentdate (byval context as httpcontext) as string

Return now. tostring ()

End Function

</SCRIPT>

Cache function added in ASP. NET 2.0

L SQL cache invalidation

Any updates to the data can be changed in the cache immediately without waiting for the cache time to expire.

L function implementation steps (only available in SQL Server and later versions)

1. Configure SQL Server to support SQL cache invalidation

Enter cd c: \ WINDOWS \ Microsoft. NET \ framework \ v2.0.50215 \

Aspnet_regsql-s slhsql2005-1-e-d pubs-ed

Aspnet_regsql-s slhsql2005-1-e-d pubs-T authors-et

Description:

Aspnet_regsql Command help (-s-e should be capitalized)

-S connected database server

-E authentication (Windows integrated authentication)

-D: databases used

-Ed enables SQL cache invalidation for this database

-T tables used

-Et enables SQL cache invalidation for this database

2. Compile ASP. Net program code using SQL cache invalidation

Page configuration: defines the page cache output time 600 seconds pubs: authors case sensitive

<% @ Outputcache duration = "600" varybyparam = "NONE" sqldependency = "pubs: authors" %>

// Define the pageload event, which is only used to demonstrate the effect. In this way, the database is updated, and the time and dataview are updated //, instead of waiting for the expiration time.

Protected sub page_load (byval sender as object,

Byval e as system. eventargs) handles me. Load

Label1.text = ctype (system. datetime. Now (), string)

End sub

3. Configure web. config

In <system. Web> Configuration, pubsconnectionstring corresponds to the database connection string

<Caching>

<Sqlcachedependency enabled = "true" polltime = "1000">

<Databases>

<Add name = "pubs" connectionstringname = "pubsconnectionstring"/>

</Databases>

</Sqlcachedependency>

</Caching>

Configure in <configuration>

<Connectionstrings>

<Add name = "pubsconnectionstring" connectionstring = "Data Source =.; initial catalog = pubs; Integrated Security = true" providername = "system. Data. sqlclient"/>

</Connectionstrings>

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 908970

Related Article

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.