g key, bool useDependency)
{
if (!this.CheckParameters(key)) return;
if (useDependency)
{
this.UpdateCacheDependency(key);
}
else
{
base.Remove(key, useDependency);
}
}
}
public class WebCacheFactory
{
public static IWebCache GetWebCache()
{
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
return new SqlDependencyWebCache(connectionString);
}
}
}
Code
<%@ Application Language="C#" %>
<script runat="server">
private string GetConnectionString()
{
return "Server=.;User ID=sa;Password=sa;database=TestDB;Connection Timeout=30";
}
void Application_Start(object sender, EventArgs e)
{
System.Data.SqlClient.SqlDependency.Start(this.GetConnectionString());
}
void Application_End(object sender, EventArgs e)
{
System.Data.SqlClient.SqlDependency.Stop(this.GetConnectionString());
}
</script>
注1:数据库中新建一个CacheDependency的表,主键为CacheKey varchar(50),另一个字段为Flag bit。缓存项依赖与CacheDependency中对应的一行。
注2:数据库中需要启动Broker Service。命令为:alter database [dbname] set enable_broker (需要断开其他的数据库连接)。
注3:在Applicantion Start和End的时候需要开启和停止SqlDependency。
结束语:
缓存的