System.Web.Caching.Cache

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi

The documentation for System.Web.Caching.Cache states that it is
"thread safe".

Does this mean that if I access System.Web.Caching.Cache from several
places in my application access to the items in the cache is
synchronised so I don't get problems with setting/getting the same item
at the same time?




Thanks,
Peter
 
Peter said:
Hi

The documentation for System.Web.Caching.Cache states that it is
"thread safe".

Does this mean that if I access System.Web.Caching.Cache from several
places in my application access to the items in the cache is
synchronised so I don't get problems with setting/getting the same item
at the same time?

Thats correct. Bear in mind though that any mutable reference types
included in the cache may not be be threadsafe themselves. Also the cache
is not really designed to support the idea of modifying an existing value.
Its designed to allow content to be added, read and dropped in a threadsafe
manner.
 
Anthony said:
Thats correct. Bear in mind though that any mutable reference types
included in the cache may not be be threadsafe themselves. Also the
cache is not really designed to support the idea of modifying an
existing value. Its designed to allow content to be added, read and
dropped in a threadsafe manner.

Thanks. I have one item I want to add to the cache, get from the cache,
and sometimes remove from the cache.

The item I want to add is a Dictionary containing various values.

I have one place which instantiates the dictionary, populates it, and
puts it in the cache.

I have several places which get the dictionary from the cache, use the
values in it (put not change the contents of it).

I have one place which will remove the dictionary from the cache. After
the Dictionary is removed, it will be re-instantiated, populated, and
put back in the cache.

Does that sound like it will give problems? What would happen if at one
place I get the dictionary from the cache, start performing tasks using
its contents, and then remove the dictionary from the cache while it is
still being used?


/Peter
 
Peter said:
Thanks. I have one item I want to add to the cache, get from the cache,
and sometimes remove from the cache.

The item I want to add is a Dictionary containing various values.

I have one place which instantiates the dictionary, populates it, and
puts it in the cache.

I have several places which get the dictionary from the cache, use the
values in it (put not change the contents of it).

I have one place which will remove the dictionary from the cache. After
the Dictionary is removed, it will be re-instantiated, populated, and
put back in the cache.

Does that sound like it will give problems?

A simple dictionary does not change its state whilst being read so as long
as you are sure it doesn't get modified then you should be ok. However
being sure that it doesn't get modified is best delivered by using a
dictionary that can't be modified. Unfortuantely the framework doesn't
provide a ReadOnlyDictionary so that'll be something you'd need to build
yourself. I would recommend you got to this trouble since time passes and
people make mistakes.
What would happen if at one
place I get the dictionary from the cache, start performing tasks using
its contents, and then remove the dictionary from the cache while it is
still being used?

In order to avoid extensive locking any code accessing the dictionary would
do this:-

Dictionary dict = Cache["keyForDict"];

Code can now just read and us values in from dict. Remember though you
really need to build a ReadOnlyDictionary. I should add the any other
references type that may be stored in this dicitionary should similarly be
immutable and so should anything they in turn reference and so on.

If after this line the item that has the key "keyForDict" is removed or
replaced in the cache the object being referenced by dict is unaffected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top