新一篇: 声明-------
<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script> 1 基本概念 ASP.NET提供了一个相当出色的缓存引擎机制,它允许页面保存和索引HTTP请求所要求的各种各样的对象。ASP.NET的缓存对各个应用来说是私有的,是存储各种对象的存储器。缓存的生存周期取决于应用的生存周期,也就是说,当应用重新启动时,缓存实际上也已重建。 缓存提供了一个简单的字典接口,以便于开发者能够轻而易举放置对象到缓存中,并在以后使用。最简单的情况下,放置一个对象到缓存中,就如同对字典增加一个条目。 例如:
1
Cache(“myKey”)
=
MyValue
2
即是把MyValue放入缓存中一个叫myKey的缓存对象中,当需要引用myKey值时,可以采用下面的使用方式:
3
myValue1
=
Cache(“myKey”)
4
if
myValue1
<>
Null
then
5
'
非空时的动作
6
…
7
end
if







1
<
html
>
2
<
head
>
3
<
title
>
4
数据缓冲实验
5
</
title
>
6
</
head
>
7
<
body
>
8
<
center
>
9
<
form
runat
=server
>
10
<
h2
>
XML文件缓存测试
</
h2
>
11
<
ASP:DataGrid
id
="DataGrid1"
runat
="server"
12
Width
="600"
13
BorderColor
="black"
14
ShowFooter
="false"
15
CellPadding
=3
16
CellSpacing
="0"
17
Font-Name
="Verdana"
18
Font-Size
="8pt"
19
/>
20
<
hr
>
21
<
h3
>
添加一个客户信息
</
h3
>
22
<
table
>
23
<
tr
>
24
<
td
>
姓 名:
</
td
>
25
<
td
><
ASP:textbox
id
=txtName
runat
=server
/></
td
>
26
<
td
><
ASP:RequiredFieldValidator
ControlToValidate
="txtName"
Display
="static"
ErrorMessage
="*"
runat
=server
/>
</
td
>
27
</
tr
>
28
<
tr
>
29
<
td
>
身份证号:
</
td
>
30
<
td
><
ASP:textbox
id
=txtIdno
runat
=server
/></
td
>
31
<
td
><
ASP:RequiredFieldValidator
ControlToValidate
="txtIdno"
32
Display
="static"
ErrorMessage
="*"
runat
=server
/>
</
td
>
33
</
tr
>
34
<
tr
>
35
<
td
>
信用卡号:
</
td
>
36
<
td
><
ASP:textbox
id
=txtCard
runat
=server
/></
td
>
37
<
td
><
ASP:RequiredFieldValidator
ControlToValidate
="txtCard"
38
Display
="static"
ErrorMessage
="*"
runat
=server
/>
</
td
>
39
</
tr
>
40
</
table
>
41
<
p
>
42
<
asp:button
text
="增加"
onclick
="AddBtn_Click"
runat
=server
/>
43
<
asp:button
text
="刷新"
onclick
="RefreshBtn_Click"
runat
=server
/>
44
<
p
><
p
><
p
>
45
<
asp:label
id
=lblMsg
runat
=server
/>
46
</
form
>
47
</
center
>
48
</
body
>
49
</
html
>

















































1
Sub LoadData1()
sub LoadData1 2
'当缓存对象DataCache5有效时,从缓存中读出客户信息;无效时,从文件读出信息 3
dim dv1 as DataView 4
dv1=Cache("DataCache5") 5
if dv1 = Nothing 6
dim ds as DataSet 7
dim fs as FileStream 8
dim sr as StreamReader 9
10
ds=New DataSet 11
fs=New FileStream(Server.MapPath("custom1.xml"),FileMode.Open,FileAccess.Read) 12
sr=New StreamReader(fs) 13
ds.ReadXml(sr) 14
fs.Close() 15
dv1=new DataView(ds.Tables(0)) 16
Cache.Insert("DataCache5",dv1,New cachedependency(Server.MapPath("custom1.xml"))) 17
lblMsg.text="数据从文件中读出
" 18
Else 19
lblmsg.text="数据从缓存中读出
" 20
end if 21
'绑定到DataGrid1对象 22
DataGrid1.datasource = dv1 23
DataGrid1.databind() 24
end sub
25

Sub Page_Load()
sub Page_Load(s as object,e as eventargs) 26
'加载页面时,从文件中读出客户信息 27
if Not IsPostBack 28
LoadData1() 29
end if 30
end sub
31

Sub AddBtn_Click()
sub AddBtn_Click(s as object,e as eventargs) 32
'增加一个客户信息到文件中 33
dim FS as FileStream 34
dim Reader as StreamReader 35
dim DS as DataSet 36
dim dr1 as DataRow 37
dim tw1 as TextWriter 38
if Not Page.IsValid 39
lblMsg.text="还有域未曾填充
" 40
else 41
DS=New DataSet() 42
FS=New FileStream(Server.mappath("custom1.xml"),FileMode.Open,FileAccess.Read,FileShare.ReadWrite) 43
Reader=New StreamReader(FS) 44
DS.readxml(Reader) 45
FS.Close() 46
dr1=DS.tables(0).newrow() 47
dr1("CustName")=txtName.text 48
dr1("CustIdno")=txtIdno.text 49
dr1("CustCard")=txtCard.text 50
DS.tables(0).rows.add(dr1) 51
52
FS=New FileStream(Server.MapPath("custom1.xml"),FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite) 53
tw1=New StreamWriter(FS) 54
tw1=textwriter.synchronized(tw1) 55
DS.writexml(tw1) 56
tw1.close() 57
LoadData1() 58
end if 59
end sub
60

Sub RefreshBtn_Click()
sub RefreshBtn_Click(s as object,e as eventargs) 61
LoadData1() 62
end sub
63






































































(2)初始运行效果 初始时,应用空间中无“DataCache”缓存对象,故文件从custom1.xml中读出客户信息,开始时为空,只显示了字段名。 (3)当添加一个用户信息后,文件custom1.xml发生改变,导致“DataCache5”对象无效,
LoadData1过程依然从文件custom1.xml中读取信息,并更新DataCache5对象。 (4)由于custom1.xml文件未曾发生改变,所以当按下“刷新“按钮后,信息却是从缓存对象DataCach5中读出来。
页面数据缓存技术可能比页面输出缓存技术使用得更普遍一些。