如题:如果数据库数据变了自动更新缓存的内容。就像Asp.net 自带的CacheDependency.
谢谢..

解决方案 »

  1.   

    1,为 SQL 缓存依赖项启用数据库。
    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;}
      

  2.   

    我知道.Net自带的缓存依赖可以实现,但是我说的是memcached!