string as lock key?

From: Lloyd Dupont (ld_at_NewsAccount.galador.net)
Date: 11/05/04


Date: Fri, 5 Nov 2004 20:58:06 +1100

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;
            }
        }
 



Relevant Pages

  • Re: Problem with BDC "View Profile" link
    ... but it seems to "short circuit" while it is reading the BDC cache. ... 71qj High Acquired Read lock on LobSystemInstance cache ... 71qj High Acquired Read lock on MethodInstance cache ... 71qj High Acquired Read lock on TypeDescriptor cache ...
    (microsoft.public.sharepoint.portalserver.development)
  • Re: How to free a locked object?
    ... timeout occurred while waiting to lock object xxx' ... Upon checking the v$session_wait, i found "latch: cache ... 'DR','Distributed Recovery', ... 'NA','Library Cache Pin', ...
    (comp.databases.oracle.server)
  • Re: ASP requests and locking
    ... Typically you would cache immutable content. ... There is no need to lock ... How many properties will you reading per request and will they ... Anthony Jones - MVP ASP/ASP.NET ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: queued spinlock code and results
    ... max number of times in a row that a lock is acquired, ... xadd-lock in cache takes 8.93ns ... static inline void unlock ... static int xlock_is_locked ...
    (Linux-Kernel)
  • Re: [PATCH] mm: PageLRU can be non-atomic bit operation
    ... so lock prefix is not needed. ... Think of the CPU cache like the page cache. ... The memory is the disk; ...
    (Linux-Kernel)

Loading