Rails caching with cookies

Discussion in 'Ruby/Rails' started by baxtr, Apr 24, 2007.

  1. baxtr

    baxtr New Member

    I'm trying to implement cookie based conditional page caching for a rails project. Very similar to this site:
    http://www.ethelred.org/articles/2006/10/22/conditional-use-of-page-cache-in-rails

    Basically non-logged in users get a cached site and logged in users get dynamic content. I have 90% of it working but there seems to be a small problem with the HTTP_COOKIE condition in the rewrite. When a user logs out they still see cached content unless they click the refresh button manually within there browser for each page, or wait about 30 seconds. Any ideas? Here is a copy of my Litespeed vhost rewrite rule.

    RewriteCond %{HTTP_COOKIE} !^.*logged_in=yes.*$
    RewriteRule ^(.*)/$ $1/index.html [QSA]
    RewriteCond %{HTTP_COOKIE} !^.*logged_in=yes.*$
    RewriteRule ^([^.]+)$ $1.html [QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /dispatch.lsapi [QSA,L]

    Thanks,
    Dan
  2. mistwang

    mistwang LiteSpeed Staff

    LSWS does not cache cookie values, it just use what received in the request.
    You can check the cookie value with firefox LiveHeader extension.
    Make sure to clear the cookie value when a user logout.
  3. mistwang

    mistwang LiteSpeed Staff

    I probably misunderstood your problem.
    I think you need to add "no-cache" header in the dynamic response to prevent the browser from caching pages.
  4. baxtr

    baxtr New Member

    That was also my first idea. I disabled cache completely within my browser and tried a no-cache header, also tried a few different browsers. I also double checked that the cookie is removed upon logout. Does everything look fine in my RewriteCond? I've been trying to figure out this little problem all day, thanks for the quick response.
  5. mistwang

    mistwang LiteSpeed Staff

    There is no problem with the rewrite rules.
    Two possibilities, one is that browser cache the page, you can verify it with LiveHeader extension, if browser uses a cached page, there should not be any new request sent. You can verify that at server end with access log as well.

    Another possibilities, is browser cache the cookie value, you should be able to verify it with LiveHeader.
    You can also turn on rewrite log at server side to verify the rewrite rules.
  6. baxtr

    baxtr New Member

    Well I played around with LiveHeaders and you were rite the page is being cached by the browser. I tried adding the no-cache headers to my default rails layout page and it didn't seem to fix the problem. When I notice the problem the page does not refresh and the the browser doesn't even request a page from the server according to liveheaders. Is there anyway to set no-cache in litespeed rather then in my rails app?
  7. mistwang

    mistwang LiteSpeed Staff

    You need to try a bit harder to stop browser caching pages. consider the following headers. ;)

    Code:
    [FONT=Verdana,Arial,Helvetica][SIZE=2]Pragma:no-cache
    Cache-Control: no-store, no-cache, private, max-stale=0
    Expires: 0
    [/SIZE][/FONT]
    Good luck! :)
  8. baxtr

    baxtr New Member

    Ok the headers seems to work fine in Firefox. However, I was testing with Safari and I just read in Google that Safari ignores caches headers within HTML. Can I try adding the headers to the extra headers field in the Rails context section of Litespeed?
  9. baxtr

    baxtr New Member

    I'm not sure this is a bug, but if you put more then one line the Extra Headers field in the Rail context menu, Litespeed fails to restart or startup. One line seems to work fine.
  10. mistwang

    mistwang LiteSpeed Staff

    OK, I will check.
  11. baxtr

    baxtr New Member

  12. mistwang

    mistwang LiteSpeed Staff

  13. baxtr

    baxtr New Member

    Wow, that was fast thanks for the amazing support. I'm in the trial period for the 64-bit linux ent version. We plan on purchasing the ent. edition when were ready to launch the site. Thanks again.
  14. mistwang

    mistwang LiteSpeed Staff

Share This Page