我正在尝试理解ASP.NET OutputCache机制.
我用Label和LinkButton构建了一个测试页面.
标签文本正在服务器上初始化,每个PageLoad上都有当前服务器日期:
protected void Page_Load(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我使用了这个指令: <%@ OutputCache Duration="600" VaryByParam="none"%>
当我第一次在Label中获得一个新文本时按下LinkButton但是如果我再次按下linkbutton,我就不会得到一个新文本.
我假设这是因为传输到服务器的参数对于每个回发都是相同的.
有没有办法使用OutputCach和回发控件?
有什么方法可以列出当前存储在OutputCache中的页面吗?
只是一个路径列表可以做,但如果有办法获得有关每个项目的更多信息(到期等),那就更好了.
我Html.RenderAction<CartController>(c => c.Show());在我的主页面上使用显示所有页面的购物车.问题是当我将一个项目添加到购物车然后点击浏览器后退按钮时.它显示旧购物车(来自Cache),直到我点击刷新按钮或导航到另一个页面.
我已经尝试了这个并且它完美地工作但是它为我的站点中的所有页面全局禁用了全局缓存(因为这个Action方法在主页面上使用).出于性能原因,我需要为其他几个部分视图(操作方法)启用缓存.
我不想在页面加载时使用带有AJAX的客户端脚本刷新购物车(和登录视图) - 但这是我现在能想到的唯一解决方案.
有谁知道更好吗?
我OutputCache在ASP.NET MVC中使用该属性相当新.
我在我网站上的静态页面上启用了它,代码如下:
[OutputCache(Duration = 7200, VaryByParam = "None")]
public class HomeController : Controller
{
public ActionResult Index()
{
//...
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,我将整个控制器缓存7200秒(2小时).
但是,它如何与动态页面一起使用?通过动态,我指的是用户必须提交表单的位置.
例如,我有一个带有电子邮件表单的页面.这是代码的样子:
public class ContactController : Controller
{
//
// GET: /Contact/
public ActionResult Index()
{
return RedirectToAction("SubmitEmail");
}
public ActionResult SubmitEmail()
{
//In view for CAPTCHA: <%= Html.GenerateCaptcha() %>
return View();
}
[CaptchaValidator]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SubmitEmail(FormCollection formValues, bool captchaValid)
{
//Validate form fields, send email if everything's good...
if (isError)
{ …Run Code Online (Sandbox Code Playgroud) 在我的asp.net mvc项目中,我在控制器上启用输出缓存,如下所示
[OutputCache(Duration = 100, VaryByParam = "*", VaryByHeader = "X-Requested-With")]
public class CatalogController : BaseController
{
public ActionResult Index(string seller)
{
// I do something
}
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,直到创建我自己的Route类,如下所示
public class MyRoute : Route
{
// there is a constructor here..
// I override this method..
// just to add one data called 'seller' to RouteData
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var data = base.GetRouteData(httpContext);
if (data == null) return null;
var seller = DoSomeMagicHere();
// add seller
data.Values.Add("seller", …Run Code Online (Sandbox Code Playgroud) 我正在建立一个在线商店,并试图通过最小化MYSQL查询来提高性能.
通过txt文件缓存mysql查询然后获取而不是查询是一种好习惯吗?这就是我正在做的事情
这比每次执行sql查询更有效吗?我遗失的任何安全问题?
我有这个动作方法:
[OutputCache(Duration = 2,
Location = OutputCacheLocation.Any,
VaryByHeader = "Accept-Charset")]
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
生成的响应是:
Cache-Control:public, max-age=2
Content-Length:5164
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Sep 2011 16:30:33 GMT
Expires:Wed, 28 Sep 2011 16:30:35 GMT
Last-Modified:Wed, 28 Sep 2011 16:30:33 GMT
Server:Microsoft-IIS/7.5
Vary:*
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
Run Code Online (Sandbox Code Playgroud)
为什么Vary标题显示星号而不是Accept-Charset?
OutputCacheAttribute确实对响应有影响,实际上,Expires和Cache-Control:max-age=n头部取决于Duration参数,而Cache-Control:public/ private/ no-cache取决于Location参数.
我已经创建了一个包装器OutputCacheAttribute来查看发生了什么:
public class CustomOutputCacheAttribute:OutputCacheAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{ …Run Code Online (Sandbox Code Playgroud) 我试图使WriteSubstition在MVC 5中工作,并且在执行替换时,它是在请求的开始而不是在替换被调用的位置进行的。
我使用了以下扩展方法:
public static class HtmlExtensions
{
public delegate string CacheCallback(HttpContextBase context);
public static object Substitution(this HtmlHelper myHtml, CacheCallback cacheCallback)
{
myHtml.ViewContext.HttpContext.Response.WriteSubstitution(
c => HttpUtility.HtmlEncode(
cacheCallback(new HttpContextWrapper(c))
));
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
并在Index.cshtml中按如下所示调用它:
<div class="row">
<div class="col-md-4">
<h2Test</h2>
@DateTime.Now
@Html.Substitution(ctx => DateTime.Now.ToString())
@{Html.ViewContext.HttpContext.Response.WriteSubstitution(c => DateTime.Now.ToString());}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如您所见,我也尝试了不使用扩展方法的情况,即内联调用WriteSubstitution。页面呈现时,替换的块位于请求的开头:
10/10/2014 11:33:46 AM10/10/2014 11:33:46 AM<!DOCTYPE html>
<html>
Run Code Online (Sandbox Code Playgroud)
这是WriteSubstitution中的错误,还是我做错了什么?
我Microsoft.Web.RedisOutputCacheProvider按照https://msdn.microsoft.com/en-us/library/azure/dn798898.aspx中的说明安装了nuget软件包,用于在多个服务器实例上运行的Azure Web App ,并且收到以下错误:
When using a custom output cache provider like 'MyRedisOutputCache', only the following expiration policies and cache features are supported: file dependencies, absolute expirations, static validation callbacks and static substitution callbacks.
Run Code Online (Sandbox Code Playgroud)
配置设置如下:
<system.web>
<caching>
<outputCache defaultProvider="MyRedisOutputCache">
<providers>
<add name="MyRedisOutputCache"
type="Microsoft.Web.Redis.RedisOutputCacheProvider"
port="1234"
host="[my host]"
accessKey="[my access key]"
ssl="true" />
</providers>
</outputCache>
</caching>
</system.web>
Run Code Online (Sandbox Code Playgroud)
我已经尝试将其设置connectionString为有效的StackExchange.Redis连接字符串,而不是设置单个端口/主机等...但仍然收到相同的错误.
我们还Microsoft.Web.RedisSessionStateProvider使用相同的设置在同一个Web应用程序中运行,没有任何问题.
任何帮助,将不胜感激.
我们正在使用MVC的outputcache属性,如下所示
[OutputCache(Location = System.Web.UI.OutputCacheLocation.Server, Duration = 60 * 60 * 12, VaryByParam = "staticDataTypes;customerSubscriptionId")]
Run Code Online (Sandbox Code Playgroud)
这是Duration的默认值是 什么?
outputcache ×10
asp.net-mvc ×6
caching ×6
asp.net ×5
azure ×1
c# ×1
duration ×1
http-caching ×1
mysql ×1
php ×1