471,056 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,056 software developers and data experts.

HttpContext.Cache and lock

Mat
Hi,

I've stumbled onto a problem when using the caching object in ASP.Net.

I'm placing a static dataset to the cache as the data only changes
once a day. Whilst writing to the cache I'm using a lock using code
like below (just typed this in);

Cache thisCache = HttpContext.Current.Cache
lock(thisCache)
{
thisCache.Insert(....)
}

When reading from the cache I'm using

Dataset a = HttpContext.Current.Cache[cacheKey]
if (a==null)
{
// Info doesnt exist...Go and create the data and write it to the
cache!
}
else
{
//I've got the info...
}

All appears to work great...until the system is placed under load -
then strange things occur. For example a empty dataset is returned
(not a null dataset but a dataset which contains the dataset schema
but has no data)

Do anyone have any ideas? I've been informed by MS that the Cache
object is meant to be thread safe, so in theory the lock isn't even
required.

Cheers,

Mat
Nov 18 '05 #1
4 5177
Hi mat,
Have you set the expiration time for the caching. What is the
expiration time you have set for the cache?..What i suspect is, when the
cache duration that has been mentioned gets exceeded, the dataset gets
removed and the problem arises.
"Mat" wrote:
Hi,

I've stumbled onto a problem when using the caching object in ASP.Net.

I'm placing a static dataset to the cache as the data only changes
once a day. Whilst writing to the cache I'm using a lock using code
like below (just typed this in);

Cache thisCache = HttpContext.Current.Cache
lock(thisCache)
{
thisCache.Insert(....)
}

When reading from the cache I'm using

Dataset a = HttpContext.Current.Cache[cacheKey]
if (a==null)
{
// Info doesnt exist...Go and create the data and write it to the
cache!
}
else
{
//I've got the info...
}

All appears to work great...until the system is placed under load -
then strange things occur. For example a empty dataset is returned
(not a null dataset but a dataset which contains the dataset schema
but has no data)

Do anyone have any ideas? I've been informed by MS that the Cache
object is meant to be thread safe, so in theory the lock isn't even
required.

Cheers,

Mat

Nov 18 '05 #2
Any chance that the code pulling the DataSet out of the cache does any
manipulations?

--
Scott
http://www.OdeToCode.com

On 12 Aug 2004 14:25:57 -0700, ma*******@ntlworld.com (Mat) wrote:
Hi,

I've stumbled onto a problem when using the caching object in ASP.Net.

I'm placing a static dataset to the cache as the data only changes
once a day. Whilst writing to the cache I'm using a lock using code
like below (just typed this in);

Cache thisCache = HttpContext.Current.Cache
lock(thisCache)
{
thisCache.Insert(....)
}

When reading from the cache I'm using

Dataset a = HttpContext.Current.Cache[cacheKey]
if (a==null)
{
// Info doesnt exist...Go and create the data and write it to the
cache!
}
else
{
//I've got the info...
}

All appears to work great...until the system is placed under load -
then strange things occur. For example a empty dataset is returned
(not a null dataset but a dataset which contains the dataset schema
but has no data)

Do anyone have any ideas? I've been informed by MS that the Cache
object is meant to be thread safe, so in theory the lock isn't even
required.

Cheers,

Mat


Nov 18 '05 #3
the HttpContext Cache lifetime is only for the current request. it used
mainly to pass context info between modules, during ta single request. this
means you are building the dataset on every page request, so you probably
have a bug under load.

note: the cache are thread safe in that setting and getting an object from
the cache is thread safe, it does not make accessing properties on the
cached objects thread safe. also code like:

Dataset a = HttpContext.Current.Cache[cacheKey];
if (a==null)
{
// Info doesnt exist...Go and create the data and write it to the
cache!
}
else
{
//I've got the info...
}

while threadsafe, in race conditions multiple threads could detect the
object not in the cache, build and insert it, with the last thread writing
to the cache winning.

-- bruce (sqlwork.com)
"Mat" <ma*******@ntlworld.com> wrote in message
news:ca**************************@posting.google.c om...
Hi,

I've stumbled onto a problem when using the caching object in ASP.Net.

I'm placing a static dataset to the cache as the data only changes
once a day. Whilst writing to the cache I'm using a lock using code
like below (just typed this in);

Cache thisCache = HttpContext.Current.Cache
lock(thisCache)
{
thisCache.Insert(....)
}

When reading from the cache I'm using

Dataset a = HttpContext.Current.Cache[cacheKey]
if (a==null)
{
// Info doesnt exist...Go and create the data and write it to the
cache!
}
else
{
//I've got the info...
}

All appears to work great...until the system is placed under load -
then strange things occur. For example a empty dataset is returned
(not a null dataset but a dataset which contains the dataset schema
but has no data)

Do anyone have any ideas? I've been informed by MS that the Cache
object is meant to be thread safe, so in theory the lock isn't even
required.

Cheers,

Mat

Nov 18 '05 #4
On Fri, 13 Aug 2004 10:42:50 -0700, "bruce barker"
<no***********@safeco.com> wrote:
the HttpContext Cache lifetime is only for the current request. it used
mainly to pass context info between modules, during ta single request. this
means you are building the dataset on every page request, so you probably
have a bug under load.


bruce: I believe you are thinking of the HttpContext.Items collection.

--
Scott
http://www.OdeToCode.com
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by George Ter-Saakov | last post: by
3 posts views Thread by James | last post: by
13 posts views Thread by Andrew Morton | last post: by
3 posts views Thread by Raj | last post: by
4 posts views Thread by msch-prv | last post: by
6 posts views Thread by =?Utf-8?B?emlubw==?= | last post: by
3 posts views Thread by Madhur | last post: by
8 posts views Thread by SpaceMarine | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.