app vars and cache vars

J

Jon

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!
 
K

Karl Seguin

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
 
K

Kevin Spencer

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 said:
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 said:
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!
 
J

Jon

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 said:
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 said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top