Caching data from a DataContext

We use the Linq-to-SQL DataContext in a web application that provides read-only data and never updates (to do this, force ObjectTrackingEnabled = false.

Since the data never changes (except for periodic configuration updates), it seems futile to overload it from SQL Server with a new DataContext for each web request.

We tried to cache the DataContext in the Application object for all requests, but this generated a lot of errors, and our research shows that it was a bad idea, DataContext should be disposed of within the same unit of work, an insecure stream, etc. etc.

So, since the DataContext is intended for the data access mechanism, and not for the data warehouse, we need to look for the caching of the data that we receive from it, and not the context itself.

Prefers to do this with the entities and collections themselves, so the code can be agnostic as to whether it deals with cached or "fresh" data.

How can this be done safely?

First, I need to make sure the objects and collections are fully loaded before I delete the DataContext. Is there a way to load the entire database from the database as efficiently as possible?

Secondly, I’m sure that maintaining references to entities and collections is a bad idea, because it will be

(a) cause damage to objects when the DataContext goes out of scope or

(b) preventing the DataContext from leaving the scope

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

+4
source share
2 answers

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

Caching will be:

  • cannot be scalable
  • I need additional code for synchronization, I assume that your data is not completely static in the database?
  • additional code will be error prone
  • will quickly eat your web server’s memory, the next thing you could solve is the memory problem on your web server.
  • will not work very well when you need to load the balance of your website.

[change]

If I need to cache 5 MB data, I would use a Cache object, possibly with lazy loading. I would use a collection of lightweight collections, such as ReadonlyCollection<T> , Collectino<T> . I would probably use ReadonlyDictionary<TKey, TValue> also for a quick search in memory. I would use LINQ-to-Objects to manage collections.

+1
source

You want to cache data received from a DataContext, not the DataContext itself. I usually reorganize generally accepted data into methods with which I can implement quiet caching, something like this (you may need to add logic with a stream):

 public class MyBusinssLayer { private List<MyType> _myTypeCache = null; public static List<MyType> GetMyTypeList() { if (_myTypeCache == null) { _myTypeCache = // data retrieved from SQL server } return _myTypeCache } } 

This is the easiest template to use and will cache for a single web request. To cache longer periods, store content in longer storage, such as Application or Cache . For example, to save Application level data, use this template type.

 public static List<MyType> GetMyTypeList() { if (Application["MyTypeCacheName"] = null) { Application["MyTypeCacheName"] = // data retrieved from SQL server } return (List<MyType>)Application["MyTypeCacheName"]; } 

This will be data that almost never changes, for example, a static set of state types to choose from in DropDownList . For more volatile data, you can use Cache with a waiting period, which should be selected depending on how often the data changes. If Cache items can be invalidated manually with code, if necessary, or by using status checks, such as SqlCacheDependency .

Hope this helps!

0
source

All Articles