MVC缓存(二)
依赖缓存:
1.监视特定的数据库表,当数据库表里数据发生变化时,自动删除缓存项,并向Cache中添加新的项。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MvcCache.Control.Controllers 8 { 9 public class SqlDependencyController : Controller 10 { 11 [OutputCache(CacheProfile = "SqlDependencyCache")] 12 public ActionResult Index() 13 { 14 ViewBag.CurrentTime = System.DateTime.Now; 15 return View(); 16 } 17 18 } 19 }
2.CacheProfile指向配置文件中name为SqlDependencyCache的缓存配置,然后配置文件中的配置缓存对数据库的依赖。
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 For more information on how to configure your ASP.NET application, please visit 4 http://go.microsoft.com/fwlink/?LinkId=169433 5 --> 6 7 <configuration> 8 <!--数据库连接字符串--> 9 <connectionStrings> 10 <add name="Conn" connectionString="server=localhost;database=wcfDemo;uid=sa;pwd=123456;" providerName="System.Data.SqlClient"/> 11 </connectionStrings> 12 <!--数据库连接字符串--> 13 <appSettings> 14 <add key="webpages:Version" value="2.0.0.0" /> 15 <add key="webpages:Enabled" value="false" /> 16 <add key="PreserveLoginUrl" value="true" /> 17 <add key="ClientValidationEnabled" value="true" /> 18 <add key="UnobtrusiveJavaScriptEnabled" value="true" /> 19 </appSettings> 20 <system.web> 21 <!--配置缓存--> 22 <caching> 23 <sqlCacheDependency><!--缓存的数据库依赖节--> 24 <databases> 25 <add name="UserCacheDependency" connectionStringName="Conn" pollTime="500"/><!--Conn:数据库连接字符串的名称,name随便启名,缓存节会用到--> 26 </databases> 27 </sqlCacheDependency> 28 <outputCacheSettings> 29 <outputCacheProfiles> 30 <add name="SqlDependencyCache" duration="3600" sqlDependency="UserCacheDependency:user"/><!--UserCacheDependency:数据库依赖配置节的名称,user:数据库中需要监听的表名称--> 31 </outputCacheProfiles> 32 </outputCacheSettings> 33 </caching> 34 <!--配置缓存--> 35 <httpRuntime targetFramework="4.5" /> 36 <compilation debug="true" targetFramework="4.5" /> 37 <pages> 38 <namespaces> 39 <add namespace="System.Web.Helpers" /> 40 <add namespace="System.Web.Mvc" /> 41 <add namespace="System.Web.Mvc.Ajax" /> 42 <add namespace="System.Web.Mvc.Html" /> 43 <add namespace="System.Web.Routing" /> 44 <add namespace="System.Web.WebPages" /> 45 </namespaces> 46 </pages> 47 </system.web> 48 <system.webServer> 49 <validation validateIntegratedModeConfiguration="false" /> 50 51 <handlers> 52 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" /> 53 <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" /> 54 <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 55 <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" /> 56 <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" /> 57 <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> 58 </handlers> 59 </system.webServer> 60 61 </configuration>
3.启用该数据库表的缓存依赖通知功能
打开vs命令工具行,输入:aspnet_regsql -S localhost -U sa -P 123456 -ed -d wcfDemo -et -t user
-S localhost:数据库地址
-U sa:数据库登录名
-P 123456:数据库登录密码
-d wcfDemo:数据库的名称
-t user:表名称(小写)