简体   繁体   English

ASP.NET缓存

[英]ASP.NET Caching

Recently I have been investigating the possibilities of caching in ASP.NET. 最近,我一直在研究ASP.NET中缓存的可能性。

I rolled my own "Cache", because I didn't know any better, it looked a bit like this: 我滚动了自己的“缓存”,因为我不知道更好,它看起来像这样:

public class DataManager
{

      private static DataManager s_instance;

      public static DataManager GetInstance()
      {
      }

      private Data[] m_myData;
      private DataTime m_cacheTime;

      public Data[] GetData()
      {
            TimeSpan span = DateTime.Now.Substract(m_cacheTime);

            if(span.TotalSeconds > 10)
            {
                  // Do SQL to get data
                  m_myData = data;
                  m_cacheTime = DateTime.Now;
                  return m_myData;     
            }
            else
            {
                  return m_myData;
            }
      }

}

So the values are stored for a while in a singleton, and when the time expires, the values are renewed. 因此,这些值将以单例形式存储一段时间,并且当时间到期时,将更新这些值。 If time has not expired, and a request for the data is done, the stored values in the field are returned. 如果时间尚未到期,并且完成了对数据的请求,则将返回该字段中存储的值。

What are the benefits over using the real method ( http://msdn.microsoft.com/en-us/library/aa478965.aspx ) instead of this? 代替使用实际方法( http://msdn.microsoft.com/en-us/library/aa478965.aspx )有什么好处?

I think the maxim "let the computer do it; it's smarter than you" applies here. 我认为“让计算机去做;比您聪明”的格言适用于此。 Just like memory management and other complicated things, the computer is a lot more informed about what it's doing than your are; 就像内存管理和其他复杂的事情一样,计算机比您的实际情况要了解得多。 consequently, able to get more performance than you are. 因此,能够获得比您更高的性能。

Microsoft has had a team of engineers working on it and they've probably managed to squeeze much more performance out of the system than would be possible for you to. 微软拥有一支由工程师组成的团队,他们可能设法从系统中挤出了比您可能更多的性能。 It's also likely that ASP.NET's built-in caching operates at a different level (which is inaccessible to your application), making it much faster. ASP.NET的内置缓存还可能在不同的级别(您的应用程序无法访问)上运行,从而使其速度更快。

The ASP.NET caching mechanism has been around for a while, so it's stable and well understood. ASP.NET缓存机制已经存在了一段时间,因此它是稳定且易于理解的。 There are lots of resources out there to help you make the most of it. 有很多资源可以帮助您充分利用它。

Rolling your own might be the right solution, depending on your requirements. 根据您的要求,自己动手可能是正确的解决方案。

The hard part about caching is choosing what is safe to cache, and when. 缓存的难点在于选择什么可以缓存以及何时可以缓存。 For applications in which data changes frequently, you can introduce some hard to troubleshoot bugs with caching, so be careful. 对于经常更改数据的应用程序,您可能会引入一些难以解决的缓存错误,因此请当心。

Caching in ASP.NET is feature rich and you can configure caching in quite a granular way. ASP.NET中的缓存功能丰富,您可以以非常精细的方式配置缓存。

In your case (data caching) one of the features you're missing out on is the ability to invalidate and refresh the cache if data on the SQL server is updated in some way (SQL Cache Dependency). 在您的情况下(数据缓存),您缺少的功能之一是能够以某种方式(SQL缓存相关性)更新SQL Server上的数据来使缓存无效并刷新缓存。

http://msdn.microsoft.com/en-us/library/ms178604.aspx http://msdn.microsoft.com/en-us/library/ms178604.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM