MVC缓存(二)
2024-10-20 13:28:12
依赖缓存:
1.监视特定的数据库表,当数据库表里数据发生变化时,自动删除缓存项,并向Cache中添加新的项。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcCache.Control.Controllers
{
public class SqlDependencyController : Controller
{
[OutputCache(CacheProfile = "SqlDependencyCache")]
public ActionResult Index()
{
ViewBag.CurrentTime = System.DateTime.Now;
return View();
} }
}
2.CacheProfile指向配置文件中name为SqlDependencyCache的缓存配置,然后配置文件中的配置缓存对数据库的依赖。
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<!--数据库连接字符串-->
<connectionStrings>
<add name="Conn" connectionString="server=localhost;database=wcfDemo;uid=sa;pwd=123456;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<!--数据库连接字符串-->
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<!--配置缓存-->
<caching>
<sqlCacheDependency><!--缓存的数据库依赖节-->
<databases>
<add name="UserCacheDependency" connectionStringName="Conn" pollTime="500"/><!--Conn:数据库连接字符串的名称,name随便启名,缓存节会用到-->
</databases>
</sqlCacheDependency>
<outputCacheSettings>
<outputCacheProfiles>
<add name="SqlDependencyCache" duration="3600" sqlDependency="UserCacheDependency:user"/><!--UserCacheDependency:数据库依赖配置节的名称,user:数据库中需要监听的表名称-->
</outputCacheProfiles>
</outputCacheSettings>
</caching>
<!--配置缓存-->
<httpRuntime targetFramework="4.5" />
<compilation debug="true" targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" /> <handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<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" />
<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" />
<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" />
</handlers>
</system.webServer> </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:表名称(小写)
最新文章
- MySql学习(二) —— where / having / group by / order by / limit 简单查询
- Git使用命令
- poj 2060 Taxi Cab Scheme (二分匹配)
- Exchange 2013 、Lync 2013、SharePoint 2013
- fiddler 代理不成功调试
- CSS3之边框样式(动画过渡)
- oracle里面的时间转字符串to_char(),字符串转时间to_date(),以及substr和instr的使用。
- JavaScript的闭包特性
- 浅谈JS的继承
- Android忽略文件以及.gitignore规则不生效的可行解决方案
- PAT1116. Come on! Let's C (map)
- 项目中PO、PM的职责区分
- 【转载】ASP.NET实现文件下载的功能
- bat处理快速安装jdk脚本
- tensorflow 的tf.split函数的用法
- django学习网站
- 让AutoMapper在你的项目里飞一会儿
- Day6 jQuery
- 初始CSS模板
- C#高级编程(第六版)学习:第三十一章:Windows窗体
热门文章
- django drf 权限permission
- log4net 日志打印不全
- Modular Arithmetic ( Arithmetic and Algebra) CGAL 4.13 -User Manual
- sharepoint 2007 update sharepoint 2013 found old privillege not availabel
- 为何会有Python学习计划
- 封装继承多态-java面向对象三大特征
- 0基础浅谈反射型xss(2)
- canvas绘制简单的霓虹灯效果
- 工作中遇到的两个问题-正则以及console
- linux安装scala环境