我们使用预览3(包括严格的压力测试)部署了一个实时,清新,时髦的网站.
页面输出缓存是我们的救星,使我们有能力满足我们的性能合同要求.
我的问题是,动作OutputCacheFilter和页面输出缓存之间有区别吗?
动作输出缓存是否比页面输出缓存更快?
问候!
我创建了一个APSX Web表单,它根据一些提供的参数返回一个远程图像.它可以像这样使用:
<img src="/ImageGetter.aspx?param1=abc¶m2=123" />
Run Code Online (Sandbox Code Playgroud)
ImageGetter.aspx的标记和代码看起来类似于:
<%@ OutputCache Duration="100000" VaryByParam="*" Location="ServerAndClient" %>
<%@ Page Language="C#" AutoEventWireup="false" EnableSessionState="False" CodeBehind="ImageGetter.aspx.cs" Inherits="ACME.Helpers.ImageGetter" %>
Run Code Online (Sandbox Code Playgroud)
此代码在ImageGetter.aspx的Page_Load方法中调用:
byte[] data = null;
Dictionary<string, string> file_locations = GetImageLocations(param1, param2);
try
{
data = new WebClient().DownloadData(file_locations["main"]);
}
catch (WebException wex)
{
try
{
data = new WebClient().DownloadData(file_locations["backup"]);
}
catch (Exception e)
{
throw;
}
}
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(data, 0, data.Length);
Response.End();
Run Code Online (Sandbox Code Playgroud)
从我的测试来看,它似乎不是缓存.这可能与输出缓存有关,还是应该根据查询字符串参数编写自己的缓存来存储字节数组?
我有一个简单的aspx页面.这是它的顶部: -
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Foo.aspx.cs"
Inherits="Foo" %>
<%@ OutputCache Duration="3600" VaryByParam="none" Location="Any" %>
Run Code Online (Sandbox Code Playgroud)
现在,每当我点击FireFox中的页面时(无论是点击F5还是点击了url栏中的Enter),我都会收到200 OK响应.以下是FireBug的回复示例: -
请求标题: -
GET /sitemap.xml HTTP/1.1
Host: localhost.foo.com.au
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2)
Gecko/20100115 Firefox/3.6
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-au,en-gb;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: <snipped>
If-Modified-Since: Tue, 01 Jun 2010 07:35:17 GMT
If-None-Match: ""
Cache-Control: max-age=0
Run Code Online (Sandbox Code Playgroud)
响应标题: -
HTTP/1.1 200 OK
Cache-Control: public
Content-Type: text/xml; charset=utf-8
Expires: Tue, 01 Jun 2010 08:35:17 GMT
Last-Modified: Tue, …Run Code Online (Sandbox Code Playgroud) 使用ASP.NET MVC Web表单,我们可以将输出缓存放在控制器级别或视图级别.我们怎么能在.cshtml页面中提到"outputcache"?
我没找到任何地方.我在哪里可以获得语法?
我想要一个简单的方法来清除我的asp.net-mvc网站上的缓存页面.
我有昂贵的数据库操作,所以我经常使用输出缓存来使网站运行得更快.我的代码看起来像这样:
[OutputCache(Duration = 30000)]
public ActionResult Index()
{
return View();
}
[OutputCache(Duration = 30000, VaryByParam = "*")]
public ActionResult GetData(MyParams myParams)
{
return PartialView("MyView", GetVM(myParams));
}
Run Code Online (Sandbox Code Playgroud)
当我想明确清除此缓存(无论现有的缓存持续时间)时,有一些时候(当出现问题时)
无论如何,有完整和部分页面Outputcaching删除缓存页面并运行完整的代码?
注意:我看到这个问题已经在这里围绕asp.net一般问过,但我没看到asp.net-mvc特定的解决方案
我试过这个,但它似乎不起作用:
public ActionResult ClearCache()
{
this.HttpContext.Response.RemoveOutputCacheItem("/MyController/Index.aspx");
this.HttpContext.Response.RemoveOutputCacheItem("/MyController/MyView.ascx");
}
Run Code Online (Sandbox Code Playgroud) 我有这个自定义过滤器来压缩我的页面输出:
public class EnableCompressionAttribute : ActionFilterAttribute
{
const CompressionMode compress = CompressionMode.Compress;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpRequestBase request = filterContext.HttpContext.Request;
HttpResponseBase response = filterContext.HttpContext.Response;
string acceptEncoding = request.Headers["Accept-Encoding"];
if (acceptEncoding == null)
return;
if (acceptEncoding.ToLower().Contains("gzip"))
{
response.Filter = new GZipStream(response.Filter, compress);
response.AppendHeader("Content-Encoding", "gzip");
}
else if (acceptEncoding.ToLower().Contains("deflate"))
{
response.Filter = new DeflateStream(response.Filter, compress);
response.AppendHeader("Content-Encoding", "deflate");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从本书获得了代码:Pro ASP.NET MVC V2 Framework(.NET中的专家语音).
现在我有一个这样的动作方法:
[OutputCache(Order=1, Duration=300,VaryByParam="*", VaryByContentEncoding="gzip; deflate")]
[EnableCompression(Order=0)]
public ActionResult About()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
如何确保OutputCache过滤器缓存压缩内容?使用像这个例子中的"Order"参数就足够了吗?
我怎样才能看到缓存中发生了什么? …
有没有办法使用OutputCache属性来缓存仅注销用户的结果并重新评估登录用户示例:
我想要什么
[OutputCache(onlycacheanon = true)]
public ActionResult GetPhoto(id){
var photo = getPhoto(id);
if(!photo.issecured){
return photo...
}
return getPhotoOnlyIfCurrentUserHasAccess(id);
//otherwise return default photo so please don't cache me
}
Run Code Online (Sandbox Code Playgroud) 我在MVC 5中使用OutputCache来缓存服务器上的视图.
我只想基于查询字符串中的两个参数来缓存视图.
行动方法
[HttpGet]
[OutputCache(Location = OutputCacheLocation.Server, Duration = 60*10, VaryByParam = "id;quoteid")]
public ActionResult MyAction(int id, ProductCategoryType category)
{
return Content(DateTime.Now.ToString());
}
Run Code Online (Sandbox Code Playgroud)
路线
context.MapRoute(
"MyCustomRoute",
"myarea/{controller}/{action}/{id}/{category}/{name}/{quoteId}",
new { controller = "MyController", name = UrlParameter.Optional, quoteId = UrlParameter.Optional },
new[] { "MyNamespace.Areas.MyArea.Controllers" });
Run Code Online (Sandbox Code Playgroud)
网址
http://localhost:17191/myarea/mycontroller/myaction/2/1/a-holiday/aquoteid
Run Code Online (Sandbox Code Playgroud)
这可以正常工作并绑定数据,但是,如果我更改{name}了URL部分的任何部分,它仍然会生成一个新的缓存项,即使在我的操作方法中我已经指出了VaryByParam="id;quoteid"
例如...
http://localhost:17191/myarea/mycontroller/myaction/2/1/a-holiday/somequoteid
Run Code Online (Sandbox Code Playgroud)
和
http://localhost:17191/myarea/mycontroller/myaction/2/1/another-holiday/somequoteid
Run Code Online (Sandbox Code Playgroud)
...正在生成不同的DateTime输出 - 但它们不应该 - 它们应该是相同的.
我做错了什么,我怎样才能实现理想的行为?
编辑
需要明确的ProductCategoryType是,Enum是受其int价值约束的.当我调试时,对此的绑定是正确的ActionResult
编辑2
自从我被要求展示以来ProductCategoryType,我在下面添加了它.当我调试时,这确实正确绑定 - 我不认为它与问题有任何关系.
public enum ProductCategoryType …Run Code Online (Sandbox Code Playgroud) 我们使用Windows server 2008 R2 Enterprise和IIS7.5.7600.16385,并在服务器上部署了一个简单的Web(asp.net mvc,c#,.net framework 4.5.1).像下面这样的控制器,*.cshtml只输出一个日期时间:
public class DetailController : Controller
{
[OutputCache(Duration = 300, VaryByParam = "id")]
public ActionResult Index(int id)
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
当我第一次请求URL http:// localhost:80/Detail/Index?id = 3时,响应是正确的:
Cache-Control:public, max-age=300
Date:Mon, 24 Oct 2016 12:11:59 GMT
Expires:Mon, 24 Oct 2016 12:16:51 GMT
Last-Modified:Mon, 24 Oct 2016 12:11:51 GMT
Run Code Online (Sandbox Code Playgroud)
但是,当我再次请求url(ctrl + f5)时,max-age不正确(然后响应来自服务器缓存):
Cache-Control:public, max-age=63612908450
Date:Mon, 24 Oct 2016 12:16:34 GMT
Expires:Mon, 24 Oct 2016 12:20:50 GMT
Last-Modified:Mon, 24 Oct 2016 12:15:50 GMT
Run Code Online (Sandbox Code Playgroud)
我不知道为什么max-age这么大,以及它是如何生成的,它会在输出缓存过期时重新转换(ctrl + …
我有一个问题,如何以编程方式指示ASP.NET跳过从输出缓存中解析请求.
想象一下,通过在运行时将缓存策略设置从CMS应用到HttpResponse,您可以缓存页面输出(例如http://domain/page.aspx).在每个请求的基础上,取决于当前用户是否经过身份验证+一组已知组的成员(或由业务逻辑匹配),我想指示ASP.NET 跳过从输出缓存中解析请求.
场景是两个不同的用户同时(或更多)在系统上.用户A经过身份验证+一组已知组的成员,用户B是匿名用户.无论缓存的页面是什么,我都希望经过身份验证的用户浏览所有页面,就像没有启用输出缓存一样 - 永远; 同时,我希望ASP.NET继续为匿名用户(或者与业务逻辑不匹配的用户)提供输出缓存页面.
典型的建议是使用VaryByHeader,VaryByParam等并污染输出缓存 - 不好,但是在使用Reflector挖掘输出缓存模块时,我注意到输出缓存模块会跳过当前请求以防一些已知的"缓存"控制"标题存在.至于我关注标题,如果用户强制通过在地址栏中点击F5或ENTER来呈现新副本,则会从浏览器发送这些标题.
所以,我正在做的只是在输出缓存订阅的ResolveRequestCache事件之前的事件中,在自定义http模块中将"cache-control"标头设置为"no-cache".像这样:
context.Request.Headers["Cache-Control"] = "no-cache";
Run Code Online (Sandbox Code Playgroud)
但是,如果设置了HttpCachePolicy.SetValidUntilExpires(true)高速缓存策略,则ASP.NET将忽略先前设置的请求标头并从输出高速缓存提供请求.
作为替代方案,我想我可以在同一个http模块中的后处理事件中编写额外的代码,以确保调用HttpCachePolicy.SetValidUntilExpires(false),以防配置输出缓存,但我认为它会更多干净的解决方案实际上能够指示ASP.NET简单地跳过从输出缓存中解析请求.我可以想象这个问题有很多尴尬的解决方案,但我追求的是正确的.
作为参考,我一直在尝试HttpCachePolicy类的大多数相关方法,例如:
HttpResponse.Cache.SetNoServerCaching()).
Run Code Online (Sandbox Code Playgroud)