Home Closet PHP: Reducing outbound traffic (or reinventing the wheel)

PHP: Reducing outbound traffic (or reinventing the wheel)

by admin

In connection with the development of a mobile version of the site I have encountered a problem – a lot of data is transmitted, As a consequence a lot of money is spent on a cell phone, it was decided to write a few functions to reduce the transferred data
First of all, the size of the output HTML was reduced with a simple class:

class OutBufferFilter {
function OutBufferFilter(){
if (@ob_start(array($ this , ‘filter’ ))){
register_shutdown_function( array($ this , ‘shutdown’ ));
}
}
function filter( $chunk, $mode ){
return (
str_replace( ><‘ , ><‘ ,
ereg_replace( "[ \t\r\n]{2, }" , ‘ ‘ ,
$chunk
)
)
);
}
function shutdown() {
while ( @ob_end_flush() );
}
} * This source code was highlighted with Source Code Highlighter

Of course, the filter method is not perfect, but with the HTML code I have it works (no pre tags are used in the design)
Then I added gzip compression:

class OutBufferFilterGZ extends OutBufferFilter {
var $use_gzhandler = false ;
function OutBufferFilterGZ() {
$ this ->use_gzhandler = function_exists( ‘ob_gzhandler’ );
parent::OutBufferFilter();
}
function filter( $chunk, $mode ) {
return (
$ this ->use_gzhandler?
ob_gzhandler( parent::filter( $chunk ), $mode ):
parent::filter( $chunk, $mode )
);
}
} * This source code was highlighted with Source Code Highlighter

Then the class was extended to cache received pages in memcached, and ifyou use nginx as the frontend, the content is served directly from memecache without php

class OutBufferCached extends OutBufferFilterGZ {
var $memcache = false ;
var $content = ;
var $timeout = 10;
function key() {
global $_SERVER;
return (( isset( $_SERVER )
and isset( $_SERVER[ "REQUEST_METHOD" ] )
and $_SERVER[ "REQUEST_METHOD" ] == GET’
)?
( pda:html:’
(isset( $_SERVER[ "SCRIPT_NAME" ] )? $_SERVER[ "SCRIPT_NAME" ]: )
(isset( $_SERVER[ "PATH_INFO" ] )? $_SERVER[ "PATH_INFO" ]: )
‘?’
(isset( $_SERVER[ "REQUEST_URI" ] )? $_SERVER[ "REQUEST_URI" ]: )
):

);
}
function OutBufferCached( $memcache = false ) {
$key = $ this -> key();
if ( !empty($key) and $memcache and @$memcache-> getVersion() !== false ) {
$ this -> memcache = $memcache;
$ this -> content =$ this -> memcache-> get( $key );
if ( $ this -> content !== false ) {
$ this -> shutdown( false );
exit();
}
else {
$ this -> content = ;
}
}
parent::OutBufferFilterGZ();
}
function filter( $chunk, $mode ) {
$ this -> content .= parent::filter( $chunk, $mode );
return ( null );
}
function shutdown( $store = true ) {
parent::shutdown();
$ this -> output();
if ( $store ) {
$key = $ this -> key();
if ( $ this -> memcache and !empty($key) and $ this -> timeout) {
$ this -> memcache-> set(
$key,
$ this -> content,
MEMCACHE_COMPRESSED,
$ this -> timeout
);
}
}
$ this -> content = ;
}
function output() {
echo $ this -> content;
flush();
}
function timeout( $t = 10 ) {
$ this -> timeout = $t;
}
function nocache() {
$ this -> timeout( 0 );
}
} * This source code was highlighted with Source Code Highlighter

All tested with PHP4 and PHP5 – no problems so far.
Actually I would like to know how interesting this bike I invented ?
Update: I have highlighted the code (but on source.virtser.net/default.aspx there is no PHP and the ones with PHP are not good for habrahabr.ru 🙂
Update 2: Stats on compression support in mobile devices : waplog.net/en/html/stat/1/gzip

You may also like