Turn cache on and off with headers?

Discussion in 'General' started by madesimplemedia, Jul 13, 2018 at 6:17 PM.

  1. Hi
    I'm experimenting using LS cache with Concrete5 CMS (C5).
    I can get the current user in C5 and set the cache headers depending on whether they are logged in or not:

    Code:
    $u = new User();
    if(!$u->isLoggedIn()) {
       header('X-LiteSpeed-Cache-Control: public, max-age=3600');
       header('X-LiteSpeed-Cache-Control: private, max-age=3600');
       echo "Not logged in";
    }
    else {
       header('X-LiteSpeed-Cache-Control: no-cache');
       echo "Logged in";
    }
    
    This works, I can see in the console that the cache is enabled, however it is always a miss.
    I can confirm that LS cache will run and "hit" on the server because I have run it using htaccess rules without issue.
    What else do I need to do to get the cache working using my method above?
    Thanks
     
  2. Jon K

    Jon K Administrator Staff Member

    Inside of .htaccess do you have `CacheLookup on` set? Even when using PHP to send headers you will still need to at least set that so the web server knows to look up the cache.
    If you have any logged in users or set cookies for different views you will also need to set vary on cookie inside of the .htaccess.

    You cannot have both public and private cache, if just regular users just set the public header and if they are logged in then set to no-cache or private cache.

    If it still doesn't work you will need to enable server debug log and check to see if any issues or post it here.
     
  3. Thanks for the tips Jon, really useful. Adding the CacheLookup on and removing the private cache line has fixed it, now seeing a hit.
    I just looked up the vary and I think this is right RewriteRule .? - [E=Cache-Vary:CONCRETE5] so I'll try that too.
     
  4. Will multiple varys work?
    Code:
    RewriteRule .? - [E=Cache-Vary:CONCRETE5]
    RewriteRule .? - [E=Cache-Vary:CONCRETE5_LOGIN]
     
    Last edited by a moderator: Jul 16, 2018 at 2:16 PM
  5. mistwang

    mistwang LiteSpeed Staff

    it wont work, you should add both in one rewrite rule if need. better only using one though.
    Code:
    RewriteRule .? - [E=Cache-Vary:CONCRETE5,CONCRETE5_LOGIN]
     
    Last edited by a moderator: Jul 16, 2018 at 2:16 PM
  6. Thanks for the info :)
     
  7. Next question!
    I'm trying now to figure out how to use esi inline to do a hole punch.
    I have the following code:
    Code:
    <?php
                               $page = Page::getCurrentPage();
                               $pageHandle = $page->getCollectionHandle();
                               if($pageHandle === "esi-test") {
                                   header('X-LiteSpeed-Cache-Control: esi=on');   
                               }
                               ?>
                               <esi:inline name="msm001" fetchable="yes" max-age="5" timeout="60">
                               <?php echo '<p>' . $_SERVER['HTTP_USER_AGENT'] . "\n\n" . '</p>';   ?>
                                </esi:inline>
    
    This is probably wrong but I can't find many examples out there.
    The browser is returning the following response headers:

    X-Frame-Options: SAMEORIGIN
    X-LiteSpeed-Cache-Control: esi=on
    X-Powered-By: PHP/7.0.30

    This seems to disable LS cache on the whole page?
     

Share This Page