c# - Should I add Locks or TransactionScope when using .Net Cache? -


i’m using httpcontext.current.cache cache data db (.net 4 web application).

i want make sure don’t run threading synchronization problem.

scenario: 3 users pointing same company object:

user a:

profile.company.name  = “compx”; profile.company.desc  = “compxdesc”; profile.company.update(); //update db 

user b:

string name = profile.company.name; 

user c:

profile.company.name  = “compy”; profile.company.update(); //update db 

questions:

  1. does cache provide type of locking?

  2. should add locks readerwriterlockslim (how exactly)?

existing code:

profilebll: public companybll company    {                {                 return companybll.getbyid(this.company_id);         }     }  // httpcontext.current.cache public static companybll getbyid(int company_id) {         string key = "getbyid_" + company_id.tostring();         companybll ret = null;         if (cache[key] != null) {             ret = (companybll)cache[key];         }         else         {             ret = dal_company<companybll>.getbyid(company_id);             cache[key] = ret;         }         return ret; } 

another option add transactionscope on db update:

user a:

using (transactionscope scope = new transactionscope()){ profile.company.name  = “compx”; profile.company.desc  = “compxdesc”; profile.company.update(); //update db scope.complete(); //commit trans } 

user b:

string name = profile.company.name; 

will solve threading problem?

thanks

you have nothing worry about. class thread safe.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -