Lock cache while updating it?

  • Thread starter Thread starter Andrew Morton
  • Start date Start date
A

Andrew Morton

I am caching some data in VB.NET using System.Web.Caching, is it possible to
lock the cache so that other sessions attempting to access the same cache
wait when it is being updated? I have the cache using a sliding timeout and
a dependency on the text file its data is extracted from.
If it's relevant, based on current statistics, I do not expect more than
about 400 people to be accessing the web site at the same time.
I've seen it appears easy in C# but I couldn't find anything regarding VB.

Andrew
 
Andrew,

You make me curious, why would you do that? Seems to me a strange solution,
because you should than make a mechanisme for those other 399 who try to
access that cache in my idea. When it is, than what it the action on that
catch?

I am real curious about this how you did that.

Cor
 
Cor said:
Andrew,

You make me curious, why would you do that? Seems to me a strange
solution, because you should than make a mechanisme for those other
399 who try to access that cache in my idea. When it is, than what it
the action on that catch?

I am real curious about this how you did that.

Cor

The cached data is a lookup table which has the same data for every user.
The cache applies to the web application rather than per-session. Unless I
have misunderstood the docs.

Andrew
 
Andrew,

Clear,

Is it than not something as updating the tables, create a page to kill the
cache and let it go again, where I assume that the cache is created when it
is nothing?

I never used that cache, however that is from what I understand from it,
would probably as first try to do it.

Cor
 
Is it than not something as updating the tables, create a page to
kill the cache and let it go again, where I assume that the cache is
created when it is nothing?

Imagine this scenario:-
--------------------------
session1 starts and finds the cache is nothing and starts parsing the data
to create the cache

While session1 is parsing the data, session2 starts and finds the cache is
nothing, so it starts parsing the data to create the cache

session1 finishes parsing the data and creates the cache

session2 finishes parsing the data and creates the cache, except session1 is
still writing the cache so something goes wrong
--------------------------

Preferred scenario:-

session1 starts and finds the cache is nothing, locks the cache and starts
parsing the data to create the cache

While session1 is parsing the data, session2 starts and finds the cache is
locked, so it waits until the cache is unlocked.

Andrew
 
Andrew,

Did you test this and did it work as you describe. For me is the problem
that somebody once wrote here that an aspnet dll is running itself
automaticly on more threads for sessions.

When that is not true, than in my opinion can the effect not be as you
write.
However I never saw any documentation which tells something about the proces
of a webapplication dll.

Sorry

Cor
 
JD said:
The MSDN docs say Cache is thread safe.

How do I use that to make other threads pause when one thread is updating
the cache?

However, looking through the docs about thread safety lead me to SyncLock,
which I think is what I want.

Andrew
 
The Cache automatically synchronizes access to the data. I believe it uses a
multireader, single writer lock. In other words you don't have to do
anything.
 
But let me emphasize, the block of code that does -> {check if the cache
data is Nothing and if it is Nothing do the update to cache } must be
synchonized.
 
JD said:
But let me emphasize, the block of code that does -> {check if the
cache data is Nothing and if it is Nothing do the update to cache }
must be synchonized.

So, before I start reading the wrong things and getting myself confused,
should I be looking at System.Threading.Mutex?

(I'm thinking that maybe I should have one object in the application
accessible to all sessions, but I can't figure out how to do that.)

What I have boils down to

cachedData=cache("thesaurus")
if cachedData is nothing then
' apply some sort of lock to the non-existent cache
' build and cache cachedData
' release lock
end if

What I want to happen is that the other threads (if any at that time) wait
at line 'cachedData=cache("thesaurus")' until the lock is released because
building the data may take some time - it would be a waste of time for
several sessions to simultaneously do that and then wait in a queue for each
to write identical data to the cache.

Oh, other info: I'm using VB.NET in the 1.1 .net framework on WS2003.

Andrew
 
1. cachedData=cache("thesaurus")
2. if cachedData is nothing then
3. ' apply some sort of lock to the non-existent cache
4. ' build and cache cachedData
5. ' release lock
6. end if

You need the lock outside everything in the code above. Because at line 3,
two threads could have gotten here at the same time and one thread acquired
the lock populating the cache, then thread number two would acquire the lock
and repopulate the cache. Look at the code below.

' apply some sort of lock to the non-existent cache
cachedData=cache("thesaurus")
if cachedData is nothing then
' build and cache cachedData
end if
' release lock

Or you could do below and I think it is more efficient:

cachedData=cache("thesaurus")
if cachedData is nothing then
' apply some sort of lock to the non-existent cache
if cachedData is nothing then
' build and cache cachedData
end if
' release lock
end if

Did you look at populating the cache once during application startup event?
 
JD said:
Or you could do below and I think it is more efficient:

cachedData=cache("thesaurus")
if cachedData is nothing then
' apply some sort of lock to the non-existent cache
if cachedData is nothing then
' build and cache cachedData
end if
' release lock
end if

Thanks, I'll give that a go.
Did you look at populating the cache once during application startup
event?

I thought about that, but I suspect that changing the file the cache depends
on would then cause the application to restart which would destroy any
existing sessions(?)

Thanks for bearing with me with this, I think I'm trying to do to many
things in work at the same time so my brain isn't working very well.

Andrew
 
I thought about that, but I suspect that changing the file the cache
depends
on would then cause the application to restart which would destroy any
existing sessions(?)


If the file itself is under the web application directory, it may cause the
application to restart regardless. It depends on what types of files ASP.NET
watches to kick off a restart or if it just watches all files?
 

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