代码之家  ›  专栏  ›  技术社区  ›  TomHastjarjanto

缓存ASP.NET ViewData

  •  0
  • TomHastjarjanto  · 技术社区  · 14 年前

    我目前正在考虑在用户登录后缓存大部分视图数据,不包括特定于用户的数据。我认为最简单的方法是缓存viewdata对象本身,并在加载后添加特定于用户的数据。这种方法有什么缺点吗?有更好的方法吗?

    string cacheKey = "Nieuws/show/" + id;
    if (HttpRuntime.Cache[cacheKey] != null)
    {
          ViewData = HttpRuntime.Cache[cacheKey] as ViewDataDictionary;
    }
    else
    {
          // add stuff to view data
    
          HttpRuntime.Cache.Insert(cacheKey, ViewData, null, DateTime.Now.AddSeconds(180), Cache.NoSlidingExpiration,
          CacheItemPriority.NotRemovable, null);
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   OlafW    14 年前

    嗯…viewdata还可能包含许多不需要的信息,您对缓存不感兴趣。我建议创建一个拟合模型类,并在会话状态中保持该类。这将为您提供一个更清晰的架构。也许还需要一些额外的时间戳来支持某种更新场景,以便从数据库重新加载数据…