我已经阅读了大量关于MVC应用程序中的页面缓存和部分页面缓存的信息。然而,我想知道你将如何缓存数据。

在我的场景中,我将使用LINQ到实体(实体框架)。在第一次调用GetNames(或任何方法)时,我想从数据库中获取数据。我想将结果保存在缓存中,并在第二次调用时使用缓存版本(如果存在)。

谁能举例说明这将如何工作,这应该在哪里实现(模型?),如果它将工作。

我在传统的ASP中见过这种情况。NET应用程序,通常用于非常静态的数据。


当前回答

你也可以尝试使用内置在ASP MVC中的缓存:

添加以下属性到你想要缓存的控制器方法:

[OutputCache(Duration=10)]

在这种情况下,ActionResult将被缓存10秒。

更多信息请点击这里

其他回答

AppFabric缓存是一种分布式的内存缓存技术,可以跨多个服务器使用物理内存以键值对的形式存储数据。AppFabric为. net Framework应用程序提供了性能和可伸缩性改进。概念和架构

适用于。net 4.5+框架

添加reference: System.Runtime.Caching

添加using语句: 使用System.Runtime.Caching;

public string[] GetNames()
{ 
    var noms = System.Runtime.Caching.MemoryCache.Default["names"];
    if(noms == null) 
    {    
        noms = DB.GetNames();
        System.Runtime.Caching.MemoryCache.Default["names"] = noms; 
    }

    return ((string[])noms);
}

在. net Framework 3.5和更早的版本中,ASP。NET在System.Web.Caching命名空间中提供了内存中的缓存实现。在以前版本的. net Framework中,缓存仅在系统中可用。Web命名空间,因此需要依赖于ASP。网类。在. net Framework 4中,System.Runtime.Caching命名空间包含为Web和非Web应用程序设计的api。

更多信息:

https://msdn.microsoft.com/en-us/library/dd997357 (v = vs.110) . aspx https://learn.microsoft.com/en-us/dotnet/framework/performance/caching-in-net-framework-applications

我用过这种方法,对我很有效。 https://msdn.microsoft.com/en-us/library/system.web.caching.cache.add (v = vs.110) . aspx system.web. cache. cache.add的参数信息。

public string GetInfo()
{
     string name = string.Empty;
     if(System.Web.HttpContext.Current.Cache["KeyName"] == null)
     {
         name = GetNameMethod();
         System.Web.HttpContext.Current.Cache.Add("KeyName", name, null, DateTime.Noew.AddMinutes(5), Cache.NoSlidingExpiration, CacheitemPriority.AboveNormal, null);
     }
     else
     {
         name = System.Web.HttpContext.Current.Cache["KeyName"] as string;
     }

      return name;

}

我使用两个类。第一个是缓存核心对象:

public class Cacher<TValue>
    where TValue : class
{
    #region Properties
    private Func<TValue> _init;
    public string Key { get; private set; }
    public TValue Value
    {
        get
        {
            var item = HttpRuntime.Cache.Get(Key) as TValue;
            if (item == null)
            {
                item = _init();
                HttpContext.Current.Cache.Insert(Key, item);
            }
            return item;
        }
    }
    #endregion

    #region Constructor
    public Cacher(string key, Func<TValue> init)
    {
        Key = key;
        _init = init;
    }
    #endregion

    #region Methods
    public void Refresh()
    {
        HttpRuntime.Cache.Remove(Key);
    }
    #endregion
}

第二个是缓存对象列表:

public static class Caches
{
    static Caches()
    {
        Languages = new Cacher<IEnumerable<Language>>("Languages", () =>
                                                          {
                                                              using (var context = new WordsContext())
                                                              {
                                                                  return context.Languages.ToList();
                                                              }
                                                          });
    }
    public static Cacher<IEnumerable<Language>> Languages { get; private set; }
}

参考系统。并使用System.Web.Caching.Cache

    public string[] GetNames()
    {
      string[] names = Cache["names"] as string[];
      if(names == null) //not in cache
      {
        names = DB.GetNames();
        Cache["names"] = names;
      }
      return names;
    }

有点简化了,但我想应该可以。这不是MVC特定的,我一直使用这种方法缓存数据。