SqlCacheDependency Application

代码
缓存设计:
         SqlCacheDependency类在所有受支持的 SQL Server 版本 (
7.020002005) 上监视特定的 SQL Server 数据库表,以便在该表发生更改时,自动从 Cache 中删除与该表关联的项。SqlCacheDependency 类向应用程序的 Cache 添加依赖于 SQL Server 数据库表或 SQL 查询的项.
      建立连接对象,再创建一个SqlCommand实例,创建SqlCacheDependency实例,在这步之后再调用Command对象来获取数据(这个顺序很重要)。之后调用Cache的Insert语句建立一个依赖于一个具体查询数据集的Cache项。
Example Code:
     
public List<Bookmark> GetBookmarkByUserId(string userId, SqlCacheDependency dependency)
        {
            List
<Bookmark> bookmarks = new List<Bookmark>();
            
string cmdText=string.Format ("select Id,Name,Url,FolderId,Type from dbo.Bookmark where UserId='{0}'",userId);

            
using (SqlConnection con = new SqlConnection(“connectionString”))
            {
                con.Open();
                SqlCommand cmd 
= new SqlCommand(cmdText);
                dependency 
= new SqlCacheDependency(cmd);//添加sql缓存依赖 SqlCacheDependency数据库表发生更改时,将自动删除缓存项。

                
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    
while (dr.Read())
                    {
                        Bookmark mark 
= new Bookmark()
                        {
                            Id 
= dr.GetString(0),
                            Name 
= dr.GetString(1),
                            Url 
= dr.GetString(2),
                            Type 
= dr.GetString(4)
                        };

                        bookmarks.Add(mark);
                    }
                }
            }

            
return bookmarks;
           
        }

        
public List<Bookmark> GetBookamrks(string userId, int timeout)
        {

            List
<Bookmark> bookmarks = new List<Bookmark>();

            
string key = "userId_" + userId;
            bookmarks 
= (List<Bookmark>)HttpRuntime.Cache[key];

            
// Check if the data exists in the data cache
            if (bookmarks == null)
            {
                SqlCacheDependency dependency 
= null;
                
// If the data is not in the cache then fetch the data from the business logic tier
                bookmarks = GetBookmarkByUserId(userId,dependency);
                TimeSpan slidingExpiration 
= TimeSpan.FromHours(timeout);
                
// Store the output in the data cache, and Add the necessary SqlCacheDependency object
                
//绝对到期
                
// HttpRuntime.Cache.Add(key, bookmarks, dependency, DateTime.Now.AddHours(timeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                
//滑动过期
                CacheItemRemovedCallback  callBack = new CacheItemRemovedCallback(onRemove);

                HttpRuntime.Cache.Add(key, bookmarks, dependency, Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriority.High, callBack);
            }

            
return bookmarks;
        }
    
//移除Bookmark时,同时移除Version
 public void onRemove(string key, object val, CacheItemRemovedReason reason)
        {
            
if (HttpRuntime.Cache[key + "version"!= null)
            {
                HttpRuntime.Cache.Remove(key 
+ "version");
            }
        }
    

原文地址:https://www.cnblogs.com/hubcarl/p/1728604.html