473,237 Members | 1,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,237 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 5316
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: George Ter-Saakov | last post by:
What is HttpContext.Cache for ? The usual HttpRequest lasts miliseconds. Why would someone to cache anything? We do have HttpContext.Items to keep any extra info during the request. Can...
3
by: James | last post by:
If I have set the session state for an asp.net application to be out of process how does this affect the HttpContext.Cache? What I want to know it is possible to store the Cache out of process as...
3
by: xzzy | last post by:
A. I need to access: System.Web.HttpContext.Current.Cache but get this error: "Object reference not set to an instance of the object" B. System.Web.HttpContext.Current.Cache is accessible...
13
by: Andrew Morton | last post by:
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...
3
by: Raj | last post by:
I created a refresh deferred MQT, and during full refresh there were 4 or 5 lock waits, all waiting on a 'S' lock on Internal Catalog Cache ? Can some one explain how to prevent this from happening?
4
by: msch-prv | last post by:
Hello. I am trying to use caching to populate a datalist. The select method of the associated objectdatasource calls up GetRecipePageByRecipeCatID() to request the proper data (I am using custom...
6
by: =?Utf-8?B?emlubw==?= | last post by:
in asp.net 2, I load a dataset from the Cache object into a local object to use to perform processing. I had been using httpcontext.current.cache and what I noticed is sometime I'm not able to get...
3
by: Madhur | last post by:
Hello I am delivering an asp.net 2.0 application to my customer. I need to know, If I need to check for the condition of HttpContext.Current to be null in my business logic. I have extensively...
8
by: SpaceMarine | last post by:
hello, my web app form has many DropDownLists that pull their content from a database. these calls are in a Business Access Layer, when first checks the context's Cache object for existing...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.