Wednesday, 15 June 2011

c# - How to work with cached Entity Framework data -


I am currently working with the entity framework and the following scenario is to manage, but because of the very behind the scenario Something happens I'm not completely sure what the best route will be, and Googling has not provided the necessary answers so far.

My product / supplier style database has around 100 products due to widgets etc. - About 10 tables are used multiple times on each page.

I currently cash out the results (these 100 products), as well as other fundamental institutions about 80% of the questions I need.

However, from time to time, I need access to extended data. Suppose product attributes - they are only displayed on a product page - so that permanently to reach the need instead of cached. They say my thoughts out loud, I appreciate any comments and advice.

  1. Easy, but perhaps the worst answer to be put in the cache with the original unit. Although the data will not be too high, it will have significant impact on the cached data size.

    • 100 products x 10 features - suddenly become 1000. Does it get disabled very quickly?
  2. I can query my catch object for the product I need, then add it again to the database. I am unsure about the mechanic given to me that I Starting with the cached unit, and it can be very dirty, very fast

  3. I will only complete the product I need, complete with all the extra data on demand, it will involve asking a lot of tables, but only one product, 10 product description lines, 10 Attribute lines, etc.

All comments and advice were appreciated.

When I cache values, I always use those values ​​to separate from the EF reference I do

The question on how much information you should cache, is that if it is not, depending on the access frequency and cost to get the information. I am generally using caching methods provided by .NET or Azure because they keep track of memory consumption and if there is a reason to unload entries when not entered for a long time, a bit ASP on MSDN. The net caching mechanism is quite good.

I usually use to hide caching details, and a solid implementation for the particular type:

  public sealed class customer ID cache: abstrark catches & lt; Customer ID Cache, Client & gt; {Private customer ID cache () {} /// & lt; Summary & gt; ////// & lt; / Summary & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; Protected override timespane gate accession () {new timespan (0, 1, 0, 0); } /// & lt; Summary & gt; ////// & lt; / Summary & gt; /// & lt; Param name = "db" & gt; & Lt; / Param & gt; /// & lt; Ultimate name = "key" & gt; & Lt; / Param & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; /// & lt; Exception cref = "argument exception" & gt; & Lt; / Exception & gt; Protected override customer GetSingleElement (ITimeJackContext DB, Object [] key) {Guide ID = (Guide) Key [0]; Customer customer = db.Customers.AsNoTracking (). SingleOrDefault (x = & gt; x.id == ID); Return customer; } /// & lt; Summary & gt; ////// & lt; / Summary & gt; /// & lt; Ultimate name = "entry" & gt; & Lt; / Param & gt; /// & lt; Returns & gt; & Lt; / Returns & gt; Protected Override IEnumerable & lt; Object & gt; GetKey (customer entry) {new item [return] [entry.Id}; }  

Usage:

  1. Retrieve information from the cache (if not there, will be loaded with DB)

      Guid Customer ID = ...; Customer customer = Customer IDcatch.instance.Tet (dB, new [] ((object) customer ID});  
  2. Disable

      CustomerIdCache.Instance.Invalidate (Customer);  

No comments:

Post a Comment