<p>Recently I have been investigating the possibilities of caching in ASP.NET.</p>

<p>I rolled my own "Cache", because I didn't know any better, it looked a bit like this:</p>

<pre><code>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 &gt; 10)
            {
                  // Do SQL to get data
                  m_myData = data;
                  m_cacheTime = DateTime.Now;
                  return m_myData;     
            }
            else
            {
                  return m_myData;
            }
      }

}
</code></pre>

<p>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.</p>

<p>What are the benefits over using the real method (<a href="http://msdn.microsoft.com/en-us/library/aa478965.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa478965.aspx</a>) instead of this?</p>

[{'answer_id': 11146, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 3, 'selected': True, 'text': '<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 11148, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': False, 'text': "<p>The ASP.NET caching mechanism has been around for a while, so it's stable and well understood.  There are lots of resources out there to help you make the most of it.</p>\n\n<p>Rolling your own might be the right solution, depending on your requirements.</p>\n\n<p>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.</p>\n"}, {'answer_id': 11154, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 1, 'selected': False, 'text': '<p>Caching in ASP.NET is feature rich and you can configure caching in quite a granular way. </p>\n\n<p>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).</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms178604.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms178604.aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/11141', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']