Re: how to test cache existence
- From: "Alvin Bruney [ASP.NET MVP]" <www.lulu.com/owc>
- Date: Wed, 23 Jan 2008 20:43:00 -0500
Good approach but I believe there is still an issue because it is a shared function - static in C# - that is accessible by all threads. Since the cache is also global and available to all threads/requests within the application, the code needs to reflect this by protecting insertions and retrievals. Essentially, I would either handle the null exception and do nothing - stick your head in the sand approach. Or lock the resource and incur a performance penalty but eliminate the null reference. That will/should protect you from concurrent access and cache scavenging that can occur under heavy load while you are accessing the cache. There's also another approach which should be considered. Downgrade the cache to session so that global access is no longer part of the design. Instead, on a per user basis, you can retrieve the cached items. Of course, that's entirely dependent on what your application is doing.
--
Regards,
Alvin Bruney [MVP ASP.NET]
[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------
""Walter Wang [MSFT]"" <wawang@xxxxxxxxxxxxxxxxxxxx> wrote in message news:u90KPxZXIHA.7336@xxxxxxxxxxxxxxxxxxxxxxxxx
Hi zino,
It's possible your first call to HttpRuntime.Cache(itemKey) in the if
statement of GetCachedItem returns valid cache entry but the cache entry
might be removed when executing the second call to the Cache(itemKey) in
GetAllClients(); especially when the server is on high load.
I'm not sure why your GetCachedItem returns the entire Cache object instead
of an entry (object) saved in the Cache. If it were me, I probably will
write the GetCachedItem as:
Public shared function GetCachedItem(itemKey as string, funct as
MyDelegate) as object
dim obj as Object = HttpRuntime.Cache(itemKey)
if obj is Nothing then
obj = funct.Invoke()
HttpRuntime.Cache.Insert(itemKey, obj, Nothing)
End If
return obj
End Function
Now in GetAllClients(), you simply cast GetCachedItem("clientCode",
myDelegate) to a DataSet because you know either it succeeds or it raises
exception if the delegate fails to return a valid DataSet (inserting a
cache entry with null value will raise exception anyway).
Regards,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
.
- References:
- how to test cache existence
- From: zino
- RE: how to test cache existence
- From: "Walter Wang [MSFT]"
- how to test cache existence
- Prev by Date: Re: GridView
- Next by Date: Reg expression for 0400 000 000 thanks
- Previous by thread: RE: how to test cache existence
- Next by thread: HtmlTextWriter
- Index(es):
Relevant Pages
|