Re: how to test cache existence



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.


.



Relevant Pages

  • SoftReference operation synchronization detail
    ... Suppose I initialize a class member field which is a SoftReference, ... SoftReference cache = new SoftReference; ... Object obj = null; ...
    (comp.lang.java.programmer)
  • Re: SoftReference operation synchronization detail
    ... Suppose I initialize a class member field which is a SoftReference, ... SoftReference cache = new SoftReference; ... Object obj = null; ...
    (comp.lang.java.programmer)
  • Re: SoftReference operation synchronization detail
    ... Suppose I initialize a class member field which is a SoftReference, ... SoftReference cache = new SoftReference; ... Object obj = null; ...
    (comp.lang.java.programmer)
  • Idea for improving linux buffer cache behaviour
    ... cache gets loaded with all this data I don't care about (the contents of the ... Also keep a count of how old the cache entry is. ... the cache entry is the current value of the counter minus the cache entry's ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • RE: ObjectDataSource caching questions
    ... The objectDataSource control's cache is maintained internally so that we ... the query from the underlying data access class. ... So we need to add such a cache entry so that the ObjectDataSource ...
    (microsoft.public.dotnet.framework.aspnet)