Welcome Guest, Not a member yet? Register   Sign In
Disable Output Cache for Logged in Users
#1

[eluser]rickyracoon[/eluser]
Is there any way to disable output caching for logged in users? I saw a technique described in this thread which is in line with what I need to accomplish, but the posted solution seems like it requires a lot of needless processing that may hinder some of the performance benefit of caching:

http://ellislab.com/forums/viewthread/151752/#735929

Any thoughts/solutions/hacks would be appreciated.
#2

[eluser]rickyracoon[/eluser]
Only way I can think of is to use the native session to keep track of auth state and edit Codeigniter.php to look like this:

Code:
session_start();
// Skip caching when user is logged in
if (!isset($_SESSION["auth_enabled"]) || $_SESSION["auth_enabled"] == FALSE)
{
  if ($EXT->_call_hook('cache_override') === FALSE)
  {
   if ($OUT->_display_cache($CFG, $URI) == TRUE)
   {
    exit;
   }
  }
}

Then in MY_Controller.php I would do this:

Code:
session_start();
if ($this->ion_auth->logged_in())
{
  $_SESSION["auth_enabled"] = TRUE;
}        
else
{
  $_SESSION["auth_enabled"] = FALSE;
}

Does anyone see any potential problems or drawback with doing it this way (other than the obvious...hacking core, etc...)? I'm using ion_auth library of course for authentication.
#3

[eluser]rickyracoon[/eluser]
Ok, so I did some testing and I think I have it working now, but it required a lot of hacking. Here are the final code modifications I made.

/system/application/libraries/MY_Controller.php (add this snippet to constructor):
Code:
if ( ! isset( $_SESSION ) )
{
  session_start();
}
if ( ! $this->ion_auth->logged_in() )
{
  $_SESSION["auth_enabled"] = FALSE;
}
else
{
  $_SESSION["auth_enabled"] = TRUE;
}


/system/libraries/Output.php (added code below to beginning of functions):
Code:
function _write_cache( $output )
{
  if ( ! isset( $_SESSION ) )
  {
    session_start();
  }  
  // Skip caching when user is logged in
  if ( isset( $_SESSION["auth_enabled"] ) && $_SESSION["auth_enabled"] == TRUE )
  {
    return;
  }

  ...original function content here...
}

function _display_cache( &$CFG, &$URI )
{
  if ( ! isset( $_SESSION ) )
  {
    session_start();
  }  
  // Skip caching when user is logged in
  if ( isset( $_SESSION["auth_enabled"] ) && $_SESSION["auth_enabled"] == TRUE )
  {
    return FALSE;
  }

  ...original function content here...
}


/system/codeigniter/Codeigniter.php (modified section below):
Code:
/*
* ------------------------------------------------------
* Is there a valid cache file?  If so, we're done...
* ------------------------------------------------------
*/
if ( ! isset( $_SESSION ) )
{
  session_start();
}  
// Skip caching when user is logged in
if ( isset( $_SESSION["auth_enabled"] ) && $_SESSION["auth_enabled"] == TRUE )
{
  // do nothing with caching
}
else
{
  if ($EXT->_call_hook('cache_override') === FALSE)
  {
    if ($OUT->_display_cache($CFG, $URI) == TRUE)
    {
      exit;
    }
  }
}

And that did the trick. Now, caching works as expected when user is not logged in. When they log in, caching is essentially disabled or skipped. Hope this helps someone who needs something similar. I know this isn't the cleanest approach would love some feedback.




Theme © iAndrew 2016 - Forum software by © MyBB