📜 ⬆️ ⬇️

PHP: Reduce outgoing traffic (or reinvent the next bike)

In connection with the development of the mobile version of the site I ran into a problem - a lot of data is transmitted, as a result, a lot of money is spent on a mobile phone, it was decided to write several functions designed to reduce the transmitted data

Firstly, the size of the HTML output was reduced using 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 .

It is clear that the filter method is not perfect, but on the HTML code that I have it works (the design does not use pre-type tags)
Then gzip compression was added:
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 expanded to ensure that the received pages were cached in memcached, and if nginx is used as a frontend, the content was given without php, but directly from memecache
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 .

Everything was tested on PHP4 and PHP5 - while there were no problems.
Actually, I would like to know how interesting is the bicycle I invented?
Update: Made a code highlighting (though there is no PHP source on source.virtser.net/default.aspx , and others where PHP is not suitable for habrahabr.ru :-)
Update 2: Mobile Compression Support Statistics : waplog.net/ru/html/stat/1/gzip

Source: https://habr.com/ru/post/30096/


All Articles