关于数据库依赖缓存的SqlCacheDependency的demo
2009-07-29 08:38
766 查看
对于数据库依赖缓存既从数据库中取出表中的数据放入缓存中待遇到对表中的数据进行增删改的时候在重新更新缓存(郁闷的是.net好像是直接把原来放表的缓存直接给删掉了)!在数据查询量比较大的时候(例如涉及几个表的联合查询的情况)数据库依赖缓存还是比较有价值的东西的!
首先先要为数据库启用缓存依赖项执行下属命令
protected void Page_Load(object sender, EventArgs e)
{
//判断缓存中是否存在Category
if (HttpRuntime.Cache["Category"] == null)//不存在则
{
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
//对GridView1 进行数据绑定
System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "Category");
//声明SqlCacheDependency其中构造函数中的这两个参数(codematic必需与WebConfig配置的sqlCacheDependency的一致,Category则是缓存的key)
HttpRuntime.Cache.Insert("Category", DateTime.Now.ToString(),
dep,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
null);
//向缓存集合中插入数据(为了直观我们在缓存中放入的是当前时间当更改表Category时则重新更改缓存中的时间)
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
//显示缓存Category中的数据
}
else//存在则把缓存数据显示到页面
{
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
}
}
protected void Button_Click(Object sender, EventArgs e)
{
InsertData();// 修改Category表中的数据
this.Label1.Text = HttpRuntime.Cache["Category"].ToString(); //显示缓存中的内容
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
}
private SqlCommand returncommand(string CommandText)
{
/* 连接数据库*/
string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["strcodematic"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(connstring);
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = CommandText;
return cmd;
}
}
private DataSet BindData()
{
/* 读出表中的数据*/
SqlCommand cmd=returncommand("SELECT * FROM Category");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
private string GetData()
{
/* 筛选出CategoryID=6 的 Description值*/
SqlCommand cmd = returncommand("SELECT Description FROM Category where CategoryID=6");
return cmd.ExecuteScalar().ToString();
}
private void InsertData()
{
/* 更新表内容*/
if (GetData() != "222")
{
SqlCommand cmd = returncommand("Update Category set Description=222 where CategoryID=6");
cmd.ExecuteNonQuery();
}
} 当对数据表中的数据不进行更新时刷新页面页面中的时间是不会变的
但点击插入对数据库进行更新时候则重新写缓存缓存中写入的是当前时间!!
首先先要为数据库启用缓存依赖项执行下属命令
protected void Page_Load(object sender, EventArgs e)
{
//判断缓存中是否存在Category
if (HttpRuntime.Cache["Category"] == null)//不存在则
{
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
//对GridView1 进行数据绑定
System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "Category");
//声明SqlCacheDependency其中构造函数中的这两个参数(codematic必需与WebConfig配置的sqlCacheDependency的一致,Category则是缓存的key)
HttpRuntime.Cache.Insert("Category", DateTime.Now.ToString(),
dep,
System.Web.Caching.Cache.NoAbsoluteExpiration,
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Default,
null);
//向缓存集合中插入数据(为了直观我们在缓存中放入的是当前时间当更改表Category时则重新更改缓存中的时间)
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
//显示缓存Category中的数据
}
else//存在则把缓存数据显示到页面
{
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
this.Label1.Text = HttpRuntime.Cache["Category"].ToString();
}
}
protected void Button_Click(Object sender, EventArgs e)
{
InsertData();// 修改Category表中的数据
this.Label1.Text = HttpRuntime.Cache["Category"].ToString(); //显示缓存中的内容
this.GridView1.DataSource = BindData();
this.GridView1.DataBind();
}
private SqlCommand returncommand(string CommandText)
{
/* 连接数据库*/
string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["strcodematic"].ConnectionString.ToString();
SqlConnection conn = new SqlConnection(connstring);
using (SqlCommand cmd = new SqlCommand())
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = CommandText;
return cmd;
}
}
private DataSet BindData()
{
/* 读出表中的数据*/
SqlCommand cmd=returncommand("SELECT * FROM Category");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
private string GetData()
{
/* 筛选出CategoryID=6 的 Description值*/
SqlCommand cmd = returncommand("SELECT Description FROM Category where CategoryID=6");
return cmd.ExecuteScalar().ToString();
}
private void InsertData()
{
/* 更新表内容*/
if (GetData() != "222")
{
SqlCommand cmd = returncommand("Update Category set Description=222 where CategoryID=6");
cmd.ExecuteNonQuery();
}
} 当对数据表中的数据不进行更新时刷新页面页面中的时间是不会变的
但点击插入对数据库进行更新时候则重新写缓存缓存中写入的是当前时间!!
相关文章推荐
- 关于数据库依赖缓存的SqlCacheDependency的demo<转>
- 使用SqlCacheDependency依赖项让数据库变化后缓存失效
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- ASP.NET 缓存 SqlCacheDependency 监视数据库表变化 让缓存更新的更及时更提高节能
- cache应用(asp.net 2.0 SQL数据缓存依赖 [SqlCacheDependency ] )
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ] -- [转]
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- \t\t【总结】ASP.NET 缓存 SqlCacheDependency 监视数据库表变化 让缓存更新的更及时更提高节能
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- sql SqlCacheDependency缓存依赖
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- sqlCachedependency【数据缓存依赖】 的使用
- SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
- asp.net Sql缓存依赖(SqlCacheDependency)简介
- asp.net mvc5中使用缓存依赖SqlCacheDependency