Persist Entity Framework Query Cache

I have an ASP.NET MVC 5 web application and use EF 6.1 to access my database.
I have some pretty complex LINQ queries that take up to 10 seconds to compile but then run in a few milliseconds. EF does cache these requests, and the second time the request is executed, it returns within a few milliseconds.
But this cache is not saved, so every time you restart the application, you must recompile the request, which takes 10 seconds.

Is there a way to keep this request cache so that it survives an application reload?

+5
source share
1 answer

You can use compiled queries: see here or here

static readonly Func<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>> s_compiledQuery2 = CompiledQuery.Compile<AdventureWorksEntities, Decimal, IQueryable<SalesOrderHeader>>( (ctx, total) => from order in ctx.SalesOrderHeaders where order.TotalDue >= total select order); 

But as mentioned here , the request object should not go beyond. You can handle this either by storing it in the cache in the session, or as an application variable.

0
source

Source: https://habr.com/ru/post/1203532/


All Articles