我试图OutputCache在我的MVC应用程序中使用该属性,它在我使用时似乎不起作用OutputCacheLocation.Client:
public class HomeController : Controller
{
[OutputCache(Duration=15, Location=OutputCacheLocation.Client)]
public ActionResult Client()
{
ViewBag.Message = "The current time is " + DateTime.Now.ToString("hh:mm:ss");
return View();
}
[OutputCache(Duration=15, Location=OutputCacheLocation.Any)]
public ActionResult Any()
{
ViewBag.Message = "The current time is " + DateTime.Now.ToString("hh:mm:ss");
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
第一个不缓存.我每秒都会点击页面,它会改变时间.第二个工作.它只会每15秒更改一次.有什么我想念的吗?我正在使用IE8和Visual Studio中的内置开发服务器进行调试.
我有一个ASP.NET MVC应用程序.我需要缓存一些页面,但仅限于未经过身份验证的用户.
我尝试使用VaryByCustom="user"以下GetVaryByCustomString实现:
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom == "user")
{
if (context.User.Identity.IsAuthenticated)
{
return context.User.Identity.Name;
}
else
{
return "";
}
}
return base.GetVaryByCustomString(context, custom);
}
Run Code Online (Sandbox Code Playgroud)
然而,这并不是我需要的,因为页面仍然被缓存.唯一的区别是现在分别为每个用户缓存.
一种可能的解决方案是Guid.NewGuid()每次用户进行身份验证时返回,但这对我来说似乎是一种巨大的资源浪费.
所以你有什么提示吗?
我在我的MVC应用程序中使用标准的outputcache标签,它运行良好,但我需要强制它在某些时候被转储.我该如何实现这一目标?缓存的页面是从一个非常简单的路径{Controller}/{PageName}构建的 - 所以大多数页面都是这样的:/ Pages/About-Us
以下是我的.aspx视图页面顶部的输出缓存标记:
<@ OutputCache Duration="100" VaryByParam="None" %>
Run Code Online (Sandbox Code Playgroud)
因此,在内容更新的同一控制器上的另一个操作中,我需要转储此缓存,甚至是所有缓存 - 这是一个非常小的应用程序,因此转储所有缓存项目并不是一件大事.
在这个场景中,用户从我们的WebApp打开非安全页面,让我们在浏览器中将其称为PageA,然后单击其中的链接,将其带到PageB的安全实例.进入PageB后,用户可以随后单击一个链接,将其带回到PageA的安全实例(他们已经查看过并在OutputCache中).我观察到,即使在访问PageB(安全的)之后通过不同的URL访问PageA,它实际上是拉动先前的缓存副本而不是创建一个新的.我在调试会话中验证了这种行为,并且惊讶于ASP.Net使用相同的OutputCache项来获取页面的安全副本.
我的问题是为什么会这样?我如何告诉ASP.Net OutPutCache将来自安全URL的访问视为与非安全等价物不同/唯一的项目?
[背景]
我们最近将网站图像切换为使用Scene7/Akamai拍摄所有图像.因此,我们在安全连接上查看给定页面时添加了使用不同Scene7 URL的代码.此OutputCache问题不允许输出安全URL的逻辑执行,并导致丑陋的浏览器警告.
我在ASP.net MVC应用程序中使用OutputCache.由于使用活动的OutputCache进行开发并不是很愉快,我想在开发系统(本地机器和开发服务器)上禁用OutputCache.
做这个的最好方式是什么?
我正在寻找一个试图使用If-Modified-Since请求对象的标头的缓存库.问题是这个标题永远不会被设置,它总是空白的,这对我来说是有意义的,看它是如何请求的.
你怎么能强制要求有一个If-Modified-Since标题?或者我是否会这样做.
这是我指的功能.
public function isNotModified(Request $request)
{
$lastModified = $request->headers->get('If-Modified-Since');
$notModified = false;
if ($etags = $request->getEtags()) {
$notModified = (in_array($this->getEtag(), $etags) || in_array('*', $etags)) && (!$lastModified || $this->headers->get('Last-Modified') == $lastModified);
} elseif ($lastModified) {
$notModified = $lastModified == $this->headers->get('Last-Modified');
}
if ($notModified) {
$this->setNotModified();
}
return $notModified;
}
Run Code Online (Sandbox Code Playgroud) 我一直在搜索有关如何在项目级别禁用客户端缓存的信息.我知道我可以在动作方法之前添加以下内容:
[System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
我还阅读了有关为缓存创建配置文件的内容,但这也意味着在几个地方引用它们.我想在web.config中设置一个设置,或者在IIS中?
我正在研究的项目包含很多部分视图
提前感谢您对此事的任何建议.
我试图这样做,它导致contentlen 0的结果
看起来像:
[OutputCache(Duration = 36000)]
public JsonResult GetFileClasses()
{
return this.Json(TopicConfig.FileExtensionsSettings.List)
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已将Output Caching添加到我的应用程序中的几个操作中,以便轻松提升性能.但是,这些操作还需要通过命中Redis数据库在每个请求(它是一个视图计数器)后递增一个计数器.
首先,我想我可以调整动作过滤器执行的顺序,以确保计算视图:
public class CountersAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//increment my counter all clever like
base.OnResultExecuted(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
但那没用; 显然,OutputCacheAttribute的行为与普通的动作过滤器不同.然后我尝试实现自定义输出缓存:
public class OutputCacheWithCountersAttribute : OutputCacheAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//straight to the source to get my headcount!
base.OnResultExecuted(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
不,也没用; 缓存操作后,操作过滤器似乎完全被忽略.游民.
所以,呃,有没有办法(没有实现自定义输出缓存提供程序),以确保我的观点被正确计算,这是干净和明智的?
我遇到的问题是输出缓存似乎不适用于我的ASP.NET MVC 4(EPiServer 7)网站.
我在我的输出缓存配置文件中有以下内容web.config:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="PageOutput" enabled="true" duration="300" varyByParam="*" location="ServerAndClient" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Run Code Online (Sandbox Code Playgroud)
这是静态资源的输出缓存配置:
<caching>
<profiles>
<add extension=".gif" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
<add extension=".png" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
<add extension=".js" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
<add extension=".css" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="00:01:00" location="Any" />
<add extension=".jpg" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
<add extension=".jpeg" policy="DontCache" kernelCachePolicy="CacheUntilChange" duration="00:01:00" location="Any" />
</profiles>
</caching>
Run Code Online (Sandbox Code Playgroud)
我的控制器装饰有输出缓存属性,如下所示:
[OutputCache(CacheProfile = "PageOutput")]
public class HomePageController : BasePageController<HomePage>
{ ...}
Run Code Online (Sandbox Code Playgroud)
我在perfmon中观察以下计数器,但在访问主页时看不到它们按预期增加:
\ASP.NET …asp.net-mvc caching outputcache asp.net-mvc-4 asp.net-web-api