This is the scenario:
Thread A ask for All Books in French
Thread B ask for All Books in Arabic
if Thread A is in the lock area, Thread B will be waiting outside the lock area for Thread A to finish.
Because there is only one "place" in the code where Thread B can get his Data.
I want to make a lock based on the Key in the cache which mean like this:
if(Cache["BooksInFrench"] == null)
{
lock("BooksInFrench")
{
if(Cache["BooksInFrench"] == null)
{
object d = GetFromDB();
cache["BooksInFrench"] = d;
}
}
the same for BooksInArabic etc.... so this way if i have 200 connections to my site and 10 of them ask for BooksInFrench the rest of the requests(BooksInArabic, BooksInHebrew.......) can run and get thier Data beacuse their request is not in the lock area.
my current code looks like this: http://pastebin.com/LFhxgHDM
i think the double locking is not very good... do you see any other solution/improvement?