<? xml version ="1.0" encoding ="utf-8" ? >
< install version ="1.5" type ="plugin" group ="system" >
< name > System - HTML cache </ name >
< author ></ author >
< creationDate > March 2010 </ creationDate >
< copyright ></ copyright >
< license > www.gnu.org/licenses/gpl-2.0.html GNU/GPL </ license >
< authorEmail ></ authorEmail >
< authorUrl ></ authorUrl >
< version > 0.6 </ version >
< description > Creates static HTML versions of content pages. It will only cache SEO URLs, *.php pages will not be cached. </ description >
< files >
< filename plugin ="htmlcache" > htmlcache.php </ filename >
</ files >
< params >
< param name ="html_cache_dir" type ="text" default ="" label ="HTML Cache dir" description ="Set the joomla HTML cache directory." />
< param name ="cache_view_1" type ="text" default ="article" label ="View to cache 1" description ="Set view type to be cached. Only HTTP content can be cached." />
< param name ="cache_view_2" type ="text" default ="" label ="View to cache 2" description ="Set view type to be cached. Only HTTP content can be cached." />
< param name ="cache_view_3" type ="text" default ="" label ="View to cache 3" description ="Set view type to be cached. Only HTTP content can be cached." />
< param name ="cache_view_4" type ="text" default ="" label ="View to cache 4" description ="Set view type to be cached. Only HTTP content can be cached." />
</ params >
</ install >
* This source code was highlighted with Source Code Highlighter .
<? php
defined ( '_JEXEC' ) or die ( 'Restricted access' ) ;
jimport ( 'joomla.plugin.plugin' ) ;
class plgSystemHtmlcache extends JPlugin
{
function plgSystemHtmlcache ( & $ subject , $ config )
{
parent :: __construct ( $ subject , $ config ) ;
}
function onAfterRender ( )
{
global $ mainframe ;
if ( $ mainframe -> isAdmin ( ) ) { return ; } // do not cache admin pages
$ document = & JFactory :: getDocument ( ) ;
$ doctype = $ document -> getType ( ) ;
$ user = & JFactory :: getUser ( ) ;
if ( $ user -> get ( 'guest' ) ! = 1 ) { return ; } // Only cache for non logged in users
if ( $ doctype ! = 'html' ) { return ; } // Only render for HTML output
$ html_cache_dir = $ this -> param ( 'html_cache_dir' ) ;
if ( $ html_cache_dir == '' ) { return ; } // Exit if no html_cache_dir specified
if ( ! file_exists ( $ html_cache_dir ) ) { mkdir ( $ html_cache_dir ) ; } // try to create folder if it does not exist
// Only render for provided views
if ( ( JRequest :: getVar ( 'view' ) ) ! = $ this -> param ( 'cache_view_1' ) &&
( JRequest :: getVar ( 'view' ) ) ! = $ This -> param ( 'cache_view_2' ) &&
( JRequest :: getVar ( 'view' ) ) ! = $ This -> param ( 'cache_view_3' ) &&
( JRequest :: getVar ( 'view' ) ) ! = $ This -> param ( 'cache_view_4' ) ) { return ; }
$ relativePath = $ this -> request_uri ( ) ;
if ( strpos ( $ relativePath , '.' ) ) { return ; } // exit if found in the request_uri
$ relativePath = str_replace ( '/' , DS , $ relativePath ) ;
// $ body = Minify_HTML :: minify (JResponse :: getBody ());
$ body = JResponse :: getBody ( ) ;
$ fullPath = $ html_cache_dir . $ relativePath ;
$ parts = explode ( DS , $ relativePath ) ;
$ currentPath = $ html_cache_dir . DS ;
foreach ( $ parts as $ p ) {
if ( $ p == '' ) {
continue ;
}
$ currentPath . = $ p ;
if ( ( ! file_exists ( $ currentPath ) ) && ( ! is_file ( $ currentPath ) ) ) {
mkdir ( $ currentPath ) ;
}
$ currentPath . = DS ;
} // end for each
$ indexFile = $ currentPath . DS . 'index.html' ;
if ( ! file_exists ( $ indexFile ) ) { $ this -> writeToFile ( $ indexFile , $ body ) ; }
}
function writeToFile ( $ fileName , $ content ) {
$ handle = fopen ( $ fileName , 'w' ) ;
fwrite ( $ handle , $ content ) ;
fclose ( $ handle ) ;
}
function request_uri ( ) {
if ( isset ( $ _SERVER [ 'REQUEST_URI' ] ) ) {
$ uri = $ _SERVER [ 'REQUEST_URI' ] ;
} else {
if ( isset ( $ _SERVER [ 'argv' ] ) ) {
$ uri = $ _SERVER [ 'PHP_SELF' ] . '?' . $ _SERVER [ 'argv' ] [ 0 ] ;
} else {
$ uri = $ _SERVER [ 'PHP_SELF' ] . '?' . $ _SERVER [ 'QUERY_STRING' ] ;
}
}
return $ uri ;
}
function param ( $ name ) {
static $ plugin , $ pluginParams ;
if ( ! isset ( $ plugin ) ) {
$ plugin = & JPluginHelper :: getPlugin ( 'system' , 'htmlcache' ) ;
$ pluginParams = new JParameter ( $ plugin -> params ) ;
}
return $ pluginParams -> get ( $ name ) ;
}
}
RewriteCond %{REQUEST_URI} (/|/[^.]*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/cache/content/%{REQUEST_URI}/index.html -f
RewriteRule (.*) /cache/html/$1/index.html [L]
ab -n 10000 -c 50 -k www.azati.com
...
Concurrency Level: 50
Time taken for tests: 4.294054 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 10000
Total transferred: 71155755 bytes
HTML transferred: 67695750 bytes
Requests per second: 2328.80 [#/sec] (mean)
Time per request: 21.470 [ms] (mean)
Time per request: 0.429 [ms] (mean, across all concurrent requests)
Transfer rate: 16182.38 [Kbytes/sec] received
...
Source: https://habr.com/ru/post/92783/
All Articles