很多时候,我们服务器的性能瓶颈会是在查询数据库的时候,所以对数据库的缓存非常重要,那么有没有一种方法,可以实现SQL SERVER数据库的缓存,当数据表没有更新时,就从缓存中读取,当有更新的时候,才从数据表中读取呢,答案是肯定的,这样的话我们对一些常用的基础数据表就可以缓存起来,比如做新闻系统的新闻类别等,每次就不需要从数据库中读取了,加快网站的访问速度。
那么如何开启SQLSERVER数据库缓存依赖,方法如下: 第一步:修改Web.Config的<system.web>节的配置,代码如下,让网站项目启用SqlCacheDependency。注意下面代码中的connectionStringName,就是指定的<connectionStrings>节中的数据库连接字符串变量名称。name则是为该SqlCacheDependency起的名字,这个名字将在第三步中用到。SqlCacheDependency类会自动完成对此配置节信息的读取以建立和数据库之间的联系。
view source
print?
01. < system.web > 02. < httpHandlers > 03. < add verb = "*" path = "*.aspx" 04. type = "URLRewriter.RewriterFactoryHandler, URLRewriter" /> 05. < add verb = "*" path = "*.shtml" 06. type = "URLRewriter.RewriterFactoryHandler, URLRewriter" /> 07. < add verb = "*" path = "*.bobo" 08. type = "URLRewriter.RewriterFactoryHandler, URLRewriter" /> 09. 10. </ httpHandlers > 11. 12. < caching > 13. < sqlCacheDependency enabled = "true" pollTime = "6000" > 14. < databases > 15. < add name = "YD_JWC_JAKE" connectionStringName = "cachestr" /> 16. </ databases > 17. </ sqlCacheDependency > 18. </ caching > 19. <!-- 20. 设置 compilation debug="true" 将调试符号插入 21. 已< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=%B1%E0%D2%EB" >编译</ SPAN >的< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=%D2%B3%C3%E6" >页面</ SPAN >中。但由于这会 22. 影响性能,因此只在< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=%BF%AA%B7%A2" >开发</ SPAN >过程中将此值 23. 设置为 true。 24. --> 25. < compilation debug = "true" > 26. < assemblies > 27. < add assembly = "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /> 28. </ assemblies > 29. </ compilation > 30. <!-- 31. 通过 < authentication > 节可以配置 < SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=ASP" >ASP</ SPAN >.< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=NET" >NET</ SPAN > 使用的 32. 安全身份验证< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=%C4%A3%CA%BD" >模式</ SPAN >, 33. 以标识传入的< SPAN class = t_tag onclick = tagshow (event) href = "tag.php?name=%D3%C3%BB%A7" >用户</ SPAN >。 34. --> 35. < authentication mode = "Forms" > 36. < forms loginUrl = "login.aspx" name = ".AJSUPCXAIUTH" ></ forms > 37. </ authentication > 38. < authorization > 39. < allow users = "*" /> 40. </ authorization > 41. <!-- 42. 如果在执行请求的过程中出现未处理的错误, 43. 则通过 < customErrors > 节可以配置相应的处理步骤。具体说来, 44. 开发人员通过该节可以配置 45. 要显示的 html 错误页 46. 以代替错误堆栈跟踪。--> 47. < customErrors mode = "RemoteOnly" defaultRedirect = "/ER3.shtml" > 48. < error statusCode = "403" redirect = "/ER1.shtml" /> 49. < error statusCode = "404" redirect = "/ER404.shtml" /> 50. </ customErrors > 51. </ system.web > 第二步:在CMD中执行下述命令,以开启SQL SERVER数据库对SqlCacheDependency的支持,利用aspnet_regsql.exe工具,该工具位于windows/microsoft.net/framework/[版本]文件夹中
- aspnet_regsql -C "data source=127.0.0.1;initial catalog=YD_JWC_JAKE;user id=sa;password=" -ed -et -t "T_NewsClass"
复制代码参数-C后面跟着的是数据库连接字符串,注意字母C是大写。参数-t后面跟着的就是你要开启数据库缓存的数据表,此处我为新闻类别的表开启了缓存依赖。(如果有多个表,则重复执行此命令,注意修改你的数据表名)
第三步:在获取数据的业务层代码中,如果是第一次读取,则从数据库中读取后,存入缓存里。以后获取数据时,数据库会自动判断表是否有更新数据,如果有,则读数据库同时更新缓存,如果没有更新,则从数据库中读取。代码如下:
view source
print?
01. private void getInfoClass( int t) 02. { 03. string CacheKey = "cacheclass" + t.ToString(); 04. object objModle = Jake.DataCache.GetCache(CacheKey); 05. DataTable dt= null ; 06. if (objModle == null ) 07. { 08. Jake.BLL.NewsManage.NewsClass nc = new Jake.BLL.NewsManage.NewsClass(); 09. dt = nc.GetList( "" ).Tables[0]; 10. objModle = dt; 11. if (objModle != null ) 12. { 13. System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency( "YD_JWC_JAKE" , "T_NewsClass" ); 14. Jake.DataCache.SetCache(CacheKey, objModle, dep); 15. } 16. } 17. else 18. { 19. dt = (DataTable)objModle; 20. } 21. DataRow[] drs = dt.Select( "" , "classid" ); 22. StringBuilder sb = new StringBuilder(); 23. sb.Append( "<ul>" ); 24. foreach (DataRow r in drs) 25. { 26. string cid=r[ "ClassId" ].ToString(); 27. Security js = new Security(); 28. string decrystr = Jake.Common.ConfigHelper.GetConfigString( "DecryStr" ); 29. cid = js.EncryptQueryString(cid, decrystr); 30. string cdesc=r[ "ClassDesc" ].ToString(); 31. if (t == 1) 32. { 33. sb.Append( "<li><a href=" /Info " + cid + " .shtml " mce_href=" Info " + cid + " .shtml "><span class='fontbold'>" + cdesc + "</span></a></li>" ); 34. } 35. else if (t == 2) 36. { 37. sb.Append( "<li><a href=" /File " + cid +" .shtml " mce_href=" File " + cid +" .shtml "><span class='fontbold'>" + cdesc + "</span></a></li>" ); 38. } 39. else 40. sb.Append( "<li><a href=" /FAQ " + cid + " .shtml " mce_href=" FAQ " + cid + " .shtml "><span class='fontbold'>" + cdesc + "</span></a></li>" ); 41. } 42. sb.Append( "</ul>" ); 43. Response.Write(sb); 44. } 以上代码中Jake.DataCache.GetCache()方法是自己定义的一个获取和设置缓存的通用方法,单独编译成了DLL:view sourceprint?01. using System; 02. using System.Collections.Generic; 03. using System.Web; 04. using System.Text; 05. 06. namespace Jake 07. { 08. public class DataCache 09. { 10. /// <summary> 11. /// 获取当前<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D3%A6%D3%C3">应用</SPAN><SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%B3%CC%D0%F2">程序</SPAN>指定CacheKey的Cache值 12. /// </summary> 13. /// <param name="CacheKey"></param> 14. /// <returns></returns> 15. public static object GetCache( string CacheKey) 16. { 17. System.Web.Caching.Cache objCache = HttpRuntime.Cache; 18. return objCache[CacheKey]; 19. } 20. 21. /// <summary> 22. /// 设置当前应用程序指定CacheKey的Cache值 23. /// </summary> 24. /// <param name="CacheKey"></param> 25. /// <param name="objObject"></param> 26. public static void SetCache( string CacheKey, object objObject) 27. { 28. System.Web.Caching.Cache objCache = HttpRuntime.Cache; 29. objCache.Insert(CacheKey, objObject); 30. } 31. /// <summary> 32. /// 设置已缓存依赖的方式缓存数据 33. /// </summary> 34. /// <param name="CacheKey">键值</param> 35. /// <param name="objObject">缓存<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%B6%D4%CF%F3">对象</SPAN></param> 36. /// <param name="dep">缓存依赖项</param> 37. public static void SetCache( string CacheKey, object objObject, System.Web.Caching.CacheDependency dep) 38. { 39. System.Web.Caching.Cache objCache = HttpRuntime.Cache; 40. objCache.Insert( 41. CacheKey, 42. objObject, 43. dep, 44. System.Web.Caching.Cache.NoAbsoluteExpiration, 45. System.Web.Caching.Cache.NoSlidingExpiration, 46. System.Web.Caching.CacheItemPriority.Default, 47. null 48. ); 49. } 50. } 51. }
|