您现在的位置:龙卷风首页 ›› 网络编程 ›› 阅读文章

asp使用缓存机制降低服务器压力

asp有服务器缓存和本地缓存两种机制降低服务器压力

前两种机制是针对服务器端的,第三种是针对本地用户的。

设置ASP缓存的几个语句

1.Application变量

一般采用缓存变量:application("变量名")=[变量值]

将这些语句放在网站根目录下的global.asa中即可取到全局变量。

2.Buffer

Buffer 属性指示是否缓冲页输出。当缓冲页输出时,只有当前页的所有服务器脚本处理完毕或者调用了 Flush 或 End 方法后,服务器才将响应发送给客户端。

服务器将输出发送给客户端后就不能再设置 Buffer 属性。因此,应该在 .asp 文件的第一行调用 Response.Buffer 。

语法
Response.Buffer [= flag]参数
flag
指定是否缓冲页输出,可为如下值之一。 值 说明
FALSE 不缓冲。该值是默认值。服务器在处理脚本的同时将输出发送给客户端。
TRUE 除非当前页的所有 ASP 脚本处理完毕或调用了 Flush或 End 方法,否则服务器不将响应发送给客户端。

注释

如果当前 ASP 脚本缓冲设为 TRUE ,但未调用 Flush 方法,则服务器将使客户端的请求保持活动。由于服务器不必为每个客户端创建新的连接,从而节省了时间。

不过,缓冲将在服务器未处理完当前页的所有脚本之前阻止在客户端显示响应。对于长的脚本来说,有可能会感觉到延迟。


即当Buffer为True的时候,asp会将这个程序解释完毕后一起发送到客户端,
当Buffer为False的时候,asp会边解析边发送数据.

这句话的意思就是指明输出页面是否被缓冲,当属性值为True时,服务器将不会向客户端发送任何信息,直到所有程序执行完或者遇到语句,才会释放缓冲区的信息。
下面的这个代码演示了Buffer的这一特性:


1

如果将Response.Buffer = "False"改为Response.Buffer = "True"则会解析完毕再输出,即输出一个含有1到100的数字的网页

Response的Buffer属性虽然能够提高页面显示速度,但是也要分什么情况。如果你正在制作一个普通的个人主页,访问量不是很高,并且没有什么复杂的执行程序,那么用不用这个属性就不是很重要,因为将数据缓冲也需要一段时间,只不过我们感觉不到罢了;但是如果你正在制作一个大型论坛或者一个产品展示或其他的商务站点,并且访问量很高,那么我建议在程序的第一行加入

这句话,因为这样能够让客户在有效的时间内获得更多的数据。

3.Expires

语法:Response.Expires = [时间]

使用浏览器或者代理缓存后,对Web服务器的点击次数就会减少。如果想精确地了解所有页面,或者对于邮递广告,就不适于使用浏览器和代理缓存了。
  浏览器缓存由HTTP“Expires”头参数控制,它由Web服务器发送给浏览器。ASP提供了2个简单的方法发送这个头部参数。设置页面在未来一定时间内到期,可以使用Response.Expires属性。下面的例子将告诉浏览器内容在10分钟后过期:< % Response.Expires = 10 % >
  设置Response.Expires为负数或者0,就禁止了缓存。对第2个属性Response.ExpiresAbsolute的设置,允许指定在一个特殊时间到来时内容过期。
  < % Response.ExpiresAbsolute = #May 31,2001 13:30:15# % >
  除了使用Response对象来设置到期时间,还可以在HTML文件头部写< META >标记。尽管代理不会注意到这个标记,但是一些浏览器可以。
  < META HTTP-EQUIV="Expires" VALUE="May 31,2001 13:30:15" >
  最后,对于HTTP代理,使用Response.CacheControl可以指示是否缓存内容。设置属性为“Public”,打开代理缓存内容的功能。
  < % Response.CacheControl = "Public" % >
  默认情况下,这个属性是设置成“Private”的。注意:不要让代理缓冲那些显示给特定用户的页面,因为代理可能会将属于其他用户的页面送给当前用户。

资料来源:http://www.syscy.com/articleview/2008-4-16/article_view_268.htm

另外,html页面其实也支持缓存。

Useful HTML Meta Tags

Note the keywords "HTTP-EQUIV", "Name" and "Content" are case-insensitive. Their values are also case-insensitive.

Tag Name Example(s) Description
Author The author's name.
cache-control HTTP 1.1. Allowed values = PUBLIC | PRIVATE | NO-CACHE | NO-STORE.
Public - may be cached in public shared caches
Private - may only be cached in private cache
no-Cache - may not be cached
no-Store - may be cached but not archived

The directive CACHE-CONTROL:NO-CACHE indicates cached information should not be used and instead requests should be forwarded to the origin server. This directive has the same semantics as the PRAGMA:NO-CACHE.
Clients SHOULD include both PRAGMA:NO-CACHE and CACHE-CONTROL:NO-CACHE when a no-cache request is sent to a server not known to be HTTP/1.1 compliant.
Also see EXPIRES.
Note: It may be better to specify cache commands in HTTP than in META statements, where they can influence more than the browser, but proxies and other intermediaries that may cache information.

Content-Language CONTENT="en-US,fr"> Declares the primary natural language(s) of the document. May be used by search engines to categorize by language.
CONTENT-TYPE CONTENT="text/html; charset=UTF-8"> The HTTP content type may be extended to give the character set. It is recommended to always use this tag and to specify the charset.
Copyright A copyright statement.
DESCRIPTION CONTENT="...summary of web page..."> The text can be used when printing a summary of the document. The text should not contain any formatting information. Used by some search engines to describe your document. Particularly important if your document has very little text, is a frameset, or has extensive scripts at the top.
EXPIRES CONTENT="Mon, 22 Jul 2002 11:12:01 GMT"> The date and time after which the document should be considered expired. An illegal EXPIRES date, e.g. "0", is interpreted as "now". Setting EXPIRES to 0 may thus be used to force a modification check at each visit.
Web robots may delete expired documents from a search engine, or schedule a revisit.

HTTP 1.1 (RFC 2068) specifies that all HTTP date/time stamps MUST be generated in Greenwich Mean Time (GMT) and in RFC 1123 format.
RFC 1123 format = wkday "," SP date SP time SP "GMT"

wkday = (Mon, Tue, Wed, Thu, Fri, Sat, Sun)
date = 2DIGIT SP month SP 4DIGIT ; day month year (e.g., 02 Jun 1982)
time = 2DIGIT ":" 2DIGIT ":" 2DIGIT ; 00:00:00 - 23:59:59
month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)

Keywords CONTENT="sex, drugs, rock & roll"> The keywords are used by some search engines to index your document in addition to words from the title and document body. Typically used for synonyms and alternates of title words. Consider adding frequent misspellings. e.g. heirarchy, hierarchy.
PRAGMA NO-CACHE This directive indicates cached information should not be used and instead requests should be forwarded to the origin server. This directive has the same semantics as the CACHE-CONTROL:NO-CACHE directive and is provided for backwards compatibility with HTTP/1.0.
Clients SHOULD include both PRAGMA:NO-CACHE and CACHE-CONTROL:NO-CACHE when a no-cache request is sent to a server not known to be HTTP/1.1 compliant.
HTTP/1.1 clients SHOULD NOT send the PRAGMA request-header. HTTP/1.1 caches SHOULD treat "PRAGMA:NO-CACHE" as if the client had sent "CACHE-CONTROL:NO-CACHE".
Also see EXPIRES.
Refresh CONTENT="15;URL=http://www.I18nGuy.com/index.html"> Specifies a delay in seconds before the browser automatically reloads the document. Optionally, specifies an alternative URL to load, making this command useful for redirecting browsers to other pages.
ROBOTS





CONTENT="ALL | NONE | NOINDEX | INDEX| NOFOLLOW | FOLLOW | NOARCHIVE"
default = empty = "ALL"
"NONE" = "NOINDEX, NOFOLLOW"

The CONTENT field is a comma separated list:
INDEX: search engine robots should include this page.
FOLLOW: robots should follow links from this page to other pages.
NOINDEX: links can be explored, although the page is not indexed.
NOFOLLOW: the page can be indexed, but no links are explored.
NONE: robots can ignore the page.
NOARCHIVE: Google uses this to prevent archiving of the page. See http://www.google.com/bot.html

GOOGLEBOT In addition to the ROBOTS META Command above, Google supports a GOOGLEBOT command. With it, you can tell Google that you do not want the page archived, but allow other search engines to do so. If you specify this command, Google will not save the page and the page will be unavailable via its cache.
See Google's FAQ.

 

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content