app vars and cache vars

Discussion in 'ASP .Net' started by Jon, Dec 14, 2004.

  1. Jon

    Jon Guest

    I have a couple of tables I want to load into a dataset and keep around
    pretty much forever, although they will need to be refreshed every so often.

    I can either put the dataset into an Application("myDataset") var or I can
    put it into the cache, Cache("myDataset"), both are universally available to
    my app, except in Global.asax, only Application() vars are available (or it
    seems that way on my machine, at least).

    I thought it would be better practice to use the cache, but I cannot do so
    if I want to load the data in Application_Start. If I want to do that, I
    need to use Application() vars.

    Does it really matter whether I use app or cache vars? I know there's no
    hard rules on when to use one and when to use the other...at least not that
    I've seen. Everyone has their own ideas on that.

    My questions:
    1. When do you recommend cache over app vars?
    2. Is there any performance difference between the two?
    3. If using cache vars when is the best time to populate them?

    Thank you!

    --
    ********************************
    Jon
     
    Jon, Dec 14, 2004
    #1
    1. Advertising

  2. Jon

    Karl Seguin Guest

    This might be a little extreme, but hopefully everyone would answer
    something along the same lines:
    Never use Application....it forces the item in memory even when there are
    memory constraints on the system. .Net is far better at managing its memory
    than you can ever dream to be. This isn't as true for simple object, but
    for a dataset, which could be huge, it's very important.

    Use the cache which lets .Net manage its memory. Note that iwth the cache
    you aren't sure that the data will be there (which is why you want to use
    it)...so you need to check if its in the cache and if not, get the data and
    re-store it.

    When is the best time to populate them? Lazily populate stuff as its
    needed. This sort of stuff should be abstracted away in a business layer
    anyways...

    public function GetAllSomething() as DataSet
    string cacheKey = "GetAllSOmething"
    DataSet ds = (DataSet) HttpRuntime.Cache[cacheKey];
    if (ds == null){
    ds = DataBase.GetMySomething()
    HttpRuntime.Cache.Insert(cacheKey, ds, null,
    DateTime.Now.AddHours(2), TimeSpan.Zero);
    }
    return ds
    end function

    that mix of c# and vb should be all you need. Whenever you need the
    dataset, simply call GetAllSomething()

    Karl

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


    "Jon" <> wrote in message
    news:...
    > I have a couple of tables I want to load into a dataset and keep around
    > pretty much forever, although they will need to be refreshed every so

    often.
    >
    > I can either put the dataset into an Application("myDataset") var or I can
    > put it into the cache, Cache("myDataset"), both are universally available

    to
    > my app, except in Global.asax, only Application() vars are available (or

    it
    > seems that way on my machine, at least).
    >
    > I thought it would be better practice to use the cache, but I cannot do so
    > if I want to load the data in Application_Start. If I want to do that, I
    > need to use Application() vars.
    >
    > Does it really matter whether I use app or cache vars? I know there's no
    > hard rules on when to use one and when to use the other...at least not

    that
    > I've seen. Everyone has their own ideas on that.
    >
    > My questions:
    > 1. When do you recommend cache over app vars?
    > 2. Is there any performance difference between the two?
    > 3. If using cache vars when is the best time to populate them?
    >
    > Thank you!
    >
    > --
    > ********************************
    > Jon
    >
    >
     
    Karl Seguin, Dec 14, 2004
    #2
    1. Advertising

  3. Actually, you can set the Cache timeout for individual items to never
    expire. If you do this, you don't have to worry about Cache items expiring
    or disappearing, unless the app recycles.

    --
    HTH,
    Kevin Spencer
    ..Net Developer
    Microsoft MVP
    Neither a follower
    nor a lender be.

    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > This might be a little extreme, but hopefully everyone would answer
    > something along the same lines:
    > Never use Application....it forces the item in memory even when there are
    > memory constraints on the system. .Net is far better at managing its

    memory
    > than you can ever dream to be. This isn't as true for simple object, but
    > for a dataset, which could be huge, it's very important.
    >
    > Use the cache which lets .Net manage its memory. Note that iwth the cache
    > you aren't sure that the data will be there (which is why you want to use
    > it)...so you need to check if its in the cache and if not, get the data

    and
    > re-store it.
    >
    > When is the best time to populate them? Lazily populate stuff as its
    > needed. This sort of stuff should be abstracted away in a business layer
    > anyways...
    >
    > public function GetAllSomething() as DataSet
    > string cacheKey = "GetAllSOmething"
    > DataSet ds = (DataSet) HttpRuntime.Cache[cacheKey];
    > if (ds == null){
    > ds = DataBase.GetMySomething()
    > HttpRuntime.Cache.Insert(cacheKey, ds, null,
    > DateTime.Now.AddHours(2), TimeSpan.Zero);
    > }
    > return ds
    > end function
    >
    > that mix of c# and vb should be all you need. Whenever you need the
    > dataset, simply call GetAllSomething()
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Jon" <> wrote in message
    > news:...
    > > I have a couple of tables I want to load into a dataset and keep around
    > > pretty much forever, although they will need to be refreshed every so

    > often.
    > >
    > > I can either put the dataset into an Application("myDataset") var or I

    can
    > > put it into the cache, Cache("myDataset"), both are universally

    available
    > to
    > > my app, except in Global.asax, only Application() vars are available (or

    > it
    > > seems that way on my machine, at least).
    > >
    > > I thought it would be better practice to use the cache, but I cannot do

    so
    > > if I want to load the data in Application_Start. If I want to do that,

    I
    > > need to use Application() vars.
    > >
    > > Does it really matter whether I use app or cache vars? I know there's

    no
    > > hard rules on when to use one and when to use the other...at least not

    > that
    > > I've seen. Everyone has their own ideas on that.
    > >
    > > My questions:
    > > 1. When do you recommend cache over app vars?
    > > 2. Is there any performance difference between the two?
    > > 3. If using cache vars when is the best time to populate them?
    > >
    > > Thank you!
    > >
    > > --
    > > ********************************
    > > Jon
    > >
    > >

    >
    >
     
    Kevin Spencer, Dec 14, 2004
    #3
  4. Jon

    Jon Guest

    Thanks, that's a good guideline in my opinion. I hadn't realized that using
    the app vars was different on memory, etc.


    "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
    wrote in message news:...
    > This might be a little extreme, but hopefully everyone would answer
    > something along the same lines:
    > Never use Application....it forces the item in memory even when there are
    > memory constraints on the system. .Net is far better at managing its
    > memory
    > than you can ever dream to be. This isn't as true for simple object, but
    > for a dataset, which could be huge, it's very important.
    >
    > Use the cache which lets .Net manage its memory. Note that iwth the cache
    > you aren't sure that the data will be there (which is why you want to use
    > it)...so you need to check if its in the cache and if not, get the data
    > and
    > re-store it.
    >
    > When is the best time to populate them? Lazily populate stuff as its
    > needed. This sort of stuff should be abstracted away in a business layer
    > anyways...
    >
    > public function GetAllSomething() as DataSet
    > string cacheKey = "GetAllSOmething"
    > DataSet ds = (DataSet) HttpRuntime.Cache[cacheKey];
    > if (ds == null){
    > ds = DataBase.GetMySomething()
    > HttpRuntime.Cache.Insert(cacheKey, ds, null,
    > DateTime.Now.AddHours(2), TimeSpan.Zero);
    > }
    > return ds
    > end function
    >
    > that mix of c# and vb should be all you need. Whenever you need the
    > dataset, simply call GetAllSomething()
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Jon" <> wrote in message
    > news:...
    >> I have a couple of tables I want to load into a dataset and keep around
    >> pretty much forever, although they will need to be refreshed every so

    > often.
    >>
    >> I can either put the dataset into an Application("myDataset") var or I
    >> can
    >> put it into the cache, Cache("myDataset"), both are universally available

    > to
    >> my app, except in Global.asax, only Application() vars are available (or

    > it
    >> seems that way on my machine, at least).
    >>
    >> I thought it would be better practice to use the cache, but I cannot do
    >> so
    >> if I want to load the data in Application_Start. If I want to do that, I
    >> need to use Application() vars.
    >>
    >> Does it really matter whether I use app or cache vars? I know there's no
    >> hard rules on when to use one and when to use the other...at least not

    > that
    >> I've seen. Everyone has their own ideas on that.
    >>
    >> My questions:
    >> 1. When do you recommend cache over app vars?
    >> 2. Is there any performance difference between the two?
    >> 3. If using cache vars when is the best time to populate them?
    >>
    >> Thank you!
    >>
    >> --
    >> ********************************
    >> Jon
    >>
    >>

    >
    >
     
    Jon, Dec 14, 2004
    #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:
    610
    Jeff Nokes
    Sep 30, 2003
  2. John
    Replies:
    2
    Views:
    1,217
  3. Linuxguy123
    Replies:
    7
    Views:
    734
    Paddy O'Loughlin
    Feb 20, 2009
  4. caccolangrifata
    Replies:
    18
    Views:
    430
    Chris Torek
    Jul 22, 2011
  5. kevin
    Replies:
    1
    Views:
    394
    Kairi Zikpin
    Jul 21, 2006
Loading...

Share This Page