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

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

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

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


当前回答

适用于。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

其他回答

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

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; }
}

Steve Smith写了两篇很棒的博文,演示了如何在ASP中使用他的CachedRepository模式。净MVC。它有效地使用存储库模式,并允许您在无需更改现有代码的情况下获得缓存。

http://ardalis.com/Introducing-the-CachedRepository-Pattern

http://ardalis.com/building-a-cachedrepository-via-strategy-pattern

在这两篇文章中,他向你展示了如何建立这种模式,并解释了为什么它是有用的。通过使用这种模式,您可以在现有代码看不到任何缓存逻辑的情况下获得缓存。本质上,使用缓存的存储库就像使用其他存储库一样。

我参考了TT的帖子,并建议以下方法:

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

public string[] GetNames()
{ 
    var noms = Cache["names"];
    if(noms == null) 
    {    
        noms = DB.GetNames();
        Cache["names"] = noms; 
    }

    return ((string[])noms);
}

您不应该返回从缓存中重新读取的值,因为您永远不知道在特定时刻它是否仍在缓存中。即使您之前将它插入到语句中,它可能已经消失或从未添加到缓存中—您只是不知道。

因此,添加从数据库读取的数据并直接返回,而不是从缓存中重新读取。

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

HttpContext.Current.Cache.Insert("subjectlist", subjectlist);