1.Cache对象支持清扫(scavenging):当系统资源不足时,它会自动从缓存中删除低优先级的或不常用的项。
2.在从缓存中获取一个项后,应当检查看该项是否为nothing。如果为nothing,就需要把该项重新赋值给缓存。
3.在添加一个项到缓存中时,可以把该项与一个文件关联。如果文件发生变化,则该项被自动从缓存中删除:Cache.Insert("itemName","itemValue!",New CacheDependency(MapPath("fileName.txt")))
4.设置缓存依赖,当数据库数据发生变化时,清除缓存中相应的项:
1.首先创建一个触发器:
CREATE TRIGGER UpdateCache
ON Products
FOR UPDATE, DELETE, INSERT
AS
DECLARE @cmd Varchar( 200 )
SELECT @cmd = 'echo ' + Cast( getDATE() As Varchar( 50 ) ) +
' > c:\dataTableChange.txt'
EXEC master..xp_cmdshell @cmd, no_output
2.测试页面
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Page_Load
Dim dstProducts As DataSet
dstProducts = Cache( "productsDS" )
If dstProducts Is Nothing Then
dstProducts = GetProducts()
Cache.Insert( "Products", dstProducts, _
New CacheDependency( "c:\dataTableChange.txt" ) )
End If
dgrdProducts.DataSource = dstProducts
dgrdProducts.DataBind()
End Sub
Function GetProducts() As DataSet
Dim conNorthwind As SqlConnection
Dim strSelect As String
Dim dadProducts As SqlDataAdapter
Dim dstProducts As DataSet
conNorthwind = New SqlConnection( "Server=Localhost;UID=sa;PWD=XXXXXX;Database=Northwind" )
strSelect = "Select TOP 10 * From Products ORDER BY ProductID"
dadProducts = New SqlDataAdapter( strSelect, conNorthwind )
dstProducts = New DataSet()
dadProducts.Fill( dstProducts, "ProductsDS" )
Return dstProducts
End Function
</Script>
<html>
<head><title>test.aspx</title>
</head>
<body>
<asp:DataGrid
ID="dgrdProducts"
Runat="Server" />
</body>
</html>
注:该方法调用xp_cmdshell存储过程来完成该功能,则需要赋予访问者管理员权限,安全性差。慎用!
来源:博远电子(软件定制),如涉及版权问题请与我们联系。
TAG
软件定制,软件开发,瀚森HANSEN,辽宁,沈阳,抚顺