1.为数据库启用缓存依赖(您需要具有管理特权,或管理帐户和密码。)
在"Visual Studio 命令提示(2010(这里我用的是2010版本的,可以使用VS2005)"运行aspnet_regsql
(MSSQL 2000启用方式略有不同)
aspnet_regsql.exe -S Excel-lang -U sa -P sa -ed -d TestDB -et -t UserInfo
或者
aspnet_regsql -C "data source=Excel-lang;initial catalog=TestDB;user id=sa;password=sa" -ed -et -t "UserInfos"
注:1.数据库服务器名称为 Excel-lang 2.用户和密码为 sa sa 3.数据库和表分别为 TestDB UserInfo
运行完成后会在数据库下面产生一张表AspNet_SqlCacheTablesForChangeNotification和一个触发器
2.检查数据库是否启用监听服务
Select DataBasePropertyex('TestDB','IsBrokerEnabled')
或者
SELECT is_broker_enabled FROM sys.databases WHERE name = 'TestDB'
(1为已启用,0为未启用)
如果没有启用
使用语句ALTER DATABASE TestDB SET ENABLE_BROKER;启用监听服务
如果启用失败或者很长时间一直在执行请执行一下语句
ALTER DATABASE TestDB SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE TestDB SET ENABLE_BROKER;
3.配置WebConfig
<configuration>
<connectionStrings>节点下配置
<add name="TestDBConnectionString" connectionString="Data Source=EXCEL-LANG;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;Password=sa" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web> 节点下配置
<caching>
<sqlCacheDependency enabled="true" pollTime="1000">
<databases>
<add name="TestDB" connectionStringName="TestDBConnectionString"/>
</databases>
</sqlCacheDependency>
</caching>
各节点属性请参照MSDN相关文档
4.页面加载时执行以下代码,从数据库中获取数据并加入缓存中
List<UserInfo> list = DataCache.GetCache("UserInfoCache") as List<UserInfo>; if (list == null) { System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("TestDB", "UserInfo"); list = userManger.GetUserLoginList(); onMoveBack = new CacheItemRemovedCallback(RemovedCallback); DataCache.SetCache("UserInfoCache", list, dep, onMoveBack); }
5.申明缓存失效后所执行的函数
CacheItemRemovedCallback onMoveBack; private void RemovedCallback(String k, Object v, CacheItemRemovedReason r) { onMoveBack = new CacheItemRemovedCallback(RemovedCallback); System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("TestDB", "UserInfo"); List<UserInfo> list = userManger.GetUserLoginList();//从数据库中获取数据 DataCache.SetCache("UserInfoCache", list, dep,onMoveBack); }
6.DataCache类
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Data; using System.Data.SqlClient; using System.Web.Caching; public class DataCache { /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <returns></returns> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设置当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 设置已缓存依赖的方式缓存数据 /// </summary> /// <param name="CacheKey">键值</param> /// <param name="objObject">缓存对象</param> /// <param name="dep">缓存依赖项</param> /// <param name="onMoveCallBack">缓存项移除是触发的回调函数</param> public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep, CacheItemRemovedCallback onMoveCallBack) { 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, onMoveCallBack ); } }
7.执行步骤为从数据库查询数据→加入缓存(同时设置过期策略以及过期回调函数)→过期时→执行过期回调函数
RemovedCallback→重新查询数据并加入缓存.
缓存依赖是