优化网站,dotNet MVC 可以通过(OutputCache)特性在某些Action上使用缓存,如果我们想要自定义缓存依据可以通过如下方式进行:

第一步, 在 global.asax.cs 文件中 overite GetVaryByCustomString函数:

   /// <summary>
/// 自定义生成的依据
/// </summary>
/// <param name="context"></param>
/// <param name="custom"></param>
/// <returns></returns>
public override string GetVaryByCustomString(HttpContext context, string custom) {
if (EqualsIgnoreCase("AnonymousID", custom)) {
return context.Request.AnonymousID;
}
return base.GetVaryByCustomString(context, custom);
} // System.Web.Util.StringUtil
static bool EqualsIgnoreCase(string s1, string s2) {
return (string.IsNullOrEmpty(s1) && string.IsNullOrEmpty(s2)) ||
(!string.IsNullOrEmpty(s1) && !string.IsNullOrEmpty(s2) && s2.Length == s1.Length &&
string.Compare(s1, 0, s2, 0, s2.Length, StringComparison.OrdinalIgnoreCase) == 0);
}

第二步:在需要输出缓存的Action上添加OutPutCache特性:

   [OutputCache(CacheProfile = "StaticPage")]
public ActionResult Index()
{
return View();
}

第三部:为了便于修改缓存配置,我们可以办具体的缓存设置写在配置文件中,如下:

    <caching>
<outputcachesettings>
<outputcacheprofiles>
<clear />
<!-- 24 hours-->
<add varybycustom="AnonymousID" varybyparam="*" duration="86400" name="StaticPage" />
</outputcacheprofiles>
</outputcachesettings>
</caching>

Mvc 页面缓存 OutputCache VaryByCustom的更多相关文章

  1. 页面缓存OutputCache

    更新页面缓存OutputCache   为什么要使用OutputCache 我认为OutputCache是最简单的缓存技术了,它针对的是页面级别的,简单的一条指令就可以达到缓存的效果,有效的减轻服务器 ...

  2. [置顶] MVC输出缓存(OutputCache参数详解)

    1.学习之前你应该知道这些 几乎每个项目都会用到缓存,这是必然的.以前在学校时做的网站基本上的一个标准就是1.搞定增删改查2.页面做的不要太差3.能运行(ps真的有这种情况,答辩验收的时候几个人在讲台 ...

  3. MVC输出缓存(OutputCache参数详解)

    版权声明:本文为博主原创文章,未经博主允许转载随意. https://blog.csdn.net/kebi007/article/details/59199115 1.学习之前你应该知道这些 几乎每个 ...

  4. ASP.NET MVC 阻止当前请求的视图页面缓存OutputCache

    设置缓存 [OutputCache(Duration =333,VaryByCustom ="Index")] 缓存: //在action中,临时阻止该次请求的视图页面缓存 Res ...

  5. 更新页面缓存OutputCache

    为什么要使用OutputCache 我认为OutputCache是最简单的缓存技术了,它针对的是页面级别的,简单的一条指令就可以达到缓存的效果,有效的减轻服务器的压力和减少带宽,对于网站一些不会频繁更 ...

  6. .Net页面缓存OutPutCache详解

    一 它在Web.Config中的位置 <system.web> <!--页面缓存--> <caching> <outputCacheSettings> ...

  7. ASP.NET 页面缓存OutputCache用法实例(附ab压力测试对比图)

    本文主要介绍Web窗体页面中的使用方法,MVC中使用方法,大家自行百度. 一.简单一行指令即可实现 <%@ OutputCache VaryByParam=" %> 这样整个页面 ...

  8. MVC页面缓存

    1.OutputCache 属性 contact.cshtml    [OutputCache(Duration=10)] public ActionResult Contact()   {      ...

  9. asp.net mvc 页面缓存

    在任务中需要实现点击浏览器back按钮,加载的前一页面需要强制刷新. 想要在前端通过js来绑定数据实现,但是觉得太麻烦,还是用另一种方式来解决: 不缓存该页面. 简单易懂: Response.Cach ...

随机推荐

  1. linux 监控命令

    先总结下常用的一些监控工具: ##linux命令 w 系统负载 lsof -p pid 进程打开的文件 lsof -i:port 端口的运行情况 free -m 内存情况 vmstat 进程.内存.内 ...

  2. poj3693 Maximum repetition substring

    题意 给出一个长度为\(n(n\leqslant 100000)\)的串,求一个字典序最小的子串使得它是某个字符串重复\(k\)次得到的,且\(k\)最大 题解 后缀数组论文上的题,跟上一篇uva那个 ...

  3. 洛谷 P1004 方格取数

    题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...

  4. 分享一个很好看的WPF界面

    今天在http://www.iopenworks.com/Products/ProductDetails/Introduction?proID=386 上面看到的,先mark下来...... 地址:h ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...

  6. Android源代码之DeskClock (一)

    一.概述 一直有read the fucking source code的计划,可是实行起来都是断断续续的.到如今也没有真正得读过多少Android的源代码(主要是懒的).如今回忆起来实在是非常羞愧, ...

  7. tcpdump抓包以及port查看的一些操作

    1.tcpdump. nginx开启后会占用80端口,此时运行命令:tcpdump tcp port 80 结果例如以下: [syswj@host ~]$ sudo tcpdump tcp port ...

  8. DateTime格式大全

    DateTime dt = DateTime.Now;//    Label1.Text = dt.ToString();//2005-11-5 13:21:25//    Label2.Text = ...

  9. 用.class文件创建对象

    第一步:  给你一个编译好的class文件以及它的包名,创建一个对象出来. 1)class文件源代码 package com.wsc.classloader; public class Tool{ p ...

  10. MyBatis Tutorial – CRUD Operations and Mapping Relationships – Part 1---- reference

    http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part ...