持久化实体框架查询缓存

Persist Entity Framework query cache(持久化实体框架查询缓存)

本文介绍了持久化实体框架查询缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET MVC 5 Web 应用程序并使用 EF 6.1 访问我的数据库.
我有一些相当复杂的 LINQ 查询,编译时间长达 10 秒,然后在几毫秒内执行.EF 确实很好地缓存了这个查询,并且第二次执行查询时它会在这几毫秒内返回.
但是这个缓存不会持久化,所以每次应用重启时都需要重新编译查询,这又需要 10 秒.

I have an ASP.NET MVC 5 web-application and use EF 6.1 to access my DB.
I have some rather complex LINQ queries which take up to 10s to compile, but then execute in a few milliseconds. EF does cache this queries fine and the second time the query is executed it returns within this few milliseconds.
But this cache is not persisted so on every app-restart the query needs to be recompiled, which takes that 10s again.

有没有办法持久化这个查询缓存,让它在应用重启后仍然存在?

推荐答案

您可以使用编译查询:看这里 或 这里

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 query object must not go out of scope. You can handle this by either keeping it cached in the session or as application variable.

这篇关于持久化实体框架查询缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:持久化实体框架查询缓存