ASP cache technology Detailed application skills

Source: Internet
Author: User
Tags delete cache what is asp

One, what is ASP cache/why to cache
when your Web site uses ASP technology to establish the initial stage, you may feel that the ASP Dynamic Web technology brings convenience, as well as the arbitrary modification of the HTTP control freely. However, as the number of visits increases, you will find that your site is getting more and more slow, and IIS restarts more and more frequently. Next, you will want to optimize the ASP, such as replacing a better performance database, indexing, writing stored procedures, and so on. Some of these measures do not need to increase the cost of pressure, some are very expensive (for example, bundle access to SQL), and the effect is not necessarily.

In the face of Web access pressure, I think the most economical way is to use caching optimization technology to ease the Web service pressure.
The increase in web traffic usually means a rapid increase in the following resource requirements:

1, the network card traffic increases, need to consume more CPU processing network traffic and network I/O thread.
2, the need to open/close the database links more frequently (if you use database technology-Typically, ASP will use the database as the data store), the amount of things that seriously consumes resources, and transactions competing for resources caused by deadlock, increase network I/O or CPU consumption.
3, if the use of Session, IIS in order to maintain state, will consume more memory, and memory consumption may cause insufficient physical memory, resulting in physical memory and secondary storage frequent exchange, resulting in code execution of the pause, web response block.
4, due to the lack of access to timely response, will cause Web page access failure, resulting in user refresh, thereby exacerbating the CPU, memory and other resource needs.

In fact, most of the time dynamic code execution is unnecessary, considering the usual web applications.

Second, the ASP cache classification

In summary, the ASP cache can be divided into two categories:

1. File caching
The so-called file caching, is based on logical judgment, a certain period of time, the implementation of an ASP will not be very big changes, so the content in the form of static HTML storage, and then with the Web redirect technology to allow clients access to static files, to reduce CPU, database resources and so on. Such a lot of applications, such as many forums in reply to the post when the entire post to regenerate a static file, and then redirect, such as Donews.com forum. The static also has a side effect (benefits) – it can easily be indexed by Google and other search engines. Some of the so-called press release systems are using this technology.

2. File Fragment Caching

So-called file caching, it is also based on logical judgment, a certain part of the data (usually the need to consume resources of the bulk database query) will not change for a certain period of time, so we can use the form of file storage, when it is necessary to read the file to obtain data, to avoid increasing the burden of the database. For example, we usually store some data in XML format, and then use XSLT technology to implement the display (XML processing usually requires a lot of CPU resources, so it is usually IE read the XML directly to the client on the customer's CPU). This is how CSDN's forum is handled.

3, main memory cache

In addition, it is possible to consider processing caching in memory, storing content that needs to be responsive in memory, and delivering it immediately from a fast store once access is needed. If a large number of access requirements are concentrated in a few small pages or main memory, I would like to use main memory cache can greatly improve Web access performance.

Iii. How to implement/use caching

Implementing caching requires consideration of the following issues:

1, which pages will not change in a short period of time?
Analysis of their own site, such a lot of pages. For example, a site usually has news information class columns, these columns are usually site maintenance staff at a certain time of day to release information, and then rarely change the page. These pages are suitable for use with static file caching. In fact, the so-called press release system is to do so, then it can also refer to the ideas of these systems to transform their original dynamic ASP page.

2, those pages for all visitors to use the same logical generation (that is, not distinguish between visitors).
In addition to news and information such as the column of all visitors to look at an interface, the forum, such as the use of resources used in general can also be designed as a unified logic generation (the same post, Dick look alike), for such application pages we can also use static cache to achieve. It is also possible to consider fragmentation of the data, using scripting techniques that are handled by the client browser beyond the server's processing power.

3, the use of the cost of caching and harvesting.
The main thing is "space Exchange (response) time." Using caching technology to preprocess the frequently needed content to improve the responsiveness of Web servers is more important to win the favor of visitors.
The cost is the increase in web space requirements, while it may affect the access effect.
But I think the proper caching is more beneficial than the disadvantage.

4, those places are not suitable to use the cache
Dynamic Query page, each person's query content is not the same, so the results are not the same, so it is unlikely to generate a cache of query results, so the use of caching is more complex and cache utilization, resulting in management costs (assuming you cache 1000 query keywords, It is also troublesome to manage these keywords in response to the cache.

Iv. Example Analysis

Suppose the original layout of a suggested forum is as follows:

Root directory:
Default.asp Home, General is the essence, recommend and so on
Listborad.asp This document lists the name and description of all the columns, and if you carry the parameter mainbid, you will list the columns under the plate.
listthread.asp If the file does not carry any parameters indicating that all the posts are listed, carry mainbid to indicate that all posts of a block are listed. If you carry subbid to express the list of specific column posts. If you carry a page parameter to represent pagination, list the topics.
Viewthread.asp list the content of a posting. Let's assume that the post is displayed as a statement, with any comments listed at the back. The ID parameter is the post to display.
Reply.asp respond to a post, carrying a parameter ID to respond to a posting

The rest of the discussion is pending.

Above, we can see, if all is to use the original asp/php to do, that almost every ASP file execution needs database operation, frequent query, multiple table query. To know that the query database will eventually bring performance degradation, the response speed down, bring visitors slow browsing impact, not conducive to the quality of the web. More importantly for a B two people to Will, they visit viewthread.asp and the like if the ID is consistent, so many times they will see the same content (their browser received almost the same HTML code), but for this "the same content", the server needs to open database links, queries, Read records, display, close records, database links .... The following operations that consume server resources, if more people visit, end up consuming server resources. In fact, the repetitive work done for the "same content" can be avoided by using caching techniques. Such as:

After reply.asp commits the content, we call to generate the static function immediately, will store the entire post content as viewthread_xxxx.htm and so on the static HTML file, and then normally accesses the viewthread.asp?id=xxxx, the system automatically Redirect to the corresponding static file viewthreadxxxx.htm go. In this way, when a post is not the latest release, he is always static content provided to the browser, once the new submission, will be updated to the static file, so that will save a lot of database operations, greatly improve the response speed.

Listborad.asp can also be implemented statically. We can analyze the parameters that it may carry, set the cache filename to listboard_xx.htm, and update listboard_xxx.htm when adding new columns. Listthread.asp is similar, except that there are many cached files because of their more parameters. Click to cache listthread.asp? subbid=xxx&page=2, then the corresponding static file is listthread_xxx_p2.htm. The same is default.asp.

So how do you decide when to update? At what time is it updated?
Discuss listthread.asp? subbid=xxx&page=2, we extracted subid and page in the execution of listthread.asp and then probed Listthread_xxx_ P2.htm is present, and if it does not exist, invoke the static build function to generate the file, which is eventually redirected to this static file. Note that the absence here means that new content needs to be updated.

How does that cause the file not to exist? Delete. We can delete static files like listthread_xxx_p2.htm when we publish a new post, delete posts, and move posts. This informs when the cache is to be cached.

Now there is a problem, how to generate a static file?

We note that the "same content" that we mentioned earlier. We can copy a copy of Default.asp, listthread.asp, etc. before the transformation, named Default_d.asp, Listthread_2.asp, and in the same directory (theoretically listthtrad.asp?subid= 123 with listthread_d.asp? Subid=123 's access will be the same, so that we can invoke the modified copy, get the HTML code, and store it as a static file, in the logic of the need to generate a static file, through a Web Access request. This Web request actually corresponds to the HTML that the server itself will output before any real viewer accesses the static content, and then returns the code, which is stored as a static file using the file manipulation function. In this way, the cached file is created before the true viewer.

Such a scheme almost does not touch the original layout, almost do not cause the transformation of the 404 error. Second, static files will also help your site is easily indexed by Google and other search engines. Why

Finally, to remind, through the Web Access, ASP programming environment, many people adopt XMLHTTP component access, this can cause many problems. The XMLHTTP itself will cache the requested resources, causing the content we request through this component to be not up to date, resulting in logical confusion. Therefore, you should select an XML Server HTTP object or WinHTTP component to implement the Web request resource.
Using caching technology in ASP can greatly improve your site performance, but these implementations are very simple, and it will show how the caching on the server works and how you use an ADO connection technology called disconnected.

Before introducing these technologies, let's explain exactly what an ASP's caching technology is.

The so-called cache is actually in memory to open up a space to save data, using the cache you do not have to frequent access to the data you have saved on the hard disk, flexible use of the cache you will be spared the distressed look at the poor hard drive read data tortured. Once you have executed a query action and put the query results into the cache, you can quickly and repeatedly access the data. And if you do not put the data into the cache, when you execute the query again, the server will consume the process from the database to get and sort.

When the data is stored in the cache, the time taken to Requery is primarily the time it takes to display the data.
That is, we should not put data that is often needed to change into a server-side cache, and we should put the data in the cache with fewer changes, but with frequent access.

Now let's talk about ASP's use of caching technology on the server side, and then discuss how ASP can use the
Caching technology.

When you have a large amount of data (static, that is, less change) needs to be displayed to the client, you can consider using the server-side caching technology. This kind of technology is especially suitable for those websites with strong display style consistency (hehe, not easy to use for non-mainstream websites). )

In fact, the implementation of the method is particularly simple, we just take a look at the following simple example to understand.

This is an example program for displaying the classification of books.

displaybooks.asp file:

Copy Code code as follows:

<%@ language=javascript% >
< HTML >
< BODY >
< form Method=post >
Book Classification <%= getbookslistbox ()% >
< p>
< input Type=submit >

<%
function Getbookslistbox ()
{
Bookslistbox = Application ("Bookslistbox")
if (Bookslistbox!= null) return bookslistbox;
CRLF = String.fromCharCode (13, 10)
Bookslistbox = "< select name=books>" + CRLF;
SQL = "SELECT * from books ' ORDER by Name";
Cnnbooks = Server.CreateObject ("ADODB"). Connection ");
Cnnbooks.open ("Books", "Admin", "");
Rstbooks = Cnnbooks.execute (SQL);
Fldbookname = Rstbooks ("BookName");
while (!rstbooks.eof) {
Bookslistbox = Bookslistbox + "< option>" +
Fldbookname + "" + CRLF;
Rstbooks.movenext ();
}
Bookslistbox = Bookslistbox + ""
Application ("Bookslistbox") = Bookslistbox
return bookslistbox;
}
% >

Very simple, in fact, is to use a very simple application technology, and the different words:

Copy Code code as follows:

Application ("Bookslistbox") = Bookslistbox

You can verify that you will find that the number of requests on the server will be reduced a lot. This situation is particularly appropriate for Web content that is not frequently updated, such as one that you update only once a day (or a very long time).

We'll discuss a client-side caching technique. This technique is also called disconnected ADO Connection technology (the level of translation is too high, it sounds so awkward). This technique is mainly used to save user's personal information, such as user's password, code name, etc. It mainly uses some of the properties of ADO. At the same time also answered some netizens have mentioned whether the use of ADO object in application. The explanation is not clear, let the code speak for the following:
File Global.asa:

Copy Code code as follows:

<!--METADATA type= "TypeLib" file= "C:\Program Files\Common
Files\system\ado\msado15.dll "-->
< SCRIPT language=vbscript runat= "Server" >
Sub Application_OnStart
SQL = "Select UserName, Password from UserInfo"
Cnnusers = "Dsn=user"
Set rsusers = Server.CreateObject ("ADODB. Recordset ")
' Note that the following two sentences are used to implement the disconnected ADO technology called available
Rscustomers.cursorlocation = adUseClient
rsCustomers.Open SQL, Cnnadvworks, adOpenStatic, adLockReadOnly
' Disconnect the recordset from the database
Rscustomers.activeconnection = Nothing
Set application ("rscustomers") = rscustomers
End Sub

File users.asp

Copy Code code as follows:

<%
' Clone method enables each user to have one of their own recordset collections
Set yourusers = Application ("Rsusers"). Clone
Set UserName = yourusers ("UserName")
Set Password = yourusers ("Password")
Do Until yourusers.eof
% >

User name:<%= UserName% > User password:<%= Password% >

<%
Yourusers.movenext
Loop
% >

In the role of caching, I guess I don't have to say any more, its role has been very obvious, especially for the information is very large or full database pages of the site, he can make good use of the memory resources of the host, speed up the implementation of ASP efficiency, reduce the burden on the server, and the dynamic network in this respect to do is the most prominent, Like his current version of the dvbbs7.1.0, but also in the use of the cache to a higher level, the front and back most of the operation is related to the cache, and now the net used is the fan of the city Prodigal's cache class:

Copy Code code as follows:

<%
Class Cls_cache
' ================== use instructions ====================
' This kind of module is the pioneer of the dynamic net, the author: The Prodigal son of the city. If you use this class module, please do not remove this description.
' Public variable: Reloadtime expiration Time (in minutes) defaults to 14400
' Maxcount the maximum value of the cached object, and automatically deletes objects that are less frequently used. The default value is 300
' CacheName the total name of the cache group, the default value is ' Dvbbs ', and if there is more than one cache group in a site, you need to change this value externally.
' Properties: Name defines cache object names, write-only properties.
' Property: Value reads and writes cached data.
' Function: Objisempty () Determines whether the current cache expires.
' Method: Delcahe (Mycahename) Deletes a cached object manually, and the parameter is the name of the cached object.
'========================
Public Reloadtime,maxcount,cachename
Private Localcachename,cachedata,delcount

Private Sub Class_Initialize ()
reloadtime=14400 ' Expiration time in minutes
Cachename= the total name of the "Dvbbs" cache group
End Sub

Private Sub Setcache (setname,newvalue)
Application.Lock
Application (setname) = NewValue
Application.UnLock
End Sub

Public Property Let name (ByVal vnewvalue) ' Cache object name
Localcachename=lcase (Vnewvalue)
End Property

Public Property Let Value (ByVal vnewvalue) ' reads and writes cached data
If localcachename<> "" Then
Cachedata=application (CacheName "_" &localcachename)
If IsArray (cachedata) Then
CacheData (0) =vnewvalue
CacheData (1) =now ()
Else
ReDim CacheData (2)
CacheData (0) =vnewvalue
CacheData (1) =now ()
End If
Setcache CacheName "_" &localcachename,cachedata
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "Please change the cachename."
End If
End Property

Public Property Get Value ()
If localcachename<> "" Then
Cachedata=application (CacheName "_" &localcachename)
If IsArray (cachedata) Then
Value=cachedata (0)
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "the CacheData is Empty."
End If
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "Please change the cachename."
End If
End Property

Public Function objisempty () ' Determines whether the current cache expires
Objisempty=true
Cachedata=application (CacheName "_" &localcachename)
If not IsArray (cachedata) Then Exit Function
If not IsDate (CacheData (1)) Then Exit Function
If DateDiff ("s", CDate (CacheData (1)), now ()) < 60*reloadtime Then Objisempty=false
End Function

Private Sub makeempty (setname) ' Frees up memory
Application.Lock
Application (setname) = Empty
Application.UnLock
End Sub

Public Sub Delcahe (mycahename) ' Delete cache
Makeempty (CacheName "_" &mycahename)
End Sub
End Class

' Set wydcache=new Cls_cache
' wydcache.reloadtime=0.5 ' defines the expiration time (in minutes)
' Wydcache.cachename= ' pages ' defines the cache name
' If Wydcache.objisempty () Then ' to determine whether it is available (including expiration and null values)
' Response.Write Wydcache.value
' Else
'..................
' Boardjumplist=xxx
' Wydcache.value=boardjumplist ' write content
' Response.Write boardjumplist
' End If
'
' Mycache. Delcahe ("cache name") Delete cache
%>

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.