马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
asp可以使用微软的activeX使得网页功能无比强大,不过安全性也较差,而且是基于的windows服务器,所以性能稳定性也一般asp.netThemajority[ifnotall]ofthepagesinadynamicwebsitearedynamic.Thatis,pagesthatarecreatedonuserrequest.Asweallknow,dynamicwebpageshelptoprovidedynamiccontent,customizedfortheuserrequestingthepage[e.g.:theusershomepage].Dynamicpagesalsohelpprovidedynamiccontentfetchedfromachangingdatastorewithouttheneedfortheadministratortochangethepagecontenteverytimesomethingchangesinthedatastore[e.g.:Listingofbooksinapublisherswebsite].Thedisadvantageistheoverheadincreatingthepagesforeveryuserrequest.
Toovercomethis,somewebsiteshavepagecreationengineswhichcreateallpagesinonegoandsavethemasHTMLpageswhicharethenservedtotheusers.Butthiswillonlyhelpinscenarioswherethepagecontentisthesameforallrequests[user-independent]asinthesecondexampleabove.Thelistingofbooksisthesameirrespectiveoftheuserrequestingthepage.EvenifthereisprovisionforlistingbookscategorywisebyprovidingdifferentcategoryIDvaluesthroughthequerystring,thepageoutputforaparticularcategoryofbooksisthesameforallusers.
ASP.NETprovidessupportfor"caching"whichwillhelpussolvethisproblemtoagreatextend.Itcancache[storeinmemory]theoutputgeneratedbyapageandwillservethiscachedcontentforfuturerequests.Andthisisusefulonlyinthesecondscenariodescribedearlier,wherethepagecontentisthesameforallrequests[user-independent].Thecachingfeatureiscustomizableinvariouswaysandwewillseehowwecandothataswegothroughthisarticle.
Cachingapage
Inordertocacheapagesoutput,weneedtospecifyan@OutputCachedirectiveatthetopofthepage.Thesyntaxisasshownbelow:
<%@OutputCacheDuration=5VaryByParam="None"%>
Asyoucansee,therearetwoattributestothisdirective.Theyare:
Duration-Thetimeinsecondsofhowlongtheoutputshouldbecached.Afterthespecifieddurationhaselapsed,thecachedoutputwillberemovedandpagecontentgeneratedforthenextrequest.Thatoutputwillagainbecachedfor10secondsandtheprocessrepeats.
VaryByParam-Thisattributeiscompulsoryandspecifiesthequerystringparameterstovarythecache.
Intheabovesnippet,wehavespecifiedtheVaryByParamattributeasNonewhichmeansthepagecontenttobeservedisthesameregardlessoftheparameterspassedthroughthequerystring[seeExample1inthesampledownload].
Iftherearetworequeststothesamepagewithvaryingquerystringparameters,e.g.:.../PageCachingByParam.aspx?id=12and.../PageCachingByParam.aspx?id=15]andseparatepagecontentisgeneratedforeachofthem,thedirectiveshouldbe:
<%@OutputCacheDuration=5VaryByParam="id"%>
ThepagecontentforthetworequestswilleachbecachedforthetimespecifiedbytheDurationattribute[seeExample2inthesampledownload].
Tospecifymultipleparameters,usesemicolontoseparatetheparameternames.IfwespecifytheVaryByParamattributeas*,thecachedcontentisvariedforallparameterspassedthroughthequerystring.
Somepagesgeneratedifferentcontentfordifferentbrowsers.Insuchcases,thereisprovisiontovarythecachedoutputfordifferentbrowsers.The@OutputCachedirectivehastobemodifiedto:
<%@OutputCacheDuration=5VaryByParam="id"VaryByCustom="browser"%>
Thiswillvarythecachedoutputnotonlyforthebrowserbutalsoitsmajorversion.I.e.,IE5,IE6,Netscape4,Netscape6willallgetdifferentcachedversionsoftheoutput.
Cachingpagefragments
Sometimeswemightwanttocachejustportionsofapage.Forexample,wemighthaveaheaderforourpagewhichwillhavethesamecontentforallusers.Theremightbesometext/imageintheheaderwhichmightchangeeveryday.Inthatcase,wewillwanttocachethisheaderforadurationofaday.
Thesolutionistoputtheheadercontentsintoausercontrolandthenspecifythattheusercontrolcontentshouldbecached.Thistechniqueiscalledfragmentcaching.
Tospecifythatausercontrolshouldbecached,weusethe@OutputCachedirectivejustlikeweuseditforthepage.
<%@OutputCacheDuration=10VaryByParam="None"%>
Withtheabovedirective,theusercontrolcontentwillbecachedforthetimespecifiedbytheDurationattribute[10secs].Regardlessofthequerystringparametersandbrowsertypeand/orversion,thesamecachedoutputisserved.[SeeExample3inthedownloadforademonstration].
DataCaching
ASP.NETalsosupportscachingofdataasobject</p>asp可以使用微软的activeX使得网页功能无比强大,不过安全性也较差,而且是基于的windows服务器,所以性能稳定性也一般 |