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;
    }
}
#2

[eluser]bd3521[/eluser]
No comments? This is a great solution!! Could you improve this to fail back to default CI file caching if APC is not present?
#3

[eluser]Patrick Savalle[/eluser]
Thats very easy Smile But why would you? Just don't install this module Smile

I do have an improved 'default' cache for you.

http://ellislab.com/forums/viewthread/215823/

I don't know if this already made it to a newer release.
#4

[eluser]bd3521[/eluser]
Improper server config, moving it to another webserver w/o apc, APC craps outs, etc...
It was just a way to make sure it runs well under different environments.

I'll just toss in if ($this->cache->apc->is_supported())
#5

[eluser]Patrick Savalle[/eluser]
Code:
<?php
if ( !defined( 'BASEPATH' ) )
    exit( 'No direct script access allowed' );

// ------------------------------------------------------------------------

/**
* Mobbr APC Output caching class
*
* @package     CodeIgniter
* @subpackage  Libraries
* @category    Core
* @author      Patrick Savalle
* @link
*
* Overrides the caching method of the standard CI_Output class
* Uses setting: /config/config['output_caching_engine'] = ( "apc" | "default" | "dummy" );
*
* Normal CI behaviour on setting = 'default' or when setting above is missing entirely.
*
* Generates an error when APC is not installed.
*/
class MBR_Output extends CI_Output
{

    // --------------------------------------------------------------------

    static private function construct_key( )
    {
        // must be a key that is available to external caches also
        return $_SERVER["REQUEST_URI"];
    }

    // --------------------------------------------------------------------

    /**
     * Write a Cache File
     *
     * @access  public
     * @return  void
     */
    function _write_cache( $output )
    {
        $engine =  get_instance( )->config->item( 'output_caching_engine' );
        if ( empty( $engine ) )
        {
            $engine = 'default';
        }
        switch ( $engine )
        {
            case 'dummy' :
                return;

            case 'apc' :
                apc_store( self::construct_key( ), array( $this->headers, $output ), (int)($this->cache_expiration * 60) );
                return;

            default :
                parent::_write_cache( $output );
                return;
        }
    }

    // --------------------------------------------------------------------

    /**
     * Update/serve a cached file
     *
     * @access  public
     * @return  void
     */
    function _display_cache( &$CFG, &$URI )
    {
        $engine = $CFG->item( 'output_caching_engine' );
        if ( empty( $engine ) )
        {
            $engine = 'default';
        }
        switch ( $engine )
        {
            case 'dummy' :
                return FALSE;

            case 'apc' :
                $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;

            default :
                return parent::_display_cache( $CFG, $URI );
        }
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB