[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Erik Cassel

12/29/2006 7:06:00 AM

The Docs for Cache say that member functions aren't necessarily thread-safe.

Does HtppRuntime.Cache return the same Cache object for any thread? If so,
then wouldn't this be a dangerous threading issue?

How is Caching implemented? Are Cache.Get and Cache.Insert thread-safe
functions?

4 Answers

Manish Bafna

1/2/2007 6:09:00 AM

0

Hi,
Cache.Get and Cache.Insert are not thread-safe thread-safe functions.You
need to write following code to make them thread-safe:

public static readonly object somevariable = null
somevariable = (object)1;

lock(somevariable )
{
Cache.Get or Cache.Insert code comes here
}

Hope this helps you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.
"Erik Cassel" wrote:

> The Docs for Cache say that member functions aren't necessarily thread-safe.
>
> Does HtppRuntime.Cache return the same Cache object for any thread? If so,
> then wouldn't this be a dangerous threading issue?
>
> How is Caching implemented? Are Cache.Get and Cache.Insert thread-safe
> functions?
>

Manish Bafna

1/3/2007 5:26:00 AM

0

Hi,
I have written following class(pseduo code like) for cache which i think is
thread-safe.You can fine-tune more.Like you

using System;
using System.Threading;

namespace GenericCache
{

public class SynchronizedCache
{

ReaderWriterLock CacheLock;
string mKey;
object msomeobject;
//you can also pass cache expiration duration here
public Cache( string pKey,object someobject )
{
mKey = pkey;
msomeobject = someobject;
}

public static object FetchData( string mkey )
{
object tempItem;

CacheLock.AcquireReaderLock( -1 );

try
{
// Get the item from the cache
tempItem = Cache[ mkey ];
}
finally
{
CacheLock.ReleaseReaderLock();
}

if ( tempItem != null )
{
// If the item exists, return it to the user.
return tempItem;
}
}

public static void InsertIntoCache(string mkey,object msomeobject)
CacheLock.AcquireWriterLock( -1 );

try
{
// Add the new item to the cache.Although i have not written
exact syntax for cache insert.You can also cache expiration duration
in the constructor of the class and and use same here.
Cache.Insert( mkey, msomeobject);
}
finally
{
CacheLock.ReleaseWriterLock();
}
}
}

}

Now you can call this method in any class in following way :
SynchronizedCache.FetchData or SynchronizedCache.InsertIntoCache.

Although i have not written but you will also need to take care that
particular key does not exist before inserting any item/key into cache.You
can also write additional methods for clearing all items in cache or for
removing particualr item in cache.
Please correct me if i have missed out on something.Hope this helps you out.

Thanks and Regards,
Manish Bafna.
MCP and MCTS.

"Erik Cassel" wrote:

> The Docs for Cache say that member functions aren't necessarily thread-safe.
>
> Does HtppRuntime.Cache return the same Cache object for any thread? If so,
> then wouldn't this be a dangerous threading issue?
>
> How is Caching implemented? Are Cache.Get and Cache.Insert thread-safe
> functions?
>

Erik Cassel

1/3/2007 6:54:00 PM

0

Manish,

Thanks for the code snippet. If I understand correctly, then â??Cache.Insert(
mkey, msomeobject)� is the HttpRuntime Cache, right?

I still see a fundamental problem: What if some 3rd-party library uses
HttpRuntime.Cache as well? They wonâ??t know about my CacheLock object, since
that is specific to my code.

Caching is such a powerful feature that I'd expect control libraries,
business logic libraries, etc. will also take advantage of the application
cache. But without a common agreement on what lock to use there is serious
risk of random crashes or strange behavior, no?

Perhaps the answer is to just hope it works, or to use another system like
NCacheâ?¦

-Erik



"Manish Bafna" wrote:

> Hi,
> I have written following class(pseduo code like) for cache which i think is
> thread-safe.You can fine-tune more.Like you
>
> using System;
> using System.Threading;
>
> namespace GenericCache
> {
>
> public class SynchronizedCache
> {
>
> ReaderWriterLock CacheLock;
> string mKey;
> object msomeobject;
> //you can also pass cache expiration duration here
> public Cache( string pKey,object someobject )
> {
> mKey = pkey;
> msomeobject = someobject;
> }
>
> public static object FetchData( string mkey )
> {
> object tempItem;
>
> CacheLock.AcquireReaderLock( -1 );
>
> try
> {
> // Get the item from the cache
> tempItem = Cache[ mkey ];
> }
> finally
> {
> CacheLock.ReleaseReaderLock();
> }
>
> if ( tempItem != null )
> {
> // If the item exists, return it to the user.
> return tempItem;
> }
> }
>
> public static void InsertIntoCache(string mkey,object msomeobject)
> CacheLock.AcquireWriterLock( -1 );
>
> try
> {
> // Add the new item to the cache.Although i have not written
> exact syntax for cache insert.You can also cache expiration duration
> in the constructor of the class and and use same here.
> Cache.Insert( mkey, msomeobject);
> }
> finally
> {
> CacheLock.ReleaseWriterLock();
> }
> }
> }
>
> }
>
> Now you can call this method in any class in following way :
> SynchronizedCache.FetchData or SynchronizedCache.InsertIntoCache.
>
> Although i have not written but you will also need to take care that
> particular key does not exist before inserting any item/key into cache.You
> can also write additional methods for clearing all items in cache or for
> removing particualr item in cache.
> Please correct me if i have missed out on something.Hope this helps you out.
>
> Thanks and Regards,
> Manish Bafna.
> MCP and MCTS.
>
> "Erik Cassel" wrote:
>
> > The Docs for Cache say that member functions aren't necessarily thread-safe.
> >
> > Does HtppRuntime.Cache return the same Cache object for any thread? If so,
> > then wouldn't this be a dangerous threading issue?
> >
> > How is Caching implemented? Are Cache.Get and Cache.Insert thread-safe
> > functions?
> >

Alvin Bruney [MVP]

1/8/2007 3:51:00 AM

0

Well, the lock isn't required for a read, just a write. The cache API comes
with a warning that should be observed by the way. It's no different from a
global variable in that regard minus the type safety.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc

"Manish Bafna" <ManishBafna@discussions.microsoft.com> wrote in message
news:204A24CE-00BF-4B1C-8052-CBF3934FB5FA@microsoft.com...
> Hi,
> Cache.Get and Cache.Insert are not thread-safe thread-safe functions.You
> need to write following code to make them thread-safe:
>
> public static readonly object somevariable = null
> somevariable = (object)1;
>
> lock(somevariable )
> {
> Cache.Get or Cache.Insert code comes here
> }
>
> Hope this helps you out.
>
> Thanks and Regards,
> Manish Bafna.
> MCP and MCTS.
> "Erik Cassel" wrote:
>
>> The Docs for Cache say that member functions aren't necessarily
>> thread-safe.
>>
>> Does HtppRuntime.Cache return the same Cache object for any thread? If
>> so,
>> then wouldn't this be a dangerous threading issue?
>>
>> How is Caching implemented? Are Cache.Get and Cache.Insert thread-safe
>> functions?
>>