缓存(销毁)依赖
1:缓存依赖的类别
文件,数据表,数据行,一段时间,控件,查询字符串等
2:缓存项之间的依赖
Key:缓存集合中唯一确认一项的键
Value:保存在缓存中的值
Reason:是"CacheItemRemovedReason"的一个枚举.说明缓存项被移出的原因
我们也可以手动移出缓存
4:数据库中的缓存依赖
这部分代码适用于sql2000,在sql2005下没有这么麻烦.回头我们再讨论sql2005下的缓存.
文件,数据表,数据行,一段时间,控件,查询字符串等
2:缓存项之间的依赖
protected void Button3_Click(object sender, EventArgs e)
{
// create item A and item B
string itemA = "ItemA";
string itemB = "ItemB";
Cache.Insert("ItemA", itemA, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
Cache.Insert("ItemB", itemB, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
}
private void MyItemRemovedCallBack(string key, object value,
CacheItemRemovedReason reason)
{
// remove the item from the cache
if (key == "ItemA" ) Cache.Remove("ItemB");
else if (key.Equals == "ItemB") Cache.Remove("ItemB");
}
以上代码中{
// create item A and item B
string itemA = "ItemA";
string itemB = "ItemB";
Cache.Insert("ItemA", itemA, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
Cache.Insert("ItemB", itemB, null, DateTime.Now.AddMinutes(10),
TimeSpan.Zero,
CacheItemPriority.Default, MyItemRemovedCallBack);
}
private void MyItemRemovedCallBack(string key, object value,
CacheItemRemovedReason reason)
{
// remove the item from the cache
if (key == "ItemA" ) Cache.Remove("ItemB");
else if (key.Equals == "ItemB") Cache.Remove("ItemB");
}
Key:缓存集合中唯一确认一项的键
Value:保存在缓存中的值
Reason:是"CacheItemRemovedReason"的一个枚举.说明缓存项被移出的原因
我们也可以手动移出缓存
Cache.Remove("ItemA");
3: 缓存依赖于文件<?xml version="1.0" encoding="utf-8"?>
<MenuItems>
<MenuItem>
<Text>Home</Text>
</MenuItem>
<MenuItem>
<Text>About us</Text>
</MenuItem>
<MenuItem>
<Text>Contact us</Text>
</MenuItem>
<MenuItem>
<Text>Help</Text>
</MenuItem>
<MenuItem>
<Text>Feature</Text>
</MenuItem>
</MenuItems>
string menuPath = "MyFiles/Menu.xml";
string folderName = "MyFiles/";
DataSet ds = null;
if (Cache["Menu"] == null)
{
ds = new DataSet();
ds.ReadXml(Server.MapPath(menuPath));
// menu is created
Cache.Insert("Menu", ds, new System.Web.Caching.CacheDependency(
Server.MapPath(menuPath)),DateTime.Now.AddMinutes(60),
TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.Default,
new System.Web.Caching.CacheItemRemovedCallback(
CacheItemRemovedCallBack));
DisplayCacheCreationTime("Object was not in the cache and created at:",
DateTime.Now.ToLongTimeString());
}
else
{
// menu is created from the cache
DisplayCacheCreationTime("Object was in the cache",String.Empty);
}
我们也可以将缓存依赖于文件夹.当文件夹下的文件和子级文件夹被创建,更新,删除的时候跟新缓存.<MenuItems>
<MenuItem>
<Text>Home</Text>
</MenuItem>
<MenuItem>
<Text>About us</Text>
</MenuItem>
<MenuItem>
<Text>Contact us</Text>
</MenuItem>
<MenuItem>
<Text>Help</Text>
</MenuItem>
<MenuItem>
<Text>Feature</Text>
</MenuItem>
</MenuItems>
string menuPath = "MyFiles/Menu.xml";
string folderName = "MyFiles/";
DataSet ds = null;
if (Cache["Menu"] == null)
{
ds = new DataSet();
ds.ReadXml(Server.MapPath(menuPath));
// menu is created
Cache.Insert("Menu", ds, new System.Web.Caching.CacheDependency(
Server.MapPath(menuPath)),DateTime.Now.AddMinutes(60),
TimeSpan.Zero,
System.Web.Caching.CacheItemPriority.Default,
new System.Web.Caching.CacheItemRemovedCallback(
CacheItemRemovedCallBack));
DisplayCacheCreationTime("Object was not in the cache and created at:",
DateTime.Now.ToLongTimeString());
}
else
{
// menu is created from the cache
DisplayCacheCreationTime("Object was in the cache",String.Empty);
}
4:数据库中的缓存依赖
<connectionStrings>
<add name="ConnectionString"
connectionString="Server=localhost;Database=School;
Trusted_Connection=true"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency pollTime="10000" enabled="true" >
<databases>
<add connectionStringName="ConnectionString" name="School"/>
</databases>
</sqlCacheDependency>
</caching></caching>
if (Cache["Users"] == null)
{
// Create the cache dependency
SqlCacheDependency dep = new SqlCacheDependency("School", "Users");
string connectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT FirstName, LastName " +
"FROM Users", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
// put in the cache object
Cache.Insert("Users", ds, dep);
}
gvUsers.DataSource = Cache["Users"] as DataSet;
gvUsers.DataBind();
SqlCacheDependency dep = new SqlCacheDependency("School", "Users"); 这句话用于创建一个与数据库School中表Users关联的缓存.当我们对Users进行INSERT, DELETE, UPDATE操作的时候缓存会被移出(Cache["Users"]==null ).<add name="ConnectionString"
connectionString="Server=localhost;Database=School;
Trusted_Connection=true"/>
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency pollTime="10000" enabled="true" >
<databases>
<add connectionStringName="ConnectionString" name="School"/>
</databases>
</sqlCacheDependency>
</caching></caching>
if (Cache["Users"] == null)
{
// Create the cache dependency
SqlCacheDependency dep = new SqlCacheDependency("School", "Users");
string connectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT FirstName, LastName " +
"FROM Users", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
// put in the cache object
Cache.Insert("Users", ds, dep);
}
gvUsers.DataSource = Cache["Users"] as DataSet;
gvUsers.DataBind();
这部分代码适用于sql2000,在sql2005下没有这么麻烦.回头我们再讨论sql2005下的缓存.