【问题标题】:.NET 4 Caching Support.NET 4 缓存支持
【发布时间】:2011-08-24 02:57:46
【问题描述】:

我了解 .NET 4 Framework 内置了缓存支持。有没有人有这方面的经验,或者可以提供很好的资源来了解更多相关信息?

我指的是内存中对象(主要是实体)的缓存,可能是 System.Runtime.Caching 的使用。

【问题讨论】:

  • 缓存什么?您是指实体框架、WCF 还是应用服务器?
  • System.Runtime.Caching 显然 ;-)
  • 如果您尚未升级到 .NET 4.5,您应该注意以下错误:stackoverflow.com/a/15715990/13087
  • Robust .NET caching 解释了如何在缓存时避免常见的陷阱,并在它提供的示例中特别使用了Memory Cache

标签: c# .net caching


【解决方案1】:

MSDN 文章"ASP.NET Caching: Techniques and Best Practices" 是一个很好的开始。

【讨论】:

    【解决方案2】:

    我假设您访问的是 thisSystem.Runtime.Caching,类似于 System.Web.Caching,并且位于更通用的命名空间中。

    http://deanhume.com/Home/BlogPost/object-caching----net-4/37

    在堆栈上,

    is-there-some-sort-of-cachedependency-in-system-runtime-caching 和,

    performance-of-system-runtime-caching.

    可能有用。

    【讨论】:

      【解决方案3】:

      希望你指的是.Netframework 4.0的System.Runtime.Caching

      下面的链接是一个很好的起点: Here

      【讨论】:

        【解决方案4】:

        我自己没有使用它,但如果您只是在内存中缓存简单的对象,您可能指的是 System.Runtime.Caching 命名空间中的MemoryCache 类。页面末尾有一个小例子说明如何使用它。

        编辑:为了让它看起来我实际上已经为这个答案做了一些工作,这是该页面的示例! :)

        private void btnGet_Click(object sender, EventArgs e)
        {
            ObjectCache cache = MemoryCache.Default;
            string fileContents = cache["filecontents"] as string;
        
            if (fileContents == null)
            {
                CacheItemPolicy policy = new CacheItemPolicy();
        
                List<string> filePaths = new List<string>();
                filePaths.Add("c:\\cache\\example.txt");
        
                policy.ChangeMonitors.Add(new 
                HostFileChangeMonitor(filePaths));
        
                // Fetch the file contents.
                fileContents = 
                    File.ReadAllText("c:\\cache\\example.txt");
        
                cache.Set("filecontents", fileContents, policy);
            }
        
            Label1.Text = fileContents;
        }
        

        这很有趣,因为它表明您可以将依赖项应用到缓存中,就像在经典的 ASP.NET 缓存中一样。这里最大的区别是您不依赖 System.Web 程序集。

        【讨论】:

        • 我用过这个,发现缺少文档。据我了解,您可以像使用 System.Web 缓存一样使用它。
        【解决方案5】:

        框架中的MemoryCache 是一个不错的起点,但您可能还想考虑LazyCache,因为它具有比内存缓存更简单的API,并且内置锁定以及其他一些不错的功能。它在 nuget 上可用:PM&gt; Install-Package LazyCache

        // Create our cache service using the defaults (Dependency injection ready).
        // Uses MemoryCache.Default as default so cache is shared between instances
        IAppCache cache = new CachingService();
        
        // Declare (but don't execute) a func/delegate whose result we want to cache
        Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources();
        
        // Get our ComplexObjects from the cache, or build them in the factory func 
        // and cache the results for next time under the given key
        ComplexObject cachedResults = cache.GetOrAdd("uniqueKey", complexObjectFactory);
        

        我最近写了一篇关于getting started with caching in dot net 的文章,您可能会觉得有用。

        (免责声明:我是 LazyCache 的作者)

        【讨论】:

          猜你喜欢
          • 2013-03-14
          • 1970-01-01
          • 2015-11-30
          • 1970-01-01
          • 2010-12-31
          • 1970-01-01
          • 2010-09-17
          • 1970-01-01
          相关资源
          最近更新 更多