标签: varybyparam

如何实现VaryByCustom缓存?

我正在尝试实现根据主机缓存某些页面的功能.这是因为我可以拥有具有相同参数的页面的多个版本,并且请求方面唯一的区别是正在请求的主机.

因此,例如这两个URL将请求相同的页面,但它们的样式不同:

http://www.a.com/something/specific
Run Code Online (Sandbox Code Playgroud)

http://www.b.com/something/specific
Run Code Online (Sandbox Code Playgroud)

我将通过这里概述的示例:

http://msdn.microsoft.com/en-us/library/5ecf4420%28v=VS.90%29.aspx

但这对我没有意义.

我已将此添加到我的global.asax中:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "host")
    {
        return "host=" + context.Request.Url.Host;
    }

    return base.GetVaryByCustomString(context, arg);
}
Run Code Online (Sandbox Code Playgroud)

并且示例声明"要以编程方式设置自定义字符串,请调用SetVaryByCustom方法并将其传递给要使用的自定义字符串",代码类似于以下内容:

Response.Cache.SetVaryByCustom("host");
Run Code Online (Sandbox Code Playgroud)

问题是我不知道该怎么办.我已经添加了前一行,MvcApplication_EndRequest因为它似乎有意义,但我不认为这是正确的,因为当我设置断点时,GetVaryByCustomString它们永远不会被击中.

有人可以告诉我我在这里失踪了吗?或者如果我需要以不同方式做到这一点?

编辑: RE Darin的答案如下,我已经用以下方式装饰我的行为:

[CustomOutputCache(CacheProfile = "FundScreener")] // or similar depending on the action
Run Code Online (Sandbox Code Playgroud)

其中CustomOutputCacheAttribute定义为:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CustomOutputCacheAttribute: OutputCacheAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        AddLabelFilesDependency(filterContext);
        base.OnResultExecuted(filterContext);
    }

    private static void AddLabelFilesDependency(ControllerContext …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc caching varybyparam

15
推荐指数
1
解决办法
2万
查看次数

VaryByCustom不适用于会话变量

我正在使用输出缓存来登录带有登录系统的网站.我有全局页面,每个用户都可以访问.这些页面被缓存并使用母版页.

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="userid" %>
Run Code Online (Sandbox Code Playgroud)

我在会话中存储用户登录详细信息.我的global.asax文件在这里:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    string result = String.Empty;
    if (arg == "userid")
    {
        object o = Session["UserID"];
        if (o != null) { result = o.ToString(); }
    }
    else { result = base.GetVaryByCustomString(context, arg); }
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我在母版页面中有一个面板,对于经过身份验证的用户可见.当用户登录并查看公共页面时,另一个访客用户也会在页面A上看到经过身份验证的用户面板.如果访客首先查看页面A,则经过身份验证的用户不会在页面A上看到面板.

我的代码的哪一部分是错的?我第一次使用VaryByCustom.

编辑

我已经像这样修改了我的global.asax,但是文本文件中没有写入:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    string result = String.Empty;

    FileInfo t = new FileInfo(Server.MapPath("App_Data\\debug.txt"));
    StreamWriter Tex = t.AppendText();
    Tex.WriteLine("GetVaryByCustomString: " + arg); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net caching outputcache varybyparam

7
推荐指数
1
解决办法
2678
查看次数

缓存在我的ASP.NET MVC网站中无法正常工作?

我正在尝试在我的ASP.NET MVC网站中使用OutputCaching.问题是,当我尝试更改一个我的查询字符串参数的值时,它将返回所请求的第一个项目的数据!

这是我的代码(改变了param名称)......

[ApiAuthorize]
[HandleErrorAsJson]
public class SearchController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    [OutputCache(Duration = 60, VaryByParam = "*")]
    public ActionResult ScoreCard(string foo, byte? bar, byte? pewpew)
    {
    ..
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 注1:ApiAuthorize - 自定义属性,用于检查名为"Key"的查询字符串参数并检查内存字典,以查看它是否存在.
  • 注2:HandleErrorAsJson - 自定义属性,如果抛出异常,则返回错误消息为json.

这是我正在做这个动作的两个示例调用: -

所以第一次调用的数据(foo = hello world,Pew Pew)返回200 OK.然后第二个api调用返回200 OK但使用前一个调用的数据.

此外,我没有使用任何代理服务器.如果我注释掉OutputCache属性,一切都很好.

我也尝试了以下(每次我需要缓存时手动列出).....

[OutputCache(Duration = 60, VaryByParam = "foo,key,bar,pewpew")]
Run Code Online (Sandbox Code Playgroud)

没运气 :(

请注意我需要确保将"Key"参数作为缓存唯一键的一部分包含在内.我不希望人们搜索相同的东西,但如果第二个人没有正确的密钥,他们不应该得到缓存的结果,而是一个错误信息(技术上,它是401未授权,但无论如何)...

思考?

asp.net-mvc caching outputcache varybyparam

5
推荐指数
1
解决办法
2239
查看次数

标签 统计

caching ×3

varybyparam ×3

asp.net ×2

asp.net-mvc ×2

c# ×2

outputcache ×2