[OutputCache(Duration =20)]//设置过期时间为20秒 public ActionResult ExampleCache() { var timeStr =DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒"); ViewBag.timeStr = timeStr; return View(); }
int Durrtion | 获取或设置缓存持续时间(以秒为单位) |
bool NoStore | 是否存储缓存,默认是false,当为true时http状态码就会变成200 |
string VaryByParam | 获取或设置参数变化的值。不同的参数都会被缓存不同的文档,多个参数用逗号隔开,none、*(空、所有) |
string CacheProfile | 获取或设置缓存配置文件名称,也就是说在配置文件中设置缓存 |
string VaryByCustom | 获取或设置基于自定义项变化的值,自定义任何输出缓存的文字,比较常用 |
Location | 枚举值 None不缓存,Server缓存在服务器端,Client缓存在浏览器,Any在浏览器、代理服务器、web服务器 |
string sqlDependency | 获取或设置 SQL 依赖项,根据数据库的变化更新缓存 |
<!---CacheProfile配置文件中设置缓存--> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="exampleCacheProfile" duration="20" location="Any" enabled="true"/> </outputCacheProfiles> </outputCacheSettings> </caching> <!---CacheProfile配置文件中设置缓存end-->
[OutputCache(Duration =20,VaryByParam ="type,page")] public ActionResult ExampleVaryByParam(string type,int page) { ViewBag.type = type; ViewBag.page= page; return View(); }
public override string GetVaryByCustomString(HttpContext context, string custom) { //custom 是Outputcache中的参数VaryByCustom参数值 ,VaryByCustome为*代指所有参数 if (custom == "type") { return context.Request.AnonymousID; //返回的这个字符串会与当前Action的缓存比较,字符串不一致则更新缓存。 } return base.GetVaryByCustomString(context, custom); }
[OutputCache(VaryByCustom ="type",VaryByParam ="type",Duration =20)] public ActionResult ExampleVaryByCustom(string type) { ViewBag.type = type; return View(); }
首先我们还是来学习一下如何启用数据库缓存依赖项。
运行cmd命令: cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
运行相关命令: aspnet_regsql -S . -U sa -P 123456 -ed -d 数据库名 -et -t 表名
解释一下
注册:aspnet_regsql -S . -E -ed -d 数据库名 -et -t 表名 删除:aspnet_regsql -S . -E -d 数据库名 -dt -t 表名 取消数据库缓存依赖: aspnet_regsql -S . -E -dd 数据库名数据库名 列出已注册表:aspnet_regsql -S . -E -d 数据库名 -lt
如图:
<!---CacheProfile配置文件中设置缓存--> <caching> <!--缓存的数据库依赖--> <sqlCacheDependency> <databases> <add name="CacheDependencyProduct" connectionStringName="Conn" pollTime="1000"/> <!--name:配置sqlDependency会用到,connectionStringName:数据库连接字符串的名称,pollTime:监听数据库变化的间隔时间,以毫秒为单位。即每隔1秒钟去监听数据库中的表Product是否有变化--> </databases> </sqlCacheDependency> <outputCacheSettings> <outputCacheProfiles> <!--<add name="exampleCacheProfile" duration="100" location="Any" enabled="true"/>--> <add name="exampleSqlDependency" duration="20" location="Any" enabled="true" sqlDependency="CacheDependencyProduct:Product"/> <!--sqlDenpendency:数据依赖节点名称+冒号+数据库表名(区分大小写)--> </outputCacheProfiles> </outputCacheSettings> </caching> <!---CacheProfile配置文件中设置缓存end-->
当然还有数据库连接字符串,还是贴出来吧
<code class="language-html"> <connectionStrings> <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-webapiToken-20170209085002.mdf;Initial Catalog=aspnet-webapiToken-20170209085002;Integrated Security=True" providerName="System.Data.SqlClient" />--> <add name="Conn" connectionString="server=.;database=Monitor;uid=sa;pwd=123456" providerName="System.Data.SqlClient"/> </connectionStrings>
为了看到效果可以在在视图中加个时间
[OutputCache(CacheProfile = "exampleSqlDependency")] public ActionResult ExampleSqlDependency() { ViewBag.timeStr = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒"); return View(); }
所实现的效果是:数据库的Product表数据没有任何变化时,20秒的缓存持续时间内,这个时间是不会变化的,也就是从浏览器缓存中去读取http Status Code 为304。
当Product有数据变化时,这个时间刷新成当前时间,也就是Action从新生成了Html文档返回给浏览器。
原文:https://www.cnblogs.com/wangwust/p/9359312.html