在ASP.NET MVC2中我使用OutputCache和VaryByParam属性.我用一个参数就可以正常工作,但是当我在方法上有几个参数时,正确的语法是什么?
[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId)
{
//I've got this one under control, since it only has one parameter
}
[OutputCache(Duration=30, VaryByParam = "customerId"]
public ActionResult Index(int customerId, int languageId)
{
//What is the correct syntax for VaryByParam now that I have a second parameter?
}
Run Code Online (Sandbox Code Playgroud)
如何使用这两个参数来缓存页面?我输入两次添加属性吗?或者写"customerId,languageId"作为值?
对于我的博客,我想使用输出缓存来保存一个特定帖子的缓存版本大约10分钟,这很好......
<%@OutputCache Duration="600" VaryByParam="*" %>
Run Code Online (Sandbox Code Playgroud)
但是,如果有人发表评论,我想清除缓存,以便刷新页面并查看评论.
我如何在ASP.Net C#中做到这一点?
我正在尝试在Web API中缓存ApiController方法的输出.
这是控制器代码:
public class TestController : ApiController
{
[OutputCache(Duration = 10, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public string Get()
{
return System.DateTime.Now.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
NB我还尝试了控制器本身的OutputCache属性,以及它的几个参数组合.
该路线在Global.asax中注册:
namespace WebApiTest
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHttpRoute("default", routeTemplate: "{controller}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个成功的回复,但它没有缓存在任何地方:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 18 Jul 2012 17:56:17 GMT
Content-Length: 96 …Run Code Online (Sandbox Code Playgroud) 我们有一个相当标准的电子商务场景,其中包含类别中的产品分页列表.无论好坏,大约80%的访问者从未浏览过第一页,根据类别,可能会有5-10多个结果页面,这些页面的查看次数要少得多.(是的,我们会优化第一页上显示的内容并进行良好的搜索 - 但这是一个不同的讨论)
我们无法缓存每一页的结果,因为我们受到内存的限制,但是缓存每个类别的第一页结果的好处将是巨大的.
我知道我可以使用对象缓存来存储有问题的数据集,但这可能是使用输出缓存,也许是通过使用response.Cache对象?
页面生命周期中的哪个位置可以完成?预渲染?
很简单,URL就像"/ ProductList?Category = something&Page = 1"而且我想要逻辑类似(伪代码):
If paramater "Page" equals 1
Use output caching: vary by param = "categoryName; page"
else
Don't use caching at all, just render the page from scratch.
Run Code Online (Sandbox Code Playgroud)
我们在IIS 6/win2003上使用ASP.NET 2.0.
我有一个asp.net mvc 3项目,我有一个家庭控制器.我已使用此属性标记了我的Index操作:
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Any, Duration = 120, VaryByParam = "*", VaryByCustom = "user")]
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
根据用户自定义的变化在Global.asax.cs中处理以检查用户cookie值,以便根据用户是否登录以及他们是什么用户来更改缓存.
当我在我的Web服务器上访问此页面时,我在响应中获得了这些标头:
Cache-Control public, max-age=120
Content-Type text/html; charset=utf-8
Content-Encoding gzip
Expires Sun, 20 Mar 2011 21:50:09 GMT
Last-Modified Sun, 20 Mar 2011 21:48:09 GMT
Vary Accept-Encoding
Date Sun, 20 Mar 2011 21:48:09 GMT
Content-Length 3105
Run Code Online (Sandbox Code Playgroud)
蝙蝠,Vary - Accept-Encoding值看起来不对,不应该发送Vary - *而不是吗?
我也将User.Identity.Name属性呈现给此视图,我注意到即使我注销它仍将呈现用户名,直到120秒到期.
public override string GetVaryByCustomString(HttpContext context, string custom)
{
if (custom.Equals("user", StringComparison.OrdinalIgnoreCase))
{
HttpCookie cookie = context.Request.Cookies["user"];
if …Run Code Online (Sandbox Code Playgroud) 我有一个部分View被缓存的问题,当它不应该被缓存.此部分视图用于显示页面上的登录/注销.它使用下面的简单代码来确定要显示的链接
@if(Request.IsAuthenticated) {
<a href="@Url.Action("LogOff", "Account", new { area = "" })">Log Off</a>
}
else {
<a href="@Url.Action("LogOn", "Account", new { area = "" })">Log On</a>
}
Run Code Online (Sandbox Code Playgroud)
使用我的MVC3应用程序中的所有页面调用此部分视图
@Html.Partial("_HeaderView")
Run Code Online (Sandbox Code Playgroud)
在我的大多数控制器中,我定义了输出缓存,因此我可以利用缓存内容.
[OutputCache(Duration = 86400, VaryByParam = "*")]
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,当我不想要部分视图时,整个页面都被缓存了.这导致了错误的行为,即使用户没有登录,它有时会显示LogOff等.有没有办法缓存所有内容,除了有问题的部分视图?
使用Windows Azure Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider作为MVC3应用程序的outputCache提供程序.以下是相关的操作方法:
[ActionName("sample-cached-page")]
[OutputCache(Duration = 300, VaryByCustom = "User",
Location = OutputCacheLocation.Server)]
[Authorize(Users = "me@mydomain.tld,another@otherdomain.tld")]
public virtual ActionResult SampleCachedPage()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
从Web浏览器加载此视图时出现以下异常:
System.Configuration.Provider.ProviderException: When using a custom output cache provider like 'DistributedCache', only the following expiration policies and cache features are supported: file dependencies, absolute expirations, static validation callbacks and static substitution callbacks.
System.Configuration.Provider.ProviderException: When using a custom output cache provider like 'DistributedCache', only the following expiration policies and cache features are supported: file dependencies, absolute …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.Net站点(碰巧是MVC,但这里没有关系),我想要缓存的几页很好.
具体来说,我想实现:
(注意 - 以上时间值仅供参考)
但我似乎无法让所有这些东西一起工作.这就是我所拥有的:
Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetETagFromFileDependencies();
Response.Cache.SetValidUntilExpires(true);
Response.Cache.SetMaxAge(TimeSpan.FromSeconds(60 * 10));
Run Code Online (Sandbox Code Playgroud)
我想要的场景可能吗?特别是:
谢谢你的任何提示!
我是缓存的新手,不知道数据如何存储在缓存中.我曾尝试在线阅读一些示例,但每个人都提供了存储和获取数据的代码片段,而不是解释如何使用memcache缓存数据.我已经读过它将数据存储在键值对中,但是我无法理解这些键值对存储在哪里?
还有人可以解释为什么进入缓存的数据被散列或加密?我在序列化数据和散列数据之间有点困惑.
我试图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中的内置开发服务器进行调试.
outputcache ×10
c# ×5
asp.net ×3
asp.net-mvc ×3
caching ×2
azure ×1
http-caching ×1
http-headers ×1
iis-6 ×1
memcached ×1