Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / July 2004

Tip: Looking for answers? Try searching our database.

The right way to cache

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shell D00d - 23 Jun 2004 13:28 GMT
Hi,

I have a user control that generates a menu. The menu changes
depending on the type of user that logs on. The user's type is stored
in a cookie which I set from my login page.

Now, on all my pages (except the login), I have put in a user control
that I created which generates a menu for the user depending on the
user's type in the cookie. I would like to cache this control for the
current session, i.e. until the user hits the logout button or the
session times-out.

From what I've read, page fragment caching is what I'm supposed to do,
but how do I tell the framework that the control should be cached only
for the current session? I've put in the @OutputCache directive in the
control, but this caches the control for all users. i.e., regardless
of who logs in, they get the same menu because the first copy that
goes into the cache keeps showing up.

Thanks.
Raymond Lewallen - 23 Jun 2004 15:44 GMT
Shell,

The cache object is application specific, not session specific, therefore,
can not be applied to a specific session.  What you can do is look at the
VaryByParam property of the cache and perhaps use that to your advantage,
caching a different version of the page based on the user type in the
cookie, although you would have to pass this user type paramter value into
the page via query string in the http context via post method.

Look up HTTPCachePolicy in BOL.

HTH,

Raymond Lewallen

> Hi,
>
[quoted text clipped - 16 lines]
>
> Thanks.
Shell D00d - 24 Jun 2004 06:11 GMT
Hi Raymond,

Thanks for your suggestion. I'll try that right away. But now that you
mention that the cache is application specific, I have a feeling I'm
going to run into another problem...

You see, the administrator can change the user's type at any time.
When that happens, the user should see the new menu (the next time the
user logs on). But if the object were cached for the whole
application, then this cache would never get updated, unless I choose
to time out, say, every hour.

Is there any way I can clear the cache at the end of every session?

Thanks.

> Shell,
>
[quoted text clipped - 10 lines]
>
> Raymond Lewallen
Raymond Lewallen - 24 Jun 2004 13:01 GMT
So how are you going to get that new user type when an admin changes it on
the fly?  Do you have user control on every page that is going out and
checking the user type in the database associated with that user and
comparing to the user type that have stored in the session variable?  Or
will the user have to log back in to get his new user type and see the new
menu?  I guess it doesn't matter.  The thing is, using the VaryByParam
property of the cache object, when it sees a different user type, its going
to pull the cached menu associated with that user type parameter.

As far as clearing the cache at the end of a session, well, you can always
do a Cache.Remove("MyMenuItem") in the Session_End method of the
global.asax, but you are defeating your purpose of caching the object in
Cache by doing that.  Go ahead and store the menuitem in a Session in that
case, and when you pull it, same as if you were storing it in cache object,
you will have to type cast it back to your menu item type, as in MyMenuItem
= CType(Session("ThisUsersMenu"), UserMenu).  Then when that users session
is destroyed, so will be that menu stored for their session.  The drawback
here is that if you have 10 users logged in under the same user type, they
all won't be using the same cached menu object, but each will have his own
copy of the same menu.

> Hi Raymond,
>
[quoted text clipped - 26 lines]
> >
> > Raymond Lewallen
Shell D00d - 25 Jun 2004 05:21 GMT
> So how are you going to get that new user type when an admin changes it on
> the fly?  

It will be reflected only after the user logs out and back in.

> menu?  I guess it doesn't matter.  The thing is, using the VaryByParam
> property of the cache object, when it sees a different user type, its going
> to pull the cached menu associated with that user type parameter.

Hmmm... well, it gets slightly more complicated. You see, the admin
can also change the rights that a user has ==> the menu should now
show different options. So even when the type remains the same, the
menu can be different. Once changed, though, the menu will be the same
for all users of that type.

> As far as clearing the cache at the end of a session, well, you can always
> do a Cache.Remove("MyMenuItem") in the Session_End method of the
> global.asax, but you are defeating your purpose of caching the object in
> Cache by doing that.  

I thought about that, but wouldn't this require me to manually put it
in the cache? i.e., with Cache.Insert()? Suppose I'm relying on the
@OutputCache directive... how would I know the key that is used to
cache the control?

Now... let's say I do decide to cache the control manually with
Cache.Insert(). How do I place this control on my form? I'm thinking
that I'll use a place holder and then do

myMenu = Session("myControl") as MyMenuControl;
myPlaceHolder.Controls.Add(myMenu);

> is destroyed, so will be that menu stored for their session.  The drawback
> here is that if you have 10 users logged in under the same user type, they
> all won't be using the same cached menu object, but each will have his own
> copy of the same menu.

You're right... I will have to store a cache for each session. Is
there a more elegant solution to the situation? Perhaps I can use an
Application level cache that is updated everytime a new session is
created. This way, all users share one copy of the menu, and it gets
created and updated only when a new user logs on. What do you think?

Thanks.
Mr. Dot Net - 07 Jul 2004 16:12 GMT
Hi,

The problem is that page fragment cache cannot be stored in the client which
is the obvious place for your need (different menu for different users).
This will not only save place on the server and will be a better scalable
solution but also will save transportation via the wires..

What I recommend is that you will create the menu control as an aspx file,
then put the aspx file as a source for IFRAME that will be put on the
desired location for the menu.
If you need help in communicating between them, write me in this group.

> Hi,
>
[quoted text clipped - 16 lines]
>
> Thanks.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.