<p>We've just started using LINQ to SQL at work for our DAL &amp; we haven't really come up with a standard for out caching model.  Previously we had being using a base 'DAL' class that implemented a cache manager property that all our DAL classes inherited from, but now we don't have that.  I'm wondering if anyone has come up with a 'standard' approach to caching LINQ to SQL results?</p>

<p>We're working in a web environment (IIS) if that makes a difference. I know this may well end up being a <a href="https://stackoverflow.com/questions/tagged/subjective">subjective</a> question, but I still think the info would be valuable.</p>

<p><strong>EDIT:</strong> To clarify, I'm not talking about caching an individual result, I'm after more of an architecture solution, as in how do you set up caching so that all your link methods use the same caching architecture.</p>

[{'answer_id': 37432, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 2, 'selected': False, 'text': "<p>It's right under your nose:</p>\n\n<pre><code>List&lt;TableItem&gt; myResult = (from t in db.Table select t).ToList();\n</code></pre>\n\n<p>Now, just cache myResult as you would have cached your old DAL's returned data.</p>\n"}, {'answer_id': 37587, 'author': 'Thomas Lundström', 'author_id': 3985, 'author_profile': 'https://Stackoverflow.com/users/3985', 'pm_score': 4, 'selected': True, 'text': '<p>A quick answer: Use the Repository pattern (see Domain Driven Design by Evans) to fetch your entities. Each repository will cache the things it will hold, ideally by letting each instance of the repository access a singleton cache (each thread/request will instantiate a new repository but there can be only one cache).</p>\n\n<p>The above answer works on one machine only. To be able to use this on many machines, use <a href="http://memcached.org/" rel="noreferrer">memcached</a> as your caching solution. Good luck!</p>\n'}, {'answer_id': 357970, 'author': 'Pete Montgomery', 'author_id': 40759, 'author_profile': 'https://Stackoverflow.com/users/40759', 'pm_score': 5, 'selected': False, 'text': '<p>My <a href="http://petemontgomery.wordpress.com/2008/08/07/caching-the-results-of-linq-queries/" rel="noreferrer">LINQ query result cache</a> is probably just what you\'re looking for.</p>\n\n<pre><code>var q = from c in context.Customers\n        where c.City == "London"\n        select new { c.Name, c.Phone };\n\nvar result = q.Take(10).FromCache();\n</code></pre>\n\n<p>Pete.</p>\n'}, {'answer_id': 358383, 'author': 'KristoferA', 'author_id': 11241, 'author_profile': 'https://Stackoverflow.com/users/11241', 'pm_score': 0, 'selected': False, 'text': '<p>See the \'GetReferenceData\' method in the \'ReferenceData\' class in <a href="http://blog.huagati.com/res/index.php/2008/06/23/application-architecture-part-2-data-access-layer-dynamic-linq/" rel="nofollow noreferrer">this article</a>:\n<a href="http://blog.huagati.com/res/index.php/2008/06/23/application-architecture-part-2-data-access-layer-dynamic-linq/" rel="nofollow noreferrer">http://blog.huagati.com/res/index.php/2008/06/23/application-architecture-part-2-data-access-layer-dynamic-linq/</a></p>\n\n<p>It uses the asp.net page cache for caching data retrieved using L2S.</p>\n'}, {'answer_id': 2879171, 'author': 'Mark', 'author_id': 64084, 'author_profile': 'https://Stackoverflow.com/users/64084', 'pm_score': 1, 'selected': False, 'text': '<p>I found <a href="http://cticoder.wordpress.com/2009/11/25/linq2sql-caching-with-sqldependency/" rel="nofollow noreferrer">this post</a>, which offers an extension method as a means of caching the LINQ objects.</p>\n\n<p>I\'ve been banging my head against the wall for weaks now trying to figure out a good caching solution for Linq2SQL, an must admit that I\'m really struggling to find a one-size fits all...</p>\n\n<p>The repository pattern tends to limit the usefullness of Linq, since (without reimplementing IQueryable) caching must bge performed outside of Linq statement.</p>\n\n<p>Moreover, deferred loading and object tracking are both big no-nos if you\'re going to cache your objects, which makes performing updates somewhat trickier.</p>\n\n<p>Anyone who\'s managed to solve this problem in the wild within a highly concurrent web project, please chime in and save the world! :) </p>\n'}, {'answer_id': 3384861, 'author': 'Remus Rusanu', 'author_id': 105929, 'author_profile': 'https://Stackoverflow.com/users/105929', 'pm_score': 1, 'selected': False, 'text': '<p>I understand this is perhaps a bit late answer... Non the less, you can give a try to the <a href="http://code.google.com/p/linqtocache/" rel="nofollow noreferrer">LinqToCache</a> project. It hooks a SqlDepdency on an arbitrary LINQ query, if possible, and provides active cache invalidation via the server side Query Notifications. The queries must be valid queries for notifications, see <a href="http://msdn.microsoft.com/en-us/library/ms181122.aspx" rel="nofollow noreferrer">Creating a Query for Notification</a>. Most Linq-to-sql queries conform to these restrictions, as long as the tables are specified using two-part names (<code>dbo.Table</code>, not only <code>Table</code>).</p>\n'}]
['https://Stackoverflow.com/questions/37374', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2975/']