一个非常基本的http请求:
GET http://stackoverflow.com/feeds/tag?tagnames=c%23&sort=newest HTTP/1.1
Host: stackoverflow.com
Accept-Encoding: gzip,deflate
Run Code Online (Sandbox Code Playgroud)
击中路线装饰:
[OutputCache(Duration = 300, VaryByParam = "tagnames;sort",
VaryByContentEncoding = "gzip;deflate", VaryByCustom = "site")]
Run Code Online (Sandbox Code Playgroud)
如果你包括if-modified-since,或者200 的旧数据,即重复和错误地服务304(无变化),即
HTTP/1.1 200 OK
Cache-Control: public, max-age=0
Content-Type: application/atom+xml; charset=utf-8
Content-Encoding: gzip
Expires: Fri, 01 Jul 2011 09:17:08 GMT
Last-Modified: Fri, 01 Jul 2011 09:12:08 GMT
Vary: *
Date: Fri, 01 Jul 2011 09:42:46 GMT
Content-Length: 14714
(payload, when decoded = some long-stale data)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,它是服务于这个近半个小时过去的 5分钟时间段; 它看起来像OutputCache的内部只是没有注意到时间; p它最终 …
有没有方法清除或重置整个网站的outputcache而不重启?
我刚开始在网站上使用outputcache,当我在设置时出错时,我需要一个可以浏览的页面来重置它.
假设我有一个ASP.NET应用程序在负载均衡器后面的几个Web服务器上运行:
我可以吗:
强制OutputCache(页面和/或控制级别)全局到期?
强制数据缓存(即Cache.Insert)到期?
从中央位置监控ASP.NET缓存使用情况(密钥,RAM等)?
一种可能的解决方案是使每次缓存使用检查文件依赖性以进行更改.可以触摸该文件,该文件将使所有缓存失效.但是,这要求开发人员在所有代码中包含依赖项.他们是更好的解决方案吗?
我可以获得WebMethod outputcached的返回值吗?例如,如果在最后X秒或分钟内调用了WebMethod,则不要再次实际运行该方法 - 只需使用与上次服务相同的结果.或者我应该在类/ WebMethod内部滚动自己的缓存?
在我的asp.net mvc应用程序中,我在不同的操作方法上使用OutputCache属性.是否可以查看与OutputCache属性相关的缓存中的当前条目?如果我在cicle上System.Web.HttpContext.Current.Cache我没有找到这种类型的条目.在此先感谢F.
我只是在ASP.NET MVC 3的RC版本中测试输出缓存.
不知何故,它没有尊重VaryByParam属性(或者更确切地说,我不确定我理解发生了什么):
public ActionResult View(UserViewCommand command) {
Run Code Online (Sandbox Code Playgroud)
这里,UserViewCommand有一个名为slug的属性,用于从数据库中查找用户.
这是我的OutputCache声明:
[HttpGet, OutputCache(Duration = 2000, VaryByParam = "None")]
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用不同的'slug'值(通过操作URL)来命中Action方法时,而不是提供错误的数据(我试图通过设计强制),而是调用action方法.
所以例如(按调用顺序)
/ user/view/abc - >使用slug = abc/user/view/abc调用action方法 - >未调用Action方法/ user/view/xyz - >再次使用slug = xyz调用action方法!是不是因为VaryByParam = none而不应该从缓存中出来?
另外,在这种情况下,OutputCaching的推荐方法是什么?(上面的例子)
我的自定义RouteBase实现有问题[OutputCache].我们有一个CMS,其中URL被映射到某些内容页面.每种类型的内容页面由不同的控制器(和不同的视图)处理.网址是完全免费的,我们需要不同的控制器,因此"catchall"路由不可用.因此,我们构建了一个自定义RouteBase实现,该实现调用数据库来查找所有URL.数据库知道要使用哪个Controller和Action(基于内容页面类型).
这很有用.
但是,将此与[OutputCache]属性相结合,输出缓存不起作用(页面仍然有效).我们确保[OutputCache]适用于我们的"普通"路由.
调试输出缓存是非常困难的,属性就在那里,我们就是它,它不起作用......想法如何处理这个将是非常受欢迎的,正确的答案也是如此!
控制器看起来像这样:
public class TextPageController : BaseController
{
private readonly ITextPageController textPageController;
public TextPageController(ITextPageController textPageController)
{
this.textPageController = textPageController;
}
[OutputCache(Duration = 300)]
public ActionResult TextPage(string pageid)
{
var model = textPageController.GetPage(pageid);
return View(model);
}
}
Run Code Online (Sandbox Code Playgroud)
自定义路线如下所示:
public class CmsPageRoute : RouteBase
{
private IRouteService _routeService;
private Dictionary<string, RouteData> _urlsToRouteData;
public CmsPageRoute(IRouteService routeService)
{
this._routeService = routeService;
this.SetCmsRoutes();
}
public void SetCmsRoutes()
{
var urlsToRouteData = new Dictionary<string, RouteData>();
foreach (var route in …Run Code Online (Sandbox Code Playgroud) 我有一个 API REST .NET 7,即使在未经授权的端点中,属性 [OutputCache] 也不会缓存
我添加了这个:
services.AddOutputCache(options =>
{
options.AddBasePolicy(builder => builder.Cache()); //removing this line doesn't work either
});
Run Code Online (Sandbox Code Playgroud)
然后在Configure()中:
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseOutputCache(); //putting it higher doesn't work either
Run Code Online (Sandbox Code Playgroud)
我的端点如下所示:
[AllowAnonymous]
[OutputCache(Duration = 99999)]
[HttpGet]
public async Task<IActionResult> GetAsync([FromQuery] Dto dto) => ReturnDataFromDataBase();
Run Code Online (Sandbox Code Playgroud)
但这不起作用。
我遵循文档https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?view=aspnetcore-7.0
编辑: 我添加更多信息,这个项目是.net 5,最近更新到.net 7([OutputCache]是在.net 7中引入的,这就是我的理解)。它不起作用,因为每次我(使用 Postman)向此端点发出请求时,它都会进入 ReturnDataFromDataBase 方法(我放置了一个断点)。我无法分享我的项目,因为它不是我的,但这是启动的配置方法(请随时纠正我):
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApiVersionDescriptionProvider provider)
{
//app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.UseCors();
app.UseAuthentication();
app.UseExceptionHandler("/Error");
app.UseIpRateLimiting();
app.UseMvc(routes => …Run Code Online (Sandbox Code Playgroud) 我在我的MVC网站中使用OutputCache属性如下:
[OutputCache(Duration = 5000,
VaryByParam = "name;region;model;id;op;content;featured;isStarred;page;size;")]
Run Code Online (Sandbox Code Playgroud)
但有时我想完全绕过输出缓存并强制从数据库中获取.对于我不断将新数据加载到数据库进行测试的测试环境,尤其如此.
无论如何我在这种情况下可以绕过缓存吗?
谢谢.
有谁知道为什么如果我的页面上有cookie,输出缓存不起作用!
示例页面
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ct.aspx.vb" Inherits="ct" %>
<%@ OutputCache Duration="600" Location="Server" VaryByParam="none" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Cache test</h1>
<p id="rndout" runat="server"></p>
</div>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
后面的示例代码:
Partial Class ct
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim rc As New Random()
Dim rn As Integer
rn = rc.Next()
rndout.InnerHtml = rn.ToString
Response.Cookies("sym")("hello") = "world"
Response.Cookies("sym").Expires = DateTime.Now.AddDays(370) …Run Code Online (Sandbox Code Playgroud) outputcache ×10
asp.net ×6
asp.net-mvc ×5
caching ×4
c# ×3
.net ×1
.net-7.0 ×1
asmx ×1
cookies ×1
web-services ×1