I have a 2.0 app. that grabs a user's ID, builds an object then caches it using the user's ID as the key. Each page tries to retrieve the object at cache(UserID). If it is successful, it proceeds to load the page, if not, then they are redirected back
to the start of the process since the nature of the app won't really allow for the object to be re-created on the fly when the cached object is nothing. My problem is that periodically, even though the expiration is set to 24 hours from the time of entry,
the cache returns nothing. This wouldn't be a big deal, except when I start the process over, the first time I try to retrieve the object from the cache, it is empty again. In fact, it is constantly empty until I recycle the application pool that this app
is running in. I really have no idea what would be causing this behavior. At first I thought it had to do with they way I was caching the objects, but I'm starting to think that it may be an issue with IIS or a property of the app pool.
Any an all help is appreciated. This project is scheduled to move into a production environment in a few weeks!
Well, the cache is not an infinite repository, but I'm sure you already know that. The cache can only hold objects as long as the app is running and while the server has memory. objects will be dropped once memory is scarced starting with those that are
oldest and set to low persisting times. Can you send your code of where and how you are adding the object to the cache?
The load event of nearly every page contain similar code:
Protected Sub Page_Load(ByVal sender
As Object,
ByVal e As System.EventArgs)
Handles Me.Load
UID = User.Identity.Name
RetrieveECRN()
...
End
Sub
Private Sub RetrieveECRN()
If Cache.Get(UID)
Is Nothing
Then
Response.Redirect("../default.aspx?CE=T")
Else
CurrentECRN = Cache.Get(UID)
End If
End Sub
This is how I am inserting into the cache upon changes within the ECRN object:
Private Sub CacheECRN()
If Cache.Get(UID)
Is Nothing
Then
Cache.Add(UID, CurrentECRN, Nothing, DateTime.Now.AddDays(1), System.TimeSpan.Zero, Caching.CacheItemPriority.Default,
Nothing)
Else
Cache.Insert(UID, CurrentECRN)
End If
End Sub
Correct. I was thinking it might have something to do with the fact that I set the object to cache at the default priority. Will whatever algorithm dumps things from the cache hold on to the object if it's priority is set to high? I'm unfamiliar with
how items are stored in this fashon, so its hard to tell if its a priority, a memory, or a timing issue. Is there any way to know for sure?
This may or may not be related to your problem, but you should be careful when getting items out of cache. Items can disappear from it at any moment in time. It is possible for the item in cache to be removed after you just checked to see if it is in there.
In this code:
If Cache.Get(UID)
Is Nothing
Then
Response.Redirect("../default.aspx?CE=T")
Else
CurrentECRN = Cache.Get(UID)
End If
Its possible, although unlikely, for the IF to false into the Else case, but for the item in Cache to be removed by the time the first line in it executes. You'll end up with a null object when you didn't expect it to be. The solution is to just get the
item out of cache, then check that variable for null. Something like ...
CurrentECRN = Cache.Get(UID)
If CurrentECRN is Nothing Then ....
Thanks for the suggestion, I'll update my code. Do you have any idea why the cache is emptying in the first place? Would changing the priority increase the object's chance of persisting when the cache purges? Is there another method of storage that is
more stable?
Cache is really meant for just that -- caching. With the caching model, if something isnt available, you do whatever you need to do to retrieve it, otherwise you use the cached copy. That doesnt appear to be the pattern you need here because you don't want
the cache to ever be invalidated.
So instead you can use static members (but be careful to ensure thread safety and without sacraficing performance), or use the Application object, which is like Cache but doesn't offer the invalidation and timeout features.
What do you mean about "why the cache is emptying in the first place"? do you mean you can not read it from cache even immediately after you insert it into the cache?
Sort of, yes. I mean that the objects I cache persist for a while (only being dropped occasionally). Then it seems that after a certain period, every object is dropped immediately.
skeize
Member
94 Points
27 Posts
Losing cached objects very randomly
Sep 08, 2006 01:03 PM|LINK
I have a 2.0 app. that grabs a user's ID, builds an object then caches it using the user's ID as the key. Each page tries to retrieve the object at cache(UserID). If it is successful, it proceeds to load the page, if not, then they are redirected back to the start of the process since the nature of the app won't really allow for the object to be re-created on the fly when the cached object is nothing. My problem is that periodically, even though the expiration is set to 24 hours from the time of entry, the cache returns nothing. This wouldn't be a big deal, except when I start the process over, the first time I try to retrieve the object from the cache, it is empty again. In fact, it is constantly empty until I recycle the application pool that this app is running in. I really have no idea what would be causing this behavior. At first I thought it had to do with they way I was caching the objects, but I'm starting to think that it may be an issue with IIS or a property of the app pool.
Any an all help is appreciated. This project is scheduled to move into a production environment in a few weeks!
Thanks,
skeize
hooligannes9...
All-Star
16504 Points
2917 Posts
Re: Losing cached objects very randomly
Sep 08, 2006 02:02 PM|LINK
Well, the cache is not an infinite repository, but I'm sure you already know that. The cache can only hold objects as long as the app is running and while the server has memory. objects will be dropped once memory is scarced starting with those that are oldest and set to low persisting times. Can you send your code of where and how you are adding the object to the cache?
skeize
Member
94 Points
27 Posts
Re: Losing cached objects very randomly
Sep 08, 2006 02:10 PM|LINK
The load event of nearly every page contain similar code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
UID = User.Identity.Name
RetrieveECRN()
...
End Sub
Private Sub RetrieveECRN()
If Cache.Get(UID) Is Nothing Then
Response.Redirect("../default.aspx?CE=T")
Else
CurrentECRN = Cache.Get(UID)
End If
End Sub
This is how I am inserting into the cache upon changes within the ECRN object:
Private Sub CacheECRN()
If Cache.Get(UID) Is Nothing Then
Cache.Add(UID, CurrentECRN, Nothing, DateTime.Now.AddDays(1), System.TimeSpan.Zero, Caching.CacheItemPriority.Default, Nothing)
Else
Cache.Insert(UID, CurrentECRN)
End If
End Sub
hooligannes9...
All-Star
16504 Points
2917 Posts
Re: Losing cached objects very randomly
Sep 08, 2006 03:16 PM|LINK
If I follow correctly,
1) Get the Username
2) Try to retrieve the ECRN from cache
3) If the object does not exist, redirect.
skeize
Member
94 Points
27 Posts
Re: Losing cached objects very randomly
Sep 08, 2006 03:37 PM|LINK
Correct. I was thinking it might have something to do with the fact that I set the object to cache at the default priority. Will whatever algorithm dumps things from the cache hold on to the object if it's priority is set to high? I'm unfamiliar with how items are stored in this fashon, so its hard to tell if its a priority, a memory, or a timing issue. Is there any way to know for sure?
skeize
InfinitiesLo...
Participant
1972 Points
322 Posts
Microsoft
Re: Losing cached objects very randomly
Sep 08, 2006 05:24 PM|LINK
This may or may not be related to your problem, but you should be careful when getting items out of cache. Items can disappear from it at any moment in time. It is possible for the item in cache to be removed after you just checked to see if it is in there. In this code:
If Cache.Get(UID) Is Nothing Then
Response.Redirect("../default.aspx?CE=T")
Else
CurrentECRN = Cache.Get(UID)
End If
Its possible, although unlikely, for the IF to false into the Else case, but for the item in Cache to be removed by the time the first line in it executes. You'll end up with a null object when you didn't expect it to be. The solution is to just get the item out of cache, then check that variable for null. Something like ...
CurrentECRN = Cache.Get(UID)
If CurrentECRN is Nothing Then ....
Infinities Loop: TRULY Understanding ViewState
.NET from a new perspective.
This posting is provided "AS IS".
skeize
Member
94 Points
27 Posts
Re: Losing cached objects very randomly
Sep 08, 2006 06:04 PM|LINK
Thanks for the suggestion, I'll update my code. Do you have any idea why the cache is emptying in the first place? Would changing the priority increase the object's chance of persisting when the cache purges? Is there another method of storage that is more stable?
skeize
InfinitiesLo...
Participant
1972 Points
322 Posts
Microsoft
Re: Losing cached objects very randomly
Sep 08, 2006 07:50 PM|LINK
Cache is really meant for just that -- caching. With the caching model, if something isnt available, you do whatever you need to do to retrieve it, otherwise you use the cached copy. That doesnt appear to be the pattern you need here because you don't want the cache to ever be invalidated.
So instead you can use static members (but be careful to ensure thread safety and without sacraficing performance), or use the Application object, which is like Cache but doesn't offer the invalidation and timeout features.
I hope that helps
Infinities Loop: TRULY Understanding ViewState
.NET from a new perspective.
This posting is provided "AS IS".
wyx2000
Contributor
3388 Points
873 Posts
Re: Losing cached objects very randomly
Sep 11, 2006 07:16 AM|LINK
skeize
Member
94 Points
27 Posts
Re: Losing cached objects very randomly
Sep 11, 2006 12:42 PM|LINK