我有一个应用程序,每个用户都可以选择一个自定义布局。布局可以是不同的,不仅是css样式,还有html。
我知道mvc会缓存布局,但有这么多布局,我怀疑它是否适合缓存。那么,将模板保存在DB中还是保存在磁盘上更好呢?
仅供参考:我使用的数据库是MongoDB。
发布于 2012-03-14 19:11:16
我会将布局保存在磁盘上,因为目前我看不到数据库有任何优势(除非你这样做了)。但值得一提的是,您可以创建一个从OutputCacheAttribute派生的类,并使保存的结果取决于您使用的布局。
布局是否取决于用户?您可以使用VaryByCustom property使其因用户而异。
编辑
你的用户被允许动态地改变布局吗?如果是,您还应该有一个与您的用户相关联的guid,在每次布局更改时更改它,以便您返回您的VaryByCustom方法:
return string.Format("User-{0}-{1}", user.Id, user.LayoutUpdateGuid);明白这是什么意思了吗?这样,当用户更改布局时,他们将看到他们的页面立即更新。
如何在您的情况下应用VaryByCustom属性
在您的操作方法中,您可以使用:
[OutputCache(Duration = 3600, VaryByCustom = "UserLayouts")]
public ActionResult Details(string param)
{
   // Returning the view
}然后,在Global.asax.cs文件的VaryByCustom方法中:
protected override string VaryByCustom(string custom)
{
  switch (custom)
  {
    case "UserLayouts":
      //// Here you fetch your user details so you can return a unique
      //// string for each user and "publishing cycle" 
      //// Also, I strongly suggest you cache this user object and expire it
      //// whenever the user is changed (e.g. when the LayoutUpdateGuid is
      //// changed) so you achieve maximum speed and not defeat the purpose
      //// of using output cache.
      return string.Format("User-{0}-{1}", user.Id, user.LayoutUpdateGuid);
    break;
  }
}缺失的那一块
这里缺少的一部分是,您需要存储一个我称为LayoutUpdateGuid的值(我相信您会找到一个更好的名称),并在用户更改其布局字符串时更改该值。这将导致VaryByCustom( => )方法在Global.asasx.cs中返回一个不同的字符串,这反过来将强制您的操作方法再次运行,并返回具有更新布局的结果。
对你来说有意义吗?
注意:我不能测试我在这里写的特定代码,但我确信(除了打字错误)它是正确的。
https://stackoverflow.com/questions/9700674
复制相似问题