Welcome Guest, Not a member yet? Register   Sign In
Output caching using APC
#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 );
        }
    }

}


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