Re: string as lock key?
From: William Stacey [MVP] (staceywREMOVE_at_mvps.org)
Date: 11/05/04
- Next message: darrel: "Re: Web Clients, the role of ASP.NET and the Future of Web Development and Web Standards"
- Previous message: csharpcomputing.com: "RE: ASP.NET 2.0 beta. To early?"
- In reply to: Lloyd Dupont: "string as lock key?"
- Next in thread: Lloyd Dupont: "Re: string as lock key? : Thanks :)"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 5 Nov 2004 15:05:56 -0500
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:eu9tz3xwEHA.3228@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; > } > } > > >
- Next message: darrel: "Re: Web Clients, the role of ASP.NET and the Future of Web Development and Web Standards"
- Previous message: csharpcomputing.com: "RE: ASP.NET 2.0 beta. To early?"
- In reply to: Lloyd Dupont: "string as lock key?"
- Next in thread: Lloyd Dupont: "Re: string as lock key? : Thanks :)"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
Loading