Jan 05, 2009 02:45 AM|vnebook|LINK
hi, I write some very simple code to store an interger into cache, laterly when I access the cache I get a mistic value instead.
Please help me to check the following code:
//First I create a simple class
public class Test
public void TestValue()
HttpContext cx = HttpContext.Current;
int hits = 1;
if (cx.Cache["test"] != null)
hits = (int)cx.Cache["test"];
hits += 1;
cx.Cache["test"] = hits;
cx.Cache.Add("test", hits, null, System.DateTime.Now.AddSeconds(60), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
Lately I call TestValue from inside Application_BeginRequest() of global.asax.cs:
protected void Application_BeginRequest(Object sender, EventArgs e)
Test t = new Test();
The first time I access a page in my application it write the correct number: 1
Then I hit refresh, I get 16 instead of the expected number of 2.
I hit refresh again then get 31
Each time I hit refresh I get the old value add with 15. When I replace "cx.Cache["test"] = hits;" with "cx.Cache["test"]=2" it write the correct number of 2 but ofcourse that is for testing only, not what I want. I even suspected
that the begin request has been called more than 1 time but I 'm wrong because the number always been written 1 time only. Can anyone help? thank you.