memcached 分布式缓存能否监视数据库变化? 如题:如果数据库数据变了自动更新缓存的内容。就像Asp.net 自带的CacheDependency.谢谢.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1,为 SQL 缓存依赖项启用数据库。aspnet_regsql.exe -E -S localhost -A mr2.为 SQL 缓存依赖项启用表。aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees3.配置配置文件。<configuration> <appSettings/> <connectionStrings> <add name="strcodematic" connectionString="data source=127.0.0.1;initial catalog=codematic;user id=sa;password=" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <caching> <sqlCacheDependency enabled="true" pollTime="6000"> <databases> <add name="codematic" connectionStringName="strcodematic" /> </databases> </sqlCacheDependency> </caching> ... </system.web></configuration>4.在代码中使用缓存,并为其设置SqlCacheDependency依赖: /// 获取当前应用程序指定CacheKey的Cache对象值public static object GetCache(string CacheKey){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey];} /// <summary>/// 设置以缓存依赖的方式缓存数据/// </summary>/// <param name="CacheKey">索引键值</param>/// <param name="objObject">缓存对象</param>/// <param name="cacheDepen">依赖对象</param>public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert( CacheKey, objObject, dep, System.Web.Caching.Cache.NoAbsoluteExpiration,//从不过期 System.Web.Caching.Cache.NoSlidingExpiration,//禁用可调过期 System.Web.Caching.CacheItemPriority.Default, null);} protected void Page_Load(object sender, EventArgs e){ string CacheKey = "cachetest"; object objModel = GetCache(CacheKey);//从缓存中获取 if (objModel == null)//缓存里没有 { objModel = GetData();//把当前时间进行缓存 if (objModel != null) { //依赖数据库codematic中的P_Product表变化 来更新缓存 System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "P_Product"); SetCache(CacheKey, objModel, dep);//写入缓存 } } GridView1.DataSource = (DataSet)objModel; GridView1.DataBind();} //查询数据private DataSet GetData(){ string conString = "data source=127.0.0.1;initial catalog=codematic;user id=sa;password="; string strSQL = "SELECT * FROM P_Product"; SqlConnection myConnection = new SqlConnection(conString); DataSet ds = new DataSet(); myConnection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(strSQL, myConnection); adapter.Fill(ds, "Product"); myConnection.Close(); return ds;} 我知道.Net自带的缓存依赖可以实现,但是我说的是memcached! 分布式开发? .net 怎么比较日期时间的大小,差值在24小时之内 jquery遍历xml,遇到了一个浏览器兼容的问题(我感觉是) asp.net中如何使用amcharts画多条曲线? asp的隐藏控件 商品网站如何能高效的进行更新? 得到执行的SQL的问题,大家快不帮忙或学习呀!^_^ 简单的问题 新手关于DataGird的基础问题!急!!!! sql的Update语句 关于框架的问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~求解 ASP.NET用流输出文件,出现问题
aspnet_regsql.exe -E -S localhost -A mr
2.为 SQL 缓存依赖项启用表。
aspnet_regsql.exe -S <Server> -U <Username> -P <Password> -ed -d Northwind -et -t Employees
3.配置配置文件。
<configuration> <appSettings/> <connectionStrings> <add name="strcodematic" connectionString="data source=127.0.0.1;initial catalog=codematic;user id=sa;password=" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <caching> <sqlCacheDependency enabled="true" pollTime="6000"> <databases> <add name="codematic" connectionStringName="strcodematic" /> </databases> </sqlCacheDependency> </caching> ... </system.web></configuration>4.在代码中使用缓存,并为其设置SqlCacheDependency依赖:
/// 获取当前应用程序指定CacheKey的Cache对象值public static object GetCache(string CacheKey){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey];} /// <summary>/// 设置以缓存依赖的方式缓存数据/// </summary>/// <param name="CacheKey">索引键值</param>/// <param name="objObject">缓存对象</param>/// <param name="cacheDepen">依赖对象</param>public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep){ System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert( CacheKey, objObject, dep, System.Web.Caching.Cache.NoAbsoluteExpiration,//从不过期 System.Web.Caching.Cache.NoSlidingExpiration,//禁用可调过期 System.Web.Caching.CacheItemPriority.Default, null);} protected void Page_Load(object sender, EventArgs e){ string CacheKey = "cachetest"; object objModel = GetCache(CacheKey);//从缓存中获取 if (objModel == null)//缓存里没有 { objModel = GetData();//把当前时间进行缓存 if (objModel != null) { //依赖数据库codematic中的P_Product表变化 来更新缓存 System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "P_Product"); SetCache(CacheKey, objModel, dep);//写入缓存 } } GridView1.DataSource = (DataSet)objModel; GridView1.DataBind();} //查询数据private DataSet GetData(){ string conString = "data source=127.0.0.1;initial catalog=codematic;user id=sa;password="; string strSQL = "SELECT * FROM P_Product"; SqlConnection myConnection = new SqlConnection(conString); DataSet ds = new DataSet(); myConnection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(strSQL, myConnection); adapter.Fill(ds, "Product"); myConnection.Close(); return ds;}