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


i'm working entity framework , have following scenario manage, because lot happens behind scenes, i'm not entirely sure best route be, , googling hasn't provided necessary answers far.

i have 100 products in product/supplier style database. there 10 tables used multiple times on each page - due widgets etc.

i cache results (these 100 products), plus other core entities, in order carry out approx 80% of queries need.

however, time time need access extended data. let's product features - appear on product page - accessed needed rather permanently cached. here thoughts out loud, appreciate comments , advice.

  1. the easy, worst answer put them in cache along original entity. although data wouldn't unwieldly, have significant impact on cached data size.

    • 100 products x 10 features - becomes 1000. become inefficient quickly?
  2. i query cached object product need, reattach database. i'm unsure of mechanics behind given i'm starting cached entity, , pretty messy, quickly.

  3. i query product need, complete additional data on demand. involve querying quite few tables, involve single product, 10 product detail lines, 10 feature rows etc.

all comments , advice appreciated.

when cache values, use asnotracking() detach values ef context.

the question how many information should cache depends on access frequency , cost retrieve information if not there. i'm using caching methods provided .net or azure because keep track of memory consumption , cause entries unloaded if not accessed long time. old article on msdn explains asp.net caching mechanism quite good.

i use abstract class hide caching details, , concrete implementation particular type:

public sealed class customeridcache : abstractcache<customeridcache, customer> {      private customeridcache()     {      }      /// <summary>     ///      /// </summary>     /// <returns></returns>     protected override timespan getexpiration()     {         return new timespan(0, 1, 0, 0);     }      /// <summary>     ///      /// </summary>     /// <param name="db"></param>     /// <param name="key"></param>     /// <returns></returns>     /// <exception cref="argumentexception"></exception>     protected override customer getsingleelement(itimejackcontext db, object[] key)     {         guid id = (guid)key[0];         customer customer = db.customers.asnotracking().singleordefault(x => x.id == id);         return customer;     }      /// <summary>     ///      /// </summary>     /// <param name="entry"></param>     /// <returns></returns>     protected override ienumerable<object> getkey(customer entry)     {         return new object[] { entry.id };     } 

usage:

  1. retrieve information cache (will loaded db if not there)

    guid customerid = ...; customer customer = customeridcache.instance.get(db, new[] { (object)customerid }); 
  2. invalidate cache information after updating database

    customeridcache.instance.invalidate(customer); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -