Caching across pages and some other stuff

Hi,

I was wondering how I access cached objects across multiple pages.  Does Page.Cache.Insert only refer to the current page and not across other pages?  If so, how can I make this possible?  Also, what is the difference between:

Page.Cache.Insert("contractTypeCol", contractTypeCol);
and
HttpContext.Current.Cache["contractTypeCol"] = contractTypeCol

Seems like they are both doing the same thing.
I guess I am not really sure what HttpContext and Current means and MSDN is not much help.

thanks



LVL 7
brdrokAsked:
Who is Participating?
 
RejojohnyConnect With a Mentor Commented:
>>HttpContext.Curent is only a matter of prefrence?  
its not just a matter of preference .. but also depends on how and where u r accessing it .. do not ask me y, but i had problems accessing cache stored using web pages in web services .. unless i used httpcontext.current ... anyway as for the the dependency of a cache object, i not think it is possible to associate it to a class .. it has to be a physical object .. or another object in the cache ..
this is what MSDN says

CacheDependency Class
Tracks cache dependencies, which can be files, directories, or keys to other objects in your application's Cache.

so u could add the object "colContractTypes" to cache and have this as a dependency to another cache ..
>>and someone/somewhere/somehow added a new object, would that mean my collection object would be evicted from my cache?
this object should always update the cache .. then ur other cache will also change .. but then in that case y would u require 2 cahche object .. right? unless u plan to have 2 different views of the same object ...
0
 
RejojohnyCommented:
both r the same .. both references the sytem.web.caching.cache object .. whatever u store in the cache is accessible across pages .. it is just like a application object .. accessible by all users .. it has some additional parameters like the time when it will expire, priority etc which helps in managing these objects more efficiently than application objects ...
0
 
brdrokAuthor Commented:
hmmm....so the page.cache vs. HttpContext.Curent is only a matter of prefrence?  

You know how one can do dependencies (i.e. on xml files) and depending on whether the xml file changes, it evicts the current object inside that cache?  what I was wondering is whether somehting like this would apply to strongly typed collections.  so, if i had an cached object based on a a collection object i called "colContractTypes", and someone/somewhere/somehow added a new object, would that mean my collection object would be evicted from my cache?

thank you....
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
brdrokAuthor Commented:
clear as mud =)

thanks very much for your insight....and i truly wish i could assign more points to this question.  hmm...having a cache object act as a dependency for another cache object seems a little too funky for me given my skill level.

what precipated all this is the following scenario:  I have two drop down list boxes and the second drop down listbox values are dependant on the selected value of the first.

using my favorite example =) I have various departments and each department would have employees associated with one single department.

since the department collection doesn't change very often (if at all), i thought this would be a great candidate for caching.  depending on the department selected, the second drop down list box will list all the employees for that employee.  do you think I should create a cache object that will hold the employees for each department (i.e.

Page.Cache.Insert("MusicDeptEmployeesCollection", colMusicDeptEmp);
Page.Cache.Insert("EnglishDeptEmployeesCollection", colEngDeptEmp);
Page.Cache.Insert("PhysicsDeptEmployeeCollection", colPhysicsDeptEmp);

hope this made sense....
0
 
QingtongConnect With a Mentor Commented:
Cache is good thing to improve the performance of the web site.

Typically what I did for cache is:

Public Function LoadSettings() as string

    Dim strCacheKey As String = TSG_DataGridController.TSG_SETTINGS & "_" & ModuleId

    If Cache.Item(strCacheKey) Is Nothing Then
         Dim strSetting As String = String.Empty
         strSetting = 'get setting string somewhere or any datatype such as datatable, dataset or ....
         Cache.Insert(strCacheKey, strSetting, Nothing, DateTime.Now.AddSeconds(10), TimeSpan.Zero)
    End If

    return Ctype(Cache.Item(strCacheKey), string)
End Function

This cache will be expired in 10 seconds.

Acturally you may cache any data type. And if you have any dependency put it in Cache.Insert instead of Nothing.
Right now the dependency is only support file change, as I know. So if your data source is based on XML file, then put the file name here.
It cannot take any database change for SQL2000. It is said SQL2005 has the function for cache dependency

Hope it may help.

Qingtong
0
 
RejojohnyCommented:
thx for the points

>>since the department collection doesn't change very often (if at all), i thought this would be a great candidate for caching.  depending on the department selected, the second drop down list box will list all the employees for that employee.  do you think I should create a cache object that will hold the employees for each department

Actually it all depends on the size of the data you r caching ... and also how frequently u use the data and how frequently it changes ... lets say u have a screen where u want to list all the employess and another screen (as u mentioned) where u list employees based on dept, then ur original idea of caching different cache for each type of employee would not be of any advantage for the first screen. ... in these type of scenarion .. u should hold dept in one cache and another cache having list of employees with the dept ids .. for the first screen u can directly get the list from the cache and for the second screen u can filter out the employees for the selected dept .... hope u got what i am trying to explain ..

in another scenario, u have a single screen which is rarely used by the user which list the employees, then it does not really make sense in caching the employee data .... u have to decide on what and how long to cache based on the frequency of usage ....
if the employee data is not
0
 
brdrokAuthor Commented:
i'm gonna have to "grok" this very carefully but I do believe I got the main idea out of it.  ever got the feeling that programming is more art than science <grin>

i really do apprciate you guys taking time out explaning stuff to me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.