SQL缓存依赖

SQL缓存依赖

SQL缓存依赖 

SQL缓存依赖配置

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe

为数据库配置

aspnet_regsql.exe -S .\sqlexpress -E -d CacheTest -ed

-S:数据库实例名

-E:windows验证

-d:数据库名

为数据表配置

aspnet_regsql.exe -S .\sqlexpress -E -d CacheTest -ed -t Student -et

-t:数据库表名

WebConfig.web文件中配置 

< appSettings >
     
< add  key ="Cache"  value ="TableCacheDependency" />
    
< add   key ="dbname"  value ="mySqlCache" />
    
< add  key ="tbStrs"  value ="Student|Teacher" />
 
</ appSettings >
< system.web >
      
< caching >
      
<!-- 页面缓存配置 -->
      
< outputCache   enableOutputCache ="true" />
      
< outputCacheSettings >
        
< outputCacheProfiles >
          
< add  name ="myCache"  enabled ="true"  duration ="10" />
        
</ outputCacheProfiles >
      
</ outputCacheSettings >
      
<!-- -SQL缓存依赖配置 -->
      
< sqlCacheDependency  enabled ="true"  pollTime ="1000" >
        
< databases >
          
< add  name ="mySqlCache"  connectionStringName ="CacheTestConnectionString"   />
        
</ databases >
      
</ sqlCacheDependency >
    
</ caching >

</system.web> 

 

 接口类库:ICacheDependency

实现类库:TableCacheDependency

工厂类库:CacheDependencyFactory

一、           接口类库:IcacheDependency

ICacheDependency.cs 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Web.Caching;

namespace  ICacheDependency
{
    
public   interface  ICacheDependency
    {
        
/* 1、AggregateCacheDependency(聚合缓存依赖) 类监视依赖项对象的集合,以便在任何依赖项对象更改时,
          该缓存项都会自动移除。数组中的对象可以是 CacheDependency 对象、SqlCacheDependency 对象、
          从 CacheDependency 派生的自定义对象或这些对象的任意组合。
          当需要缓存的记录集来自多个表时,就需要使用 AggregateCacheDependency对象,
         2、AggregateCacheDependency 确保,当保存集合内任意一对象(CacheDependency)失效时,使它关联的缓存数据也失效果,
          同时也会使AggregateCacheDependency内所有的对象失效
         
*/
        AggregateCacheDependency GetDependency();
    }

} 

 

二、实现类库:TableCacheDependency 

 TableCacheDependency.cs

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Web.Caching;
using  System.Configuration;
namespace  TableCacheDependency
{
    
public   class  TableCacheDependency : ICacheDependency.ICacheDependency
    {
        
public  AggregateCacheDependency GetDependency()
        {
            AggregateCacheDependency dep 
=   new  AggregateCacheDependency();

            
// 获取dbname
             string  dbname  =  ConfigurationSettings.AppSettings[ " dbname " ];

            
// 获取数据表字符串,并转换成数组
             string  tbStrs  =  ConfigurationSettings.AppSettings[ " tbStrs " ];
            
string [] tables  =  tbStrs.Split( ' | ' );

            
// 遍历数组,创建SqlCacheDependency,并添加到dep
             foreach  ( string  table  in  tables)
            {
                dep.Add(
new  SqlCacheDependency(dbname, table));
            }

            
return  dep;
        }
    }
}

 

三、工厂类库:CacheDependencyFactory 

 Access.cs(工厂类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace CacheDependencyFactory
{
    
public class Access
    {
        
//WebConfig中存储的是实现类的包名TableCacheDependency
        private static readonly string path = ConfigurationSettings.AppSettings["Cache"];
        
public static ICacheDependency.ICacheDependency Create()
        {
            
string className = path + ".TableCacheDependency";//包名+类名
            return (ICacheDependency.ICacheDependency)Assembly.Load(path).CreateInstance(className);
        }
    }
}

 

Facode.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;

namespace CacheDependencyFactory
{
    
public class Facode
    {
        
public static AggregateCacheDependency GetDependency()
        {
            
return Access.Create().GetDependency();
        }
    }

}

 

MySqlCache.aspx.cs 

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.Caching;

namespace  WebApplication1
{
    
public   partial   class  MySqlCache : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
                ShowTime();

        }

        
protected   void  Button1_Click( object  sender, EventArgs e)
        {
            ShowTime();

        }
        
protected   void  ShowTime()
        {
            
//
             string  key  =   " TimeNow " ;

            
// 试图从缓存中获取时间
             string  time  = ( string )HttpRuntime.Cache[key];

            
if  ( string .IsNullOrEmpty(time))
            {
                
// 如果获取失败
                
// 重新获取当前时间
                time  =  DateTime.Now.ToString();
                
                
// 调用工厂类库中的缓存依赖,并插入缓存
                Cache.Insert(key, time, CacheDependencyFactory.Facode.GetDependency());

            }
            label1.Text 
=  time;
             
 
        }
    }
}

 

你可能感兴趣的:(sql)