SqlCacheDependency Application

ExpandedBlockStart.gif 代码
缓存设计:
         SqlCacheDependency类在所有受支持的 SQL Server 版本 (
7.0 2000 2005 ) 上监视特定的 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/archive/2010/05/06/1728604.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值