简体   繁体   English

缓存实体框架数据并跟踪其变化?

[英]Cache Entity Framework data and track its changes?

What I want is pretty simple conceptually but I can't figure out how it would be best to implement such a thing. 我想要的是概念上非常简单,但我无法弄清楚如何最好地实现这样的事情。

In my web application I have services which access repositories which access EF which interacts with the SQL Server database. 在我的Web应用程序中,我有访问存储库的服务,这些存储库访问与SQL Server数据库交互的EF。 All of these are instanced once per web request. 所有这些都是每个Web请求实例一次。

I want to have an extra layer between the repositories and EF (or the services and the repositories?) which statically keeps track of objects being pulled from and pushed to the database. 我想在存储库和EF(或服务和存储库?)之间有一个额外的层,它静态地跟踪从数据库中提取和推送到数据库的对象。

The goal, assuming DB-access is only accomplished through the application, would be that we know for a fact that unless some repository access EF and commits a change, the object set didn't really change. 假设数据库访问仅通过应用程序完成,目标就是我们知道一个事实,即除非某些存储库访问EF并提交更改,否则对象集并没有真正改变。

An example would be: 一个例子是:

Repository invokes method GetAllCarrots(); Repository调用方法GetAllCarrots();

GetAllCarrots() performs a query on SQL Server retrieving a List<Carrot> , if nothing else happens in between, I would like to prevent this query from being actually made on the SQL Server each time (regardless of it being on a different web request, I want to be able to handle that scenario) GetAllCarrots()执行查询SQL Server检索List<Carrot>的查询,如果之间没有其他任何事情发生,我想防止每次在SQL Server上实际进行此查询(无论它在不同的Web请求上) ,我希望能够处理这种情况)

Now, if a call to BuyCarrot() adds a Carrot to the table, then I want that to invalidate the static cache for Carrot s, which would make GetAllCarrots(); 现在,如果对BuyCarrot()的调用将Carrot添加到表中,那么我希望它能使Carrot的静态缓存失效,这会产生GetAllCarrots(); require a query to the database once again. 需要再次查询数据库。

What are some good resources on database caching? 有什么关于数据库缓存的好资源?

You can use LinqToCache for this. 您可以使用LinqToCache

It allows you to use the following code inside your repository: 它允许您在存储库中使用以下代码:

var queryTags = from t in ctx.Tags select t;
var tags = queryTags.AsCached("Tags");
foreach (Tag t in tags)
{
  ...
}

The idea is that you use SqlDependency to be notified when the result of a query changes. 我们的想法是,当查询结果发生变化时,您将使用SqlDependency进行通知。 As long as the result doesn't change you can cache it. 只要结果不变,您就可以缓存它。

LinqToCache keeps track of your queries and returns the cached data when queried. LinqToCache会跟踪您的查询并在查询时返回缓存的数据。 When a notification is received from SqlServer the cache is reset. 从SqlServer收到通知时,将重置缓存。

I recommend you reading the http://rusanu.com/2010/08/04/sqldependency-based-caching-of-linq-queries/ . 我建议你阅读http://rusanu.com/2010/08/04/sqldependency-based-caching-of-linq-queries/

I had a similar challenge, and due to EF's use and restrictions, i've decided to implement the cache as an additional service between the client and server's service, using an IoC. 我遇到了类似的挑战,由于EF的使用和限制,我决定使用IoC将缓存实现为客户端和服务器服务之间的附加服务。 Monitoring all service methods that could affect the cached data. 监视可能影响缓存数据的所有服务方法。

Off course is not a perfect solution when you have a farm of servers running the services, if the goal is to support multiple servers i would implement using the SqlDependency. 当你有一个运行服务的服务器场时,如果目标是支持我将使用SqlDependency实现的多个服务器,那么当然不是一个完美的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM