Hey Guys,
I am looking to find the best way to cache the DB Lookup Tables which consists of about 75 table.
I want to cache these tables data to use them in my application so i won't open a connection with the DB each time i need them.
so Here is what i am doing:
1- i have created i static class contains static properties to each lookup table called (MyApplicatioCache).
2- each property in (get) i am filling it with the intended data from DB.
3- Put the result in HttpRuntime.Cache["PropertyName"]
4- each time i
GET this lookup table data i make a check if the HttpRuntime.Cache["PropertyName"] Not equal Null.
5- if yes then i am getting it from cahce else i am getting it from DB
6- Finally, I am invoking all properties at application start event in global.asax
Untill now everything is good But Recently i've faced a performance issue and i can't solve it which is:
if i wanted the cache object (Payer) to be updated from DB i am doing this:
MyApplicationCache.Payer = null; //Sets HttpRuntime.Cache["Payer"] = null so if i requested it again it reloads From DB.
list<payer> payerList = MyApplicationCache.Payer;
Now the Performance problem raises:
1- PayerList in DB are about 1700 record.
2- each payer object has a List property called PayerBranches which requires looping on all PayerList List and getting PayerBranches For Each PayerList Item.
//MyApplicationCache Payer Property:
public static List<LDM.DataEntityTier.Payer> Payer
{
get
{
if (HttpRuntime.Cache["Payer"] != null) return (List<LDM.DataEntityTier.Payer>)HttpRuntime.Cache["Payer"];
else
{
using (LDM.DataAccess.OracleManager OracleManager = new LDM.DataAccess.OracleManager())
{
OracleManager.OpenConnection();
List<LDM.DataEntityTier.Payer> result = new LDM.DataService.PayerService().GetPayersListWithFullName(3, OracleManager, "UTC");
List<PayerBranches> payerBranchesList = new LDM.DataService.PayerBranchesService().GetListOfObject(OracleManager, "UTC");
OracleManager.CloseConnection();
foreach (Payer payerItem in result)
{
payerItem.PayerBranches = new List<PayerBranches>();
foreach (PayerBranches item in payerBranchesList.FindAll(x => x.PayerID == payerItem.Id))
{
payerItem.PayerBranches.Add(item);
}
}
HttpRuntime.Cache["Payer"] = result;
return result;
}
}
}
set
{
if (value == null)
{
HttpRuntime.Cache.Remove("Payer");
}
}
}
This Problem faced me with each property that have a list in it
i don't know if there is a better way to cache data or there is a problem in my code
That's the way i have worked.
Is there is any better way to do that???