开发者

How do you handle browser cache with login/logout?

To improve performances, I'd like to add a fairly long Cache-Control (up t开发者_如何学JAVAo 30 minutes) to each page since they do not change often. However, each page also displays the name of the user logged in (like this website).

The problem is when the user logs in or logs out: the user name must change. How can I change the user name after each login/logout action while keeping a long Cache-Control?

Here are the solutions I can think of:

  • Ajax request (not cached) to retrieve and display the user name. If I have 2 requests (/user?registered and /user?new), they could be cached as well. But I am afraid this extra request would nullify my caching performance-wise
  • Add a unique URL variable (?time=) to make the URL different, and cancel the cache. However, I would have to add this variable to all links on my webpage, not very convenient code-wise

This problems becomes greater if I actually have more content that is not the same for registered users and new users.


Cache-Control: private

Is usually enough in practice. It's what SO uses.

In theory, if you needed to allow for the case of variable logins from the same client you should probably set Vary on Cookie (assuming that's the mechanism you're using for login). However, this value of Vary (along with most others) messes up IE's caching completely so it's generally avoided. Also, it's often desirable to allow the user to step through the back/forward list including logged-in/out pages without having to re-fetch.

For situations where enforcing proper logged-in-ness for every page is critical (such as banking), an full Cache-Control: no-cache is typically used instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜