Cache items missing

Discussion in 'ASP .Net' started by MattC, Feb 11, 2005.

  1. MattC

    MattC Guest

    Hi,

    I am persisting the viewstate for each page into the Cache object, below is
    shown my methods for saving and loading:

    I am able to save the viewstate to the cache and most times I can load it
    ok, however it seems that every now and again it fails to Deserialize the
    viewstate.

    My Application pool is set to shutdown after 180mins of idle.

    IIS6.0 W2K3 SVR


    TIA

    MattC

    private string ViewStateCacheKey
    {
    get{ return "VIEWSTATE_" + Request.UserHostAddress; }
    }

    protected override void SavePageStateToPersistenceMedium(object viewState)
    {
    LosFormatter oLosFormatter = new LosFormatter();
    StringWriter oStringWriter = new StringWriter();
    oLosFormatter.Serialize(oStringWriter, viewState);
    string str = this.ViewStateCacheKey + "_" + Guid.NewGuid().ToString();

    try
    {
    Cache.Insert(str, //key
    oStringWriter.ToString(), //value
    null, //dependency
    Cache.NoAbsoluteExpiration, //no absolute expiration
    new TimeSpan(0,0,Session.Timeout + 10,0,0), //sliding expiration
    Seesion timeout
    CacheItemPriority.High,
    onRemove); //call back on removal
    }
    catch(Exception e)
    {
    throw new Exception("Failed to store viewstate in Cache", e);
    }

    }

    RegisterHiddenField("__VIEWSTATE_KEY", str);
    RegisterHiddenField("__VIEWSTATE", String.Empty);

    }

    protected override object LoadPageStateFromPersistenceMedium()
    {

    object viewstate = null;//return viewstate
    LosFormatter oLosFormatter = new LosFormatter();
    string str = Request.Form["__VIEWSTATE_KEY"];

    try
    {
    viewstate = oLosFormatter.Deserialize(Cache[str].ToString());//cache
    }
    catch(Exception e)
    {
    Events.WriteToLog("Failed to deserialize ViewState '" + str +"'
    from cache: " + e.Message);//system event log
    }

    return viewstate;

    }
     
    MattC, Feb 11, 2005
    #1
    1. Advertising

  2. At the 180 minute mark, the pool is recycled and the app domain is unloaded.
    Variables and objects belonging to that application domain are lost. Your
    viewstate may still be there, but the cache values are gone.

    --
    Regards,
    Alvin Bruney [MVP ASP.NET]

    [Shameless Author plug]
    The Microsoft Office Web Components Black Book with .NET
    Now Available @ http://www.lulu.com/owc
    ----------------------------------------------------------


    "MattC" <> wrote in message
    news:%...
    > Hi,
    >
    > I am persisting the viewstate for each page into the Cache object, below
    > is shown my methods for saving and loading:
    >
    > I am able to save the viewstate to the cache and most times I can load it
    > ok, however it seems that every now and again it fails to Deserialize the
    > viewstate.
    >
    > My Application pool is set to shutdown after 180mins of idle.
    >
    > IIS6.0 W2K3 SVR
    >
    >
    > TIA
    >
    > MattC
    >
    > private string ViewStateCacheKey
    > {
    > get{ return "VIEWSTATE_" + Request.UserHostAddress; }
    > }
    >
    > protected override void SavePageStateToPersistenceMedium(object viewState)
    > {
    > LosFormatter oLosFormatter = new LosFormatter();
    > StringWriter oStringWriter = new StringWriter();
    > oLosFormatter.Serialize(oStringWriter, viewState);
    > string str = this.ViewStateCacheKey + "_" + Guid.NewGuid().ToString();
    >
    > try
    > {
    > Cache.Insert(str, //key
    > oStringWriter.ToString(), //value
    > null, //dependency
    > Cache.NoAbsoluteExpiration, //no absolute expiration
    > new TimeSpan(0,0,Session.Timeout + 10,0,0), //sliding expiration
    > Seesion timeout
    > CacheItemPriority.High,
    > onRemove); //call back on removal
    > }
    > catch(Exception e)
    > {
    > throw new Exception("Failed to store viewstate in Cache", e);
    > }
    >
    > }
    >
    > RegisterHiddenField("__VIEWSTATE_KEY", str);
    > RegisterHiddenField("__VIEWSTATE", String.Empty);
    >
    > }
    >
    > protected override object LoadPageStateFromPersistenceMedium()
    > {
    >
    > object viewstate = null;//return viewstate
    > LosFormatter oLosFormatter = new LosFormatter();
    > string str = Request.Form["__VIEWSTATE_KEY"];
    >
    > try
    > {
    > viewstate = oLosFormatter.Deserialize(Cache[str].ToString());//cache
    > }
    > catch(Exception e)
    > {
    > Events.WriteToLog("Failed to deserialize ViewState '" + str +"'
    > from cache: " + e.Message);//system event log
    > }
    >
    > return viewstate;
    >
    > }
    >
     
    Alvin Bruney [MVP], Feb 11, 2005
    #2
    1. Advertising

  3. MattC

    Karl Seguin Guest

    Also, UserHostAddress isn't guaranteed to be unique per visitor...namely
    those who sit behind a proxy (increasingly popular). I would expect strange
    behaviour if two users from behind the same proxy visit your application.
    This is likely why sessions are prefered for server-side viewstate than
    cache.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:%...
    > At the 180 minute mark, the pool is recycled and the app domain is

    unloaded.
    > Variables and objects belonging to that application domain are lost. Your
    > viewstate may still be there, but the cache values are gone.
    >
    > --
    > Regards,
    > Alvin Bruney [MVP ASP.NET]
    >
    > [Shameless Author plug]
    > The Microsoft Office Web Components Black Book with .NET
    > Now Available @ http://www.lulu.com/owc
    > ----------------------------------------------------------
    >
    >
    > "MattC" <> wrote in message
    > news:%...
    > > Hi,
    > >
    > > I am persisting the viewstate for each page into the Cache object, below
    > > is shown my methods for saving and loading:
    > >
    > > I am able to save the viewstate to the cache and most times I can load

    it
    > > ok, however it seems that every now and again it fails to Deserialize

    the
    > > viewstate.
    > >
    > > My Application pool is set to shutdown after 180mins of idle.
    > >
    > > IIS6.0 W2K3 SVR
    > >
    > >
    > > TIA
    > >
    > > MattC
    > >
    > > private string ViewStateCacheKey
    > > {
    > > get{ return "VIEWSTATE_" + Request.UserHostAddress; }
    > > }
    > >
    > > protected override void SavePageStateToPersistenceMedium(object

    viewState)
    > > {
    > > LosFormatter oLosFormatter = new LosFormatter();
    > > StringWriter oStringWriter = new StringWriter();
    > > oLosFormatter.Serialize(oStringWriter, viewState);
    > > string str = this.ViewStateCacheKey + "_" + Guid.NewGuid().ToString();
    > >
    > > try
    > > {
    > > Cache.Insert(str, //key
    > > oStringWriter.ToString(), //value
    > > null, //dependency
    > > Cache.NoAbsoluteExpiration, //no absolute expiration
    > > new TimeSpan(0,0,Session.Timeout + 10,0,0), //sliding expiration
    > > Seesion timeout
    > > CacheItemPriority.High,
    > > onRemove); //call back on removal
    > > }
    > > catch(Exception e)
    > > {
    > > throw new Exception("Failed to store viewstate in Cache", e);
    > > }
    > >
    > > }
    > >
    > > RegisterHiddenField("__VIEWSTATE_KEY", str);
    > > RegisterHiddenField("__VIEWSTATE", String.Empty);
    > >
    > > }
    > >
    > > protected override object LoadPageStateFromPersistenceMedium()
    > > {
    > >
    > > object viewstate = null;//return viewstate
    > > LosFormatter oLosFormatter = new LosFormatter();
    > > string str = Request.Form["__VIEWSTATE_KEY"];
    > >
    > > try
    > > {
    > > viewstate =

    oLosFormatter.Deserialize(Cache[str].ToString());//cache
    > > }
    > > catch(Exception e)
    > > {
    > > Events.WriteToLog("Failed to deserialize ViewState '" + str

    +"'
    > > from cache: " + e.Message);//system event log
    > > }
    > >
    > > return viewstate;
    > >
    > > }
    > >

    >
    >
     
    Karl Seguin, Feb 11, 2005
    #3
  4. MattC

    MattC Guest

    Guys,

    The viewstate not being found is happening at times well within the 180min
    mark. The proxy point is well taken and may be something to consider,
    however, this is an Intranet app and so does not suffer with this.

    I think I have found my answer. It lies in the priority I gave the cache
    items.
    CacheItemPriority.High

    I think as the worker process grew with all the postbacks storing new
    viewstate ASP.NET removed some entries to free resources.

    As a test I have set this to NotRemovable and am relying on the expiration
    to remove it.

    MattC


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > Also, UserHostAddress isn't guaranteed to be unique per visitor...namely
    > those who sit behind a proxy (increasingly popular). I would expect
    > strange
    > behaviour if two users from behind the same proxy visit your application.
    > This is likely why sessions are prefered for server-side viewstate than
    > cache.
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    > news:%...
    >> At the 180 minute mark, the pool is recycled and the app domain is

    > unloaded.
    >> Variables and objects belonging to that application domain are lost. Your
    >> viewstate may still be there, but the cache values are gone.
    >>
    >> --
    >> Regards,
    >> Alvin Bruney [MVP ASP.NET]
    >>
    >> [Shameless Author plug]
    >> The Microsoft Office Web Components Black Book with .NET
    >> Now Available @ http://www.lulu.com/owc
    >> ----------------------------------------------------------
    >>
    >>
    >> "MattC" <> wrote in message
    >> news:%...
    >> > Hi,
    >> >
    >> > I am persisting the viewstate for each page into the Cache object,
    >> > below
    >> > is shown my methods for saving and loading:
    >> >
    >> > I am able to save the viewstate to the cache and most times I can load

    > it
    >> > ok, however it seems that every now and again it fails to Deserialize

    > the
    >> > viewstate.
    >> >
    >> > My Application pool is set to shutdown after 180mins of idle.
    >> >
    >> > IIS6.0 W2K3 SVR
    >> >
    >> >
    >> > TIA
    >> >
    >> > MattC
    >> >
    >> > private string ViewStateCacheKey
    >> > {
    >> > get{ return "VIEWSTATE_" + Request.UserHostAddress; }
    >> > }
    >> >
    >> > protected override void SavePageStateToPersistenceMedium(object

    > viewState)
    >> > {
    >> > LosFormatter oLosFormatter = new LosFormatter();
    >> > StringWriter oStringWriter = new StringWriter();
    >> > oLosFormatter.Serialize(oStringWriter, viewState);
    >> > string str = this.ViewStateCacheKey + "_" + Guid.NewGuid().ToString();
    >> >
    >> > try
    >> > {
    >> > Cache.Insert(str, //key
    >> > oStringWriter.ToString(), //value
    >> > null, //dependency
    >> > Cache.NoAbsoluteExpiration, //no absolute expiration
    >> > new TimeSpan(0,0,Session.Timeout + 10,0,0), //sliding expiration
    >> > Seesion timeout
    >> > CacheItemPriority.High,
    >> > onRemove); //call back on removal
    >> > }
    >> > catch(Exception e)
    >> > {
    >> > throw new Exception("Failed to store viewstate in Cache", e);
    >> > }
    >> >
    >> > }
    >> >
    >> > RegisterHiddenField("__VIEWSTATE_KEY", str);
    >> > RegisterHiddenField("__VIEWSTATE", String.Empty);
    >> >
    >> > }
    >> >
    >> > protected override object LoadPageStateFromPersistenceMedium()
    >> > {
    >> >
    >> > object viewstate = null;//return viewstate
    >> > LosFormatter oLosFormatter = new LosFormatter();
    >> > string str = Request.Form["__VIEWSTATE_KEY"];
    >> >
    >> > try
    >> > {
    >> > viewstate =

    > oLosFormatter.Deserialize(Cache[str].ToString());//cache
    >> > }
    >> > catch(Exception e)
    >> > {
    >> > Events.WriteToLog("Failed to deserialize ViewState '" + str

    > +"'
    >> > from cache: " + e.Message);//system event log
    >> > }
    >> >
    >> > return viewstate;
    >> >
    >> > }
    >> >

    >>
    >>

    >
    >
     
    MattC, Feb 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Nokes

    Cache::Cache Stale Segments

    Jeff Nokes, Sep 30, 2003, in forum: Perl
    Replies:
    0
    Views:
    582
    Jeff Nokes
    Sep 30, 2003
  2. DesignerX

    Page.Cache vs HttpContext.Current.Cache

    DesignerX, Jan 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    8,268
    vMike
    Jan 20, 2004
  3. =?Utf-8?B?b25l?=
    Replies:
    1
    Views:
    5,300
    Karl Seguin [MVP]
    Mar 8, 2006
  4. Sergey via DotNetMonster.com

    ASP.NET Cache vs Window System Cache

    Sergey via DotNetMonster.com, Nov 15, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    481
    Sergey via DotNetMonster.com
    Nov 15, 2006
  5. John
    Replies:
    2
    Views:
    1,185
Loading...

Share This Page