Welcome Guest, Not a member yet? Register   Sign In
Output caching using APC
#1

[eluser]Patrick Savalle[/eluser]
Most serious installations have APC enabled. When using APC you can use it for output caching also. Just place this class in application/core/MY_Ouput.php and your output caching will use APC, avoiding disk-writes and reads.

As a bonus this version also saves the HTTP header with the cache.

Code:
<?php
if ( !defined( 'BASEPATH' ) )
    exit( 'No direct script access allowed' );

/**
* APC Output caching class
*
* @package     CodeIgniter
* @subpackage  Libraries
* @category    Core
* @author      Patrick Savalle
* @link
*
* Overrides the caching method of the standard CI_Output class
*
* Generates an error when APC is not installed.
*/
class MY_Output extends CI_Output
{
    static private function construct_key( )
    {
        return $_SERVER["REQUEST_URI"];
    }

    function _write_cache( $output )
    {
        apc_store( self::construct_key( ), array( $this->headers, $output ), (int)($this->cache_expiration * 60) );
    }

    function _display_cache( &$CFG, &$URI )
    {
        $cache = apc_fetch( self::construct_key( ), $success );
        if ( $success )
        {
            list( $headers, $output ) = $cache;
            if ( count( $headers ) > 0 )
            {
                foreach ( $headers as $header )
                {
                    header( $header[0], $header[1] );
                }
            }
            echo $output;
            return TRUE;
        }
        return FALSE;
    }
}


Messages In This Thread
Output caching using APC - by El Forum - 04-24-2012, 03:03 AM
Output caching using APC - by El Forum - 07-10-2012, 12:53 PM
Output caching using APC - by El Forum - 07-10-2012, 12:58 PM
Output caching using APC - by El Forum - 07-10-2012, 01:17 PM
Output caching using APC - by El Forum - 07-10-2012, 01:20 PM



Theme © iAndrew 2016 - Forum software by © MyBB