Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you implement caching in Linq to SQL?

We've just started using LINQ to SQL at work for our DAL & 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?

We're working in a web environment (IIS) if that makes a difference. I know this may well end up being a subjective question, but I still think the info would be valuable.

EDIT: 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.

like image 408
Glenn Slaven Avatar asked Sep 01 '08 02:09

Glenn Slaven


People also ask

Are LINQ queries cached?

By calling the AsCacheable extension method, our LINQ queries get cached for the specified duration.

What you can do with LINQ to SQL?

LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.

How does SQL caching work?

In SQL Server, the buffer cache is the memory that allows you to query frequently accessed data quickly. When data is written to or read from a SQL Server database, the buffer manager copies it into the buffer cache (aka the buffer pool).

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.


5 Answers

My LINQ query result cache is probably just what you're looking for.

var q = from c in context.Customers
        where c.City == "London"
        select new { c.Name, c.Phone };

var result = q.Take(10).FromCache();

Pete.

like image 52
Pete Montgomery Avatar answered Sep 22 '22 09:09

Pete Montgomery


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).

The above answer works on one machine only. To be able to use this on many machines, use memcached as your caching solution. Good luck!

like image 25
Thomas Lundström Avatar answered Sep 25 '22 09:09

Thomas Lundström


It's right under your nose:

List<TableItem> myResult = (from t in db.Table select t).ToList();

Now, just cache myResult as you would have cached your old DAL's returned data.

like image 42
Greg Hurlman Avatar answered Sep 21 '22 09:09

Greg Hurlman


I found this post, which offers an extension method as a means of caching the LINQ objects.

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

The repository pattern tends to limit the usefullness of Linq, since (without reimplementing IQueryable) caching must bge performed outside of Linq statement.

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.

Anyone who's managed to solve this problem in the wild within a highly concurrent web project, please chime in and save the world! :)

like image 27
Mark Avatar answered Sep 22 '22 09:09

Mark


I understand this is perhaps a bit late answer... Non the less, you can give a try to the LinqToCache 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 Creating a Query for Notification. Most Linq-to-sql queries conform to these restrictions, as long as the tables are specified using two-part names (dbo.Table, not only Table).

like image 22
Remus Rusanu Avatar answered Sep 22 '22 09:09

Remus Rusanu