似是而非

导航

【转】系统缓存全解析二:动态缓存(3)-文件,对象缓存依赖与数据库缓存依赖

(依据文件,数据库,表等变化而自动更新缓存内容)


一,缓存文件:


      
简单语言介绍以下,将硬盘中的文本文件缓存至web服务器内存中,一般常用在缓存配置文件或其他公共文件上,并自动监听文件是否有被修改,一旦被修改,将自动的更新缓存。因为与下面的数据库缓存依赖基本代码一致,所以不加代码示例。


二,缓存非序列化对象或者序列化对象:


不用多说,就是将对象放入缓存内啦。


三,数据库缓存依赖项:


为了实现.NET缓存依赖需要调用System.Web.Caching下的三个类:


1,Cache类 ;//将数据缓存到该对象内;


2,SqlCacheDependency类;//主要负责为Cache对象 与 数据库表或结果集合建立关系的类


3,SqlCacheDependencyAdmin类;//可忽略不用,以后总结


以及System.Data.SqlClient下的两个类:


1,System.Data.SqlClient.SqlDependency   //开启或关闭对于数据库的监听


以及开启MSSQL内某一数据库的消息传递服务:


           alter database Common set enable_broker  
//开启


           alter database Common set disable_broker //关闭


 


下面是详细的。NET针对MSSQL缓存依赖项的例程:


using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using System.Web.Caching;
using
System.Collections;
using System.Data.SqlClient;
using
System.Data;
using System.Xml;
using
System.Collections.ObjectModel;
using System.Web;


namespace HOSoft.Herp4.AutoTest.Common.Data
{
    public sealed
class CacheProvider<T>
    {
        #region 全局变量
        public
static Cache HerpCache = HttpRuntime.Cache;
        public static
CacheItemRemovedCallback Callback = new
CacheItemRemovedCallback(CacheProvider<T>.RemovedItemCallBack);
       
public static TimeSpan timespan;
        #endregion
       
       
//#region 静态构造函数
        //static CacheProvider()
        //{
       
//    HerpCache = new Cache();
        //}
       
//#endregion



        public static void StartSqlDependency(Collection<string>
StrConnection)
        {
           
            if
(StrConnection.Count > 0)
            {
                foreach (string
str in StrConnection)
                {
                   
SqlDependency.Start(str);
                }
            }
        }



        public static void StopSqlDependency(Collection<string>
StrConnection)
        {
            if (StrConnection.Count >
0)
            {
                foreach (string str in
StrConnection)
                {
                   
SqlDependency.Stop(str);
                }
               
           
}
        }



        public static SqlCacheDependency
CreateSqlCacheDependency(SqlCommand Sqlcommand)
        {
            if
(Sqlcommand.GetHashCode() != 0)
            {
               
SqlCacheDependency SCacheDependency = new
SqlCacheDependency(Sqlcommand);
                return
SCacheDependency;
            }
            else
           
{
                return null;
            }
        }


 


        public static bool AddItemInCache(string DataName, T DataValue,
SqlCommand Command,String DataBase_Name)
        {
            if
(!string.IsNullOrEmpty(DataName) && DataValue.GetHashCode() !=
0)
            {
                if (HerpCache[DataName] ==
null)
                {
                    switch
(DataBase_Name.Trim().ToLower())
                   
{
                        case "common": timespan = new TimeSpan(24, 0,
0);
                            break;


                        case "house":timespan = new TimeSpan(24, 0,
0);
                            break;


                        case "oa":timespan = new TimeSpan(12, 0,
0);
                            break;


                        case "report":timespan = new TimeSpan(24, 0,
0);
                            break;
                        default:
timespan = new TimeSpan(6, 0, 0);
                            break;


                    }
                    HerpCache.Add(DataName,
DataValue, CreateSqlCacheDependency(Command), Cache.NoAbsoluteExpiration,
timespan, CacheItemPriority.High, Callback);


                    return true;
                }
               
else
                return false;
            }
           
else
            return false;
        }


        public static bool RemoveItemFromCache(string DataName)
       
{
            if (string.IsNullOrEmpty(DataName))
           
{
                if (HerpCache[DataName] != null)
               
{
                    HerpCache.Remove(DataName);
                   
return true;


                }
                else return false;
           
}
            return false;
        }


        public static void RemovedItemCallBack(string key,Object
value,CacheItemRemovedReason reason)
        {
        }


    }
}

 

引用地址:http://hi.baidu.com/jiangyangw3r/item/4290ff0235a2d112cd34ea6c

posted on 2012-07-13 16:57  似是而非  阅读(280)  评论(0编辑  收藏  举报