By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,869 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

string as lock key?

P: n/a
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other lock,
could they?
could this be a problem ?

here is my code I wonder about:
-----------
public static string LogsByMonthCacheKey(string blogname)
{
return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
}
public static DataTable GetLogsByMonth(string blogname)
{
string ck = LogsByMonthCacheKey(blogname);
lock(ck) // lock with a unique string, is it alright ?
{
DataTable result = (DataTable) GetCache(ck);
if (result != null)
return result;

SqlConnection conn = DBConnectionPool.Get();
try
{
SqlCommand select = conn.CreateCommand();
select.CommandText = "dbo.gb_EntryByMonth";
select.CommandType = CommandType.StoredProcedure;
select.Parameters.Add(new SqlParameter("@blogname",
blogname));

SqlDataAdapter sda = new SqlDataAdapter(select);
result = new DataTable();
sda.Fill(result);
}
finally { DBConnectionPool.Let(conn); }

AddCache(ck, result);
return result;
}
}

Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I guess my question is why lock at all? If you don't lock, the worse
that'll happen is X request (let's say 25, which is probably more than you'd
ever get) will hit the function at the same time, and most of those will get
a null ack from GetCache(ck) - thus making them hit the database. They'll
then each fill the cache object up with the same key...ok so not the most
efficient, but that's under pretty bad (and unlikely situations)...after
that though, your using the cache and everything is fast.

On the flip side, if you lock, even when things are cached you'll have to
lock all the other threads...so you are turning this great multithread
environment into a sequential system *yawn*. I could understand it if you
had concerns about things actually getting out of synch and causing some
serious problems..but the only side effect I see (And I could be wrong) is
that you might hit the database one or two extra times between the time the
first cache check fails and it puts it in the cache...seems over the top...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other lock, could they?
could this be a problem ?

here is my code I wonder about:
-----------
public static string LogsByMonthCacheKey(string blogname)
{
return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
}
public static DataTable GetLogsByMonth(string blogname)
{
string ck = LogsByMonthCacheKey(blogname);
lock(ck) // lock with a unique string, is it alright ?
{
DataTable result = (DataTable) GetCache(ck);
if (result != null)
return result;

SqlConnection conn = DBConnectionPool.Get();
try
{
SqlCommand select = conn.CreateCommand();
select.CommandText = "dbo.gb_EntryByMonth";
select.CommandType = CommandType.StoredProcedure;
select.Parameters.Add(new SqlParameter("@blogname",
blogname));

SqlDataAdapter sda = new SqlDataAdapter(select);
result = new DataTable();
sda.Fill(result);
}
finally { DBConnectionPool.Let(conn); }

AddCache(ck, result);
return result;
}
}

Nov 18 '05 #2

P: n/a

"Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other
lock, could they?
could this be a problem ?


That will work fine, so long as you intern the strings (like you're doing).
Just remember that AddCache and GetCache need to be thread-safe since
locking on the cache keys does not serialize access to the cache object.

David
Nov 18 '05 #3

P: n/a
Hi Lloyd:

I generally avoid locking on stirng types because they are such wierd
beasts, you never know who else might have a reference to the same
string.

Consider:

String s1 = "MyTest";
String s2 = new
StringBuilder().Append("My").Append("Test").ToStri ng();
String s3 = String.Intern(s2);
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.

I'm still debating if you really need a lock here. The worst that
could happen is you might end up with extra database queries if
multiple requests come in and don't find an entry in the cache. Once
the cache is warm you'll then have the overhead of many find grained
locks. The lock is relatively cheap as locks go, but how many of them
might you have?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Fri, 5 Nov 2004 20:58:06 +1100, "Lloyd Dupont"
<ld@NewsAccount.galador.net> wrote:
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other lock,
could they?
could this be a problem ?

here is my code I wonder about:
-----------
public static string LogsByMonthCacheKey(string blogname)
{
return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
}
public static DataTable GetLogsByMonth(string blogname)
{
string ck = LogsByMonthCacheKey(blogname);
lock(ck) // lock with a unique string, is it alright ?
{
DataTable result = (DataTable) GetCache(ck);
if (result != null)
return result;

SqlConnection conn = DBConnectionPool.Get();
try
{
SqlCommand select = conn.CreateCommand();
select.CommandText = "dbo.gb_EntryByMonth";
select.CommandType = CommandType.StoredProcedure;
select.Parameters.Add(new SqlParameter("@blogname",
blogname));

SqlDataAdapter sda = new SqlDataAdapter(select);
result = new DataTable();
sda.Fill(result);
}
finally { DBConnectionPool.Let(conn); }

AddCache(ck, result);
return result;
}
}


Nov 18 '05 #4

P: n/a
Not 100% on that usage of locking on a interned string. IIRC, the runtime
is free to move around interns so compact space, so it may be possible for
this lock not to work as expected. To be safe, just lock on the
typeof(class) or a static readonly object set in your static constructor.
Also note that interning is slow. Not sure about all you need for your app,
but I may also concider using a hash table for your cache and maybe even
your database.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other lock, could they?
could this be a problem ?

here is my code I wonder about:
-----------
public static string LogsByMonthCacheKey(string blogname)
{
return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
}
public static DataTable GetLogsByMonth(string blogname)
{
string ck = LogsByMonthCacheKey(blogname);
lock(ck) // lock with a unique string, is it alright ?
{
DataTable result = (DataTable) GetCache(ck);
if (result != null)
return result;

SqlConnection conn = DBConnectionPool.Get();
try
{
SqlCommand select = conn.CreateCommand();
select.CommandText = "dbo.gb_EntryByMonth";
select.CommandType = CommandType.StoredProcedure;
select.Parameters.Add(new SqlParameter("@blogname",
blogname));

SqlDataAdapter sda = new SqlDataAdapter(select);
result = new DataTable();
sda.Fill(result);
}
finally { DBConnectionPool.Let(conn); }

AddCache(ck, result);
return result;
}
}


Nov 18 '05 #5

P: n/a
Thanks all!

I was concerning locking on a string and this cncern seems to be shared....
Also I was wondering about the real usefulness lock as you were too...

Well, maybe I just drop the lock then, hey!
Thanks ;)

"Lloyd Dupont" <ld@NewsAccount.galador.net> wrote in message
news:eu**************@TK2MSFTNGP10.phx.gbl...
in an ASP.NET page I have some static method which get value for the cache
or, if the cache is empty, query the database, put the value in the cache
and return it.

because ASP.NET is thread intensive I was thinking to lock these method,
using a fine grained lock.
the best lock I was thinking about was the string key in the cache!
however I have one concern, these unique string might be used in other
lock, could they?
could this be a problem ?

here is my code I wonder about:
-----------
public static string LogsByMonthCacheKey(string blogname)
{
return string.Intern(BLOG_KEY + ":LogsByMonth:" + blogname);
}
public static DataTable GetLogsByMonth(string blogname)
{
string ck = LogsByMonthCacheKey(blogname);
lock(ck) // lock with a unique string, is it alright ?
{
DataTable result = (DataTable) GetCache(ck);
if (result != null)
return result;

SqlConnection conn = DBConnectionPool.Get();
try
{
SqlCommand select = conn.CreateCommand();
select.CommandText = "dbo.gb_EntryByMonth";
select.CommandType = CommandType.StoredProcedure;
select.Parameters.Add(new SqlParameter("@blogname",
blogname));

SqlDataAdapter sda = new SqlDataAdapter(select);
result = new DataTable();
sda.Fill(result);
}
finally { DBConnectionPool.Let(conn); }

AddCache(ck, result);
return result;
}
}

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.