<p>For a .NET component that will be used in both web applications and rich client applications, there seem to be two obvious options for caching: System.Web.Caching or the Ent. Lib. Caching Block.</p>

<ul>
<li>What do you use?</li>
<li>Why?</li>
</ul>

<h2><a href="http://msdn.microsoft.com/en-us/library/system.web.caching.aspx" rel="noreferrer">System.Web.Caching</a></h2>

<p>Is this safe to use outside of web apps?  I've seen mixed information, but I think the answer is maybe-kind-of-not-really.</p>

<ul>
<li><a href="http://support.microsoft.com/kb/917411" rel="noreferrer">a KB article warning against 1.0 and 1.1 non web app use</a></li>
<li>The 2.0 page has a <em>comment</em> that indicates it's OK: <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache(VS.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/system.web.caching.cache(VS.80).aspx</a></li>
<li><a href="http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx" rel="noreferrer">Scott Hanselman is creeped out by the notion</a></li>
<li><a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx" rel="noreferrer">The 3.5 page</a> includes a warning against such use</li>
<li>Rob Howard <a href="http://weblogs.asp.net/cschittko/archive/2004/07/04/172684.aspx" rel="noreferrer">encouraged use outside of web apps</a></li>
</ul>

<p>I don't expect to use one of its highlights, <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx" rel="noreferrer">SqlCacheDependency</a>, but the addition of <a href="http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemupdatecallback.aspx" rel="noreferrer">CacheItemUpdateCallback</a> in .NET 3.5 seems like a Really Good Thing.  </p>

<h2><a href="http://msdn.microsoft.com/en-us/library/aa480453.aspx" rel="noreferrer">Enterprise Library Caching Application Block</a></h2>

<ul>
<li>other blocks are already in use so the dependency already exists</li>
<li>cache persistence isn't necessary; regenerating the cache on restart is OK</li>
</ul>

<p>Some cache items should always be available, but be refreshed periodically.  For these items, getting a callback <em>after</em> an item has been removed is not very convenient.  It looks like a client will have to just sleep and poll until the cache item is repopulated.</p>

<h2><a href="http://jehiah.cz/projects/memcached-win32/" rel="noreferrer">Memcached for Win32</a> + <a href="http://sourceforge.net/projects/memcacheddotnet/" rel="noreferrer">.NET client</a></h2>

<p>What are the pros and cons when you don't need a <em>distributed</em> cache?</p>

[{'answer_id': 21882, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 2, 'selected': False, 'text': '<p>Take a look at <a href="http://www.danga.com/memcached/" rel="nofollow noreferrer">memcached</a>.  It is a really cool, fast and lightweight distributed caching system.  There are APIs for several of the most popular languages, including C#.  It may not serve well on the client side (unless of course the client is obtaining the cached data from a server of some kind), but if you abstract your usage of memcached to a specific interface, you could then implement the interface with another caching system.</p>\n'}, {'answer_id': 21983, 'author': 'PhilPursglove', 'author_id': 1738, 'author_profile': 'https://Stackoverflow.com/users/1738', 'pm_score': 2, 'selected': False, 'text': '<p>Bear in mind that the EntLib documentation specifically steers you towards the ASP.NET cache for ASP.NET applications. That\'s probably the strongest recommendation towards using it here. Plus the EntLib cache doesn\'t have dependencies, which for me is a big reason not to use it.</p>\n\n<p>I don\'t think there\'s a technical limitation as such on shipping System.Web as part of your app, though it\'s slightly odd that they\'ve put that notice in on the .NET 3.5 page. Hanselman actually says he started out being creeped out by this notion, but became convinced. Also if you read the comments, he says that the block has too many moving parts and the ASP.NET Cache is much more lightweght.<br>\nI think this is exactly the kind of problem that <a href="http://blogs.msdn.com/velocity/" rel="nofollow noreferrer">Velocity</a> is going to solve, but that\'s only a preview for now :-(  </p>\n\n<p>I\'d say use Web.Caching and see how you get on. If you put some kind of abstraction layer over the top of it, you\'ve always got the option to swap it out for the EntLib block later on if you find problems.</p>\n'}, {'answer_id': 228028, 'author': 'Andrew Harry', 'author_id': 30576, 'author_profile': 'https://Stackoverflow.com/users/30576', 'pm_score': -1, 'selected': False, 'text': '<p>@Davide Vosti</p>\n\n<p>"If they put it in the web namespace, I think\'s it\'s for a good reason."\nDoes that same logic apply to the Concurrency and Coordination Runtime (CCR) in the robotic studio? no? didn\'t think so.</p>\n'}, {'answer_id': 984076, 'author': 'Andrew Siemer', 'author_id': 83889, 'author_profile': 'https://Stackoverflow.com/users/83889', 'pm_score': 5, 'selected': True, 'text': '<p>These are the items that I consider for the topic of Caching:</p>\n\n<p>MemCached Win32\nVelocity\n.net Cache\nEnterprise Library Caching Application Block</p>\n\n<p><strong>MemCached Win32:</strong> Up until recently I have used MemCached Win32.  This is a akin to a web farm (many servers serving the same content for high availability) but it is a cache farm.  This means that you can install it locally on your web server initially if you don\'t have the resources to go bigger.  Then as you go down the road you can scale horizontally (more servers) or vertically (more hardware).  This is a product that was ported from the original MemCached to work on Windows.  This product has been used extensively in very high traffic sites.  <a href="http://lineofthought.com/tools/memcached" rel="nofollow noreferrer">http://lineofthought.com/tools/memcached</a></p>\n\n<p><strong>Velocity:</strong> This is Microsofts answer to products such as MemCached.  MemCached has been out for quite some time, Velocity is in CTP mode.  I must say that from what I have read so far this product will certainly turn my head once it is out.  But I can\'t bring myself to run big production projects on a CTP product with zero track record.  I have started playing with it though as once it gains momentum MemCached won\'t even compare for those locked in the windows world!  <a href="http://blogs.msdn.com/velocity/" rel="nofollow noreferrer">http://blogs.msdn.com/velocity/</a></p>\n\n<p><strong>.NET Cache:</strong>  There is no reason to discount the standard .NET Cache.  It is built in and ready to use for free and with no (major) set up required.  It offers flexibility by way of offering mechanisms for storing items in local memory, a SINGLE state server, or a centralized database.  Where Velocity steps in is when you need more than a single state server (cache in memory) and don\'t want to use a slow database for holding your cache.</p>\n\n<p><strong>Enterprise Application Block:</strong> I stay away from all of the Enterprise Application Blocks.  They are heavy frameworks that give more than I generally require!  As long as you remember to wrap everything that touches code that is not your own and follow simple rules for coding, stick to any of the other methods over this one!  (just my opinion of course - MySpace leverages as much as they can out of Enterprise Application Blocks!)</p>\n\n<p><strong>You don\'t have to choose up front!</strong>  I generally create a cache wrapper that I communicate with in my code for methods such as Get, Set, Exists, Remove, ListKeys, etc.  This then points to an underlying level of cache abstraction that can point to MemCached, Velocity, or .NET cache.  I use StructureMap (or choose another IoC container) to inject which form of cache I want to use for a given environment.  In my local dev box I might use .NET cache in the session.  In production I generally use MemCached Win 32.  But regardless of how it is set up you can easily swap things around to try each system out to see what works best for you.  You just need to make sure that you application knows as little as possible about how things are cached!  Once this layer of abstraction is in place you can then do things such as run a compression algorithm (gzip) for all the data that is going in and out of cache which would allow you to store 10 times the amount of data in cache. - <strong>transparently</strong>.</p>\n\n<p>I cover .NET Cache, MemCached Win32, StructureMap, and the appropriate abstractions in my book if you are interested!</p>\n\n<p>ASP.NET 3.5 Social Networking (<a href="https://rads.stackoverflow.com/amzn/click/com/1847194788" rel="nofollow noreferrer" rel="nofollow noreferrer">http://www.amazon.com/ASP-NET-3-5-Social-Networking-Enterprise-ready/dp/1847194788/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1225408005&amp;sr=8-1</a> )\nAndrew Siemer www.andrewsiemer.com blog.andrewsiemer.com www.socialnetworkingin.net</p>\n\n<p><strong>Update</strong>\nChanged the link that lists sites using memcached.  Thank you David for noticing that it was broken!</p>\n'}]
['https://Stackoverflow.com/questions/21870', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/150/']