开发者

How do I cache query results using LINQ?

开发者 https://www.devze.com 2022-12-28 16:53 出处:网络
Is there any way to cache LINQ to SQL queries by looking at the parameters that were previously passed and bypass the database a开发者_C百科ll together?

Is there any way to cache LINQ to SQL queries by looking at the parameters that were previously passed and bypass the database a开发者_C百科ll together?

I know L2S caches some database calls, but I'm looking for a permanant solution as in, even if the applciation restarts, that cache reloads and never asks the database again.

Are there any frameworks for C#?


Linq-to-SQL wasn't really designed for this, the context is actually intended to have a short lifespan, not a long one. If you want to cache some specific queries, I'd recommend using a different medium, not actually relying on the context itself.

Here's one option to get an idea of what I mean: Caching the results of LINQ queries

Alternatively if there are a few things you just access non-stop that rarely change, consider just caching a List<T> or some other form of the result and forcing a re-cache when needed.


When you say that you'd like the results to cache across applications restarts, it sounds like you have a occasionally connected application that you'd like to keep a cache of results in case you lose network connection. If this is indeed the case, have a look at the Sync Framework, which does exactly that. It also allows you to change the cached data, and when the client regains connection, transmit the data and resolve any conflicts. Since it persists the cache to disk (usually by using SQL Server Compact), it will survive application restarts (or even computer restarts).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号