从DataContext的缓存数据 [英] Caching data from a DataContext

查看:204
本文介绍了从DataContext的缓存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正处在一个Web应用程序,提供只读数据的应用程序,并永远不会更新使用LINQ到SQL的DataContext(已设置ObjectTrackingEnabled = false来执行本。

由于从未改变(除了偶尔的配置更新)似乎浪费了数据从SQL Server为每个Web请求一个新的DataContext被重新加载它。

我们试图在Application对象所有请求使用缓存DataContext的,但它产生了很多错误。我们的研究,因为表明,这是一个坏主意,DataContext的应在同一工作单元内进行处理,不是线程安全的,等等等等。

因此​​,既然DataContext的,就是要一个数据访问机制,而不是数据存储,我们需要寻找在高速缓存,我们从它那里得到的数据,而不是上下文本身。

请问preFER与实体和集合自己因此code可能是不可知它是否是处理缓存或新鲜的数据来做到这一点。

怎么可以这样做的安全性?

首先,我需要确保实体和集合之前,我处置的DataContext都满载。有没有办法迫使一切满载从数据库方便?

二,我是pretty确保存储的实体和集合引用是一个坏主意,因为它要么

(a)安排时,在DataContext超出范围的实体损坏或

(二)prevent DataContext的外出范围<​​/ P>

所以,我要克隆EntitySets,并将其储存?如果是这样,怎么样?或者有什么何去何从?


解决方案

这是不完全的回答你的问题,但我建议避免在网站端缓存。结果
我宁愿集中精力优化数据库查询更快,更高效的数据检索。结果

缓存将:


  • 不具有可扩展性

  • 需要额外的code同步,我假设你的数据不是完全静态的DB?

  • 额外的code将错误容易发生

  • 会吃掉你的web服务器的内存快,你可能最终解决,接下来的事情就是你的Web服务器上的内存问题

  • 将不能很好地工作,当你需要负载均衡您的网站

如果我需要缓存5MB的数据,我会用的缓存对象,可能与延迟加载。我会用一组轻量级的藏品,如 ReadOnlyCollection还&LT; T&GT; Col​​lectino&LT; T&GT; 。我可能会使用 ReadonlyDictionary&LT; TKEY的,TValue&GT; 还为内存快速搜索。我会使用LINQ到对象的集合来处理。

We're using a Linq-to-SQL DataContext in a web application that provides read-only data to the application and is never updated (have set ObjectTrackingEnabled = false to enforce this.

Since the data never changes (except for occasional config updates) it seems wasteful to be reloading it from SQL Server with a new DataContext for each web request.

We tried caching the DataContext in the Application object for all requests to use, but it was generating a lot of error and our research since shows that this was a bad idea, DataContext should be disposed of within the same unit of work, not thread safe, etc, etc.

So since the DataContext is meant to be a data access mechanism, not a data store, we need to be looking at caching the data that we get from it, not the context itself.

Would prefer to do this with the entities and collections themselves so the code can be agnostic about whether it is dealing with cached or "fresh" data.

How can this be done safely?

First, I need to make sure that the entities and collections are fully loaded before I dispose of the DataContext. Is there a way to force a full load of everything from the database conveniently?

Second, I'm pretty sure that storing references to the entities and collections is a bad idea, because it will either

(a) cause the entities to be corrupted when the DataContext goes out of scope or

(b) prevent the DataContext from going out of scope

So should I clone the EntitySets and store them? If so, how? Or what's the go here?

解决方案

This is not exactly an answer to your question, but I suggest avoiding caching on web site side.
I would rather focus on optimizing database queries for faster and more efficient data retrieval.

Caching will:

  • not be scalable
  • need extra code for synchronization, I assume your data isn't completely static in DB?
  • extra code will be bug prone
  • will eat up memory of your web server quickly, the next thing you might end up addressing is memory issue on your web server
  • will not work very well, when you need to load-balance your web site

[Edit]

If I needed to cache 5MB data, I would use Cache object, probably with lazy loading. I would use a set of lightweight collections, like ReadonlyCollection<T>, Collectino<T>. I would probably use ReadonlyDictionary<TKey, TValue> also for quick searches in the memory. I would use LINQ-to-Objects to manipulate with the collections.

这篇关于从DataContext的缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆