PDF rausgenommen
This commit is contained in:
1811
msd2/wordpress/wp-content/plugins/cachify/inc/cachify.class.php
Normal file
1811
msd2/wordpress/wp-content/plugins/cachify/inc/cachify.class.php
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,188 @@
|
||||
<?php
|
||||
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
|
||||
/**
|
||||
* Cachify_APC
|
||||
*/
|
||||
|
||||
final class Cachify_APC {
|
||||
|
||||
|
||||
/**
|
||||
* Availability check
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return boolean true/false TRUE when installed
|
||||
*/
|
||||
|
||||
public static function is_available()
|
||||
{
|
||||
return extension_loaded('apc');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Caching method as string
|
||||
*
|
||||
* @since 2.1.2
|
||||
* @change 2.1.2
|
||||
*
|
||||
* @return string Caching method
|
||||
*/
|
||||
|
||||
public static function stringify_method() {
|
||||
return 'APC';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Speicherung im Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $data Inhalt des Eintrags
|
||||
* @param integer $lifetime Lebensdauer des Eintrags
|
||||
*/
|
||||
|
||||
public static function store_item($hash, $data, $lifetime)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) or empty($data) ) {
|
||||
wp_die('APC add item: Empty input.');
|
||||
}
|
||||
|
||||
/* Store */
|
||||
apc_store(
|
||||
$hash,
|
||||
gzencode( $data . self::_cache_signatur(), 9),
|
||||
$lifetime
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lesen aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @return mixed $diff Wert des Eintrags
|
||||
*/
|
||||
|
||||
public static function get_item($hash)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) ) {
|
||||
wp_die('APC get item: Empty input.');
|
||||
}
|
||||
|
||||
return ( function_exists('apc_exists') ? apc_exists($hash) : apc_fetch($hash) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Entfernung aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $url URL des Eintrags [optional]
|
||||
*/
|
||||
|
||||
public static function delete_item($hash, $url = '')
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) ) {
|
||||
wp_die('APC delete item: Empty input.');
|
||||
}
|
||||
|
||||
/* Löschen */
|
||||
apc_delete($hash);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Leerung des Cache
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @change 2.0.7
|
||||
*/
|
||||
|
||||
public static function clear_cache()
|
||||
{
|
||||
if ( ! self::is_available() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
@apc_clear_cache('user');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ausgabe des Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*/
|
||||
|
||||
public static function print_cache()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ermittlung der Cache-Größe
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return mixed $diff Cache-Größe
|
||||
*/
|
||||
|
||||
public static function get_stats()
|
||||
{
|
||||
/* Infos */
|
||||
$data = apc_cache_info('user');
|
||||
|
||||
/* Leer */
|
||||
if ( empty($data['mem_size']) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return $data['mem_size'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generierung der Signatur
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0.5
|
||||
*
|
||||
* @return string $diff Signatur als String
|
||||
*/
|
||||
|
||||
private static function _cache_signatur()
|
||||
{
|
||||
return sprintf(
|
||||
"\n\n<!-- %s\n%s @ %s -->",
|
||||
'Cachify | http://cachify.de',
|
||||
'APC Cache',
|
||||
date_i18n(
|
||||
'd.m.Y H:i:s',
|
||||
current_time('timestamp')
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
|
||||
/**
|
||||
* Cachify_DB
|
||||
*/
|
||||
|
||||
final class Cachify_DB {
|
||||
|
||||
|
||||
/**
|
||||
* Availability check
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return boolean true/false TRUE when installed
|
||||
*/
|
||||
|
||||
public static function is_available()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Caching method as string
|
||||
*
|
||||
* @since 2.1.2
|
||||
* @change 2.1.2
|
||||
*
|
||||
* @return string Caching method
|
||||
*/
|
||||
|
||||
public static function stringify_method() {
|
||||
return 'DB';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Speicherung im Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $data Inhalt des Eintrags
|
||||
* @param integer $lifetime Lebensdauer des Eintrags
|
||||
*/
|
||||
|
||||
public static function store_item($hash, $data, $lifetime)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) or empty($data) ) {
|
||||
wp_die('DB add item: Empty input.');
|
||||
}
|
||||
|
||||
/* Store */
|
||||
set_transient(
|
||||
$hash,
|
||||
array(
|
||||
'data' => $data,
|
||||
'meta' => array(
|
||||
'queries' => self::_page_queries(),
|
||||
'timer' => self::_page_timer(),
|
||||
'memory' => self::_page_memory(),
|
||||
'time' => current_time('timestamp')
|
||||
)
|
||||
),
|
||||
$lifetime
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lesen aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @return mixed $diff Wert des Eintrags
|
||||
*/
|
||||
|
||||
public static function get_item($hash)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) ) {
|
||||
wp_die('DB get item: Empty input.');
|
||||
}
|
||||
|
||||
return get_transient($hash);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Entfernung aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $url URL des Eintrags [optional]
|
||||
*/
|
||||
|
||||
public static function delete_item($hash, $url = '')
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($hash) ) {
|
||||
wp_die('DB delete item: Empty input.');
|
||||
}
|
||||
|
||||
/* Löschen */
|
||||
delete_transient($hash);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Leerung des Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*/
|
||||
|
||||
public static function clear_cache()
|
||||
{
|
||||
/* Init */
|
||||
global $wpdb;
|
||||
|
||||
/* Löschen */
|
||||
$wpdb->query("DELETE FROM `" .$wpdb->options. "` WHERE `option_name` LIKE ('\_transient%.cachify')");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ausgabe des Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0.2
|
||||
*
|
||||
* @param array $cache Array mit Cache-Werten
|
||||
*/
|
||||
|
||||
public static function print_cache($cache)
|
||||
{
|
||||
/* Kein Array? */
|
||||
if ( ! is_array($cache) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Content */
|
||||
echo $cache['data'];
|
||||
|
||||
/* Signatur */
|
||||
if ( isset($cache['meta']) ) {
|
||||
echo self::_cache_signatur($cache['meta']);
|
||||
}
|
||||
|
||||
/* Raus */
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ermittlung der Cache-Größe
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return integer $diff Spaltengröße
|
||||
*/
|
||||
|
||||
public static function get_stats()
|
||||
{
|
||||
/* Init */
|
||||
global $wpdb;
|
||||
|
||||
/* Auslesen */
|
||||
return $wpdb->get_var(
|
||||
"SELECT SUM( CHAR_LENGTH(option_value) ) FROM `" .$wpdb->options. "` WHERE `option_name` LIKE ('\_transient%.cachify')"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generierung der Signatur
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0.5
|
||||
*
|
||||
* @param array $meta Inhalt der Metadaten
|
||||
* @return string $diff Signatur als String
|
||||
*/
|
||||
|
||||
private static function _cache_signatur($meta)
|
||||
{
|
||||
/* Kein Array? */
|
||||
if ( ! is_array($meta) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
return sprintf(
|
||||
"\n\n<!--\n%s\n%s\n%s\n%s\n-->",
|
||||
'Cachify | http://cachify.de',
|
||||
sprintf(
|
||||
'Ohne Plugin: %d DB-Anfragen, %s Sekunden, %s',
|
||||
$meta['queries'],
|
||||
$meta['timer'],
|
||||
$meta['memory']
|
||||
),
|
||||
sprintf(
|
||||
'Mit Plugin: %d DB-Anfragen, %s Sekunden, %s',
|
||||
self::_page_queries(),
|
||||
self::_page_timer(),
|
||||
self::_page_memory()
|
||||
),
|
||||
sprintf(
|
||||
'Generiert: %s zuvor',
|
||||
human_time_diff($meta['time'], current_time('timestamp'))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rückgabe der Query-Anzahl
|
||||
*
|
||||
* @since 0.1
|
||||
* @change 2.0
|
||||
*
|
||||
* @return intval $diff Query-Anzahl
|
||||
*/
|
||||
|
||||
private static function _page_queries()
|
||||
{
|
||||
return $GLOBALS['wpdb']->num_queries;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rückgabe der Ausführungszeit
|
||||
*
|
||||
* @since 0.1
|
||||
* @change 2.0
|
||||
*
|
||||
* @return intval $diff Anzahl der Sekunden
|
||||
*/
|
||||
|
||||
private static function _page_timer()
|
||||
{
|
||||
return timer_stop(0, 2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rückgabe des Speicherverbrauchs
|
||||
*
|
||||
* @since 0.7
|
||||
* @change 2.0
|
||||
*
|
||||
* @return string $diff Konvertierter Größenwert
|
||||
*/
|
||||
|
||||
private static function _page_memory()
|
||||
{
|
||||
return ( function_exists('memory_get_usage') ? size_format(memory_get_usage(), 2) : 0 );
|
||||
}
|
||||
}
|
@ -0,0 +1,392 @@
|
||||
<?php
|
||||
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
|
||||
/**
|
||||
* Cachify_HDD
|
||||
*/
|
||||
|
||||
final class Cachify_HDD {
|
||||
|
||||
|
||||
/**
|
||||
* Availability check
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return boolean true/false TRUE when installed
|
||||
*/
|
||||
|
||||
public static function is_available()
|
||||
{
|
||||
return get_option('permalink_structure');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Caching method as string
|
||||
*
|
||||
* @since 2.1.2
|
||||
* @change 2.1.2
|
||||
*
|
||||
* @return string Caching method
|
||||
*/
|
||||
|
||||
public static function stringify_method() {
|
||||
return 'HDD';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Speicherung im Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags [optional]
|
||||
* @param string $data Inhalt des Eintrags
|
||||
* @param integer $lifetime Lebensdauer des Eintrags [optional]
|
||||
*/
|
||||
|
||||
public static function store_item($hash, $data, $lifetime)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($data) ) {
|
||||
wp_die('HDD add item: Empty input.');
|
||||
}
|
||||
|
||||
/* Speichern */
|
||||
self::_create_files(
|
||||
$data . self::_cache_signatur()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lesen aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return boolean $diff TRUE wenn Cache vorhanden
|
||||
*/
|
||||
|
||||
public static function get_item()
|
||||
{
|
||||
return is_readable(
|
||||
self::_file_html()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Entfernen aus dem Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $hash Hash des Eintrags [optional]
|
||||
* @param string $url URL des Eintrags
|
||||
*/
|
||||
|
||||
public static function delete_item($hash = '', $url)
|
||||
{
|
||||
/* Leer? */
|
||||
if ( empty($url) ) {
|
||||
wp_die('HDD delete item: Empty input.');
|
||||
}
|
||||
|
||||
/* Löschen */
|
||||
self::_clear_dir(
|
||||
self::_file_path($url)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Leerung des Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*/
|
||||
|
||||
public static function clear_cache()
|
||||
{
|
||||
self::_clear_dir(
|
||||
CACHIFY_CACHE_DIR
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ausgabe des Cache
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*/
|
||||
|
||||
public static function print_cache()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ermittlung der Cache-Größe
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return integer $diff Ordnergröße
|
||||
*/
|
||||
|
||||
public static function get_stats()
|
||||
{
|
||||
return self::_dir_size( CACHIFY_CACHE_DIR );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generierung der Signatur
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0.5
|
||||
*
|
||||
* @return string $diff Signatur als String
|
||||
*/
|
||||
|
||||
private static function _cache_signatur()
|
||||
{
|
||||
return sprintf(
|
||||
"\n\n<!-- %s\n%s @ %s -->",
|
||||
'Cachify | http://cachify.de',
|
||||
'HDD Cache',
|
||||
date_i18n(
|
||||
'd.m.Y H:i:s',
|
||||
current_time('timestamp')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialisierung des Cache-Speichervorgangs
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $data Cache-Inhalt
|
||||
*/
|
||||
|
||||
private static function _create_files($data)
|
||||
{
|
||||
/* Ordner anlegen */
|
||||
if ( ! wp_mkdir_p( self::_file_path() ) ) {
|
||||
wp_die('Unable to create directory.');
|
||||
}
|
||||
|
||||
/* Dateien schreiben */
|
||||
self::_create_file( self::_file_html(), $data );
|
||||
self::_create_file( self::_file_gzip(), gzencode($data, 9) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Anlegen der Cache-Datei
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $file Pfad der Cache-Datei
|
||||
* @param string $data Cache-Inhalt
|
||||
*/
|
||||
|
||||
private static function _create_file($file, $data)
|
||||
{
|
||||
/* Beschreibbar? */
|
||||
if ( ! $handle = @fopen($file, 'wb') ) {
|
||||
wp_die('Could not write file.');
|
||||
}
|
||||
|
||||
/* Schreiben */
|
||||
@fwrite($handle, $data);
|
||||
fclose($handle);
|
||||
clearstatcache();
|
||||
|
||||
/* Permissions */
|
||||
$stat = @stat( dirname($file) );
|
||||
$perms = $stat['mode'] & 0007777;
|
||||
$perms = $perms & 0000666;
|
||||
@chmod($file, $perms);
|
||||
clearstatcache();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Rekrusive Leerung eines Ordners
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0.5
|
||||
*
|
||||
* @param string $dir Ordnerpfad
|
||||
*/
|
||||
|
||||
private static function _clear_dir($dir) {
|
||||
/* Weg mit dem Slash */
|
||||
$dir = untrailingslashit($dir);
|
||||
|
||||
/* Ordner? */
|
||||
if ( ! is_dir($dir) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Einlesen */
|
||||
$objects = array_diff(
|
||||
scandir($dir),
|
||||
array('..', '.')
|
||||
);
|
||||
|
||||
/* Leer? */
|
||||
if ( empty($objects) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Loopen */
|
||||
foreach ( $objects as $object ) {
|
||||
/* Um Pfad erweitern */
|
||||
$object = $dir. DIRECTORY_SEPARATOR .$object;
|
||||
|
||||
/* Ordner/Datei */
|
||||
if ( is_dir($object) ) {
|
||||
self::_clear_dir($object);
|
||||
} else {
|
||||
unlink($object);
|
||||
}
|
||||
}
|
||||
|
||||
/* Killen */
|
||||
@rmdir($dir);
|
||||
|
||||
/* Aufräumen */
|
||||
clearstatcache();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ermittlung der Ordnergröße
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $dir Ordnerpfad
|
||||
* @return mixed $size Ordnergröße
|
||||
*/
|
||||
|
||||
public static function _dir_size($dir = '.')
|
||||
{
|
||||
/* Ordner? */
|
||||
if ( ! is_dir($dir) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Einlesen */
|
||||
$objects = array_diff(
|
||||
scandir($dir),
|
||||
array('..', '.')
|
||||
);
|
||||
|
||||
/* Leer? */
|
||||
if ( empty($objects) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Init */
|
||||
$size = 0;
|
||||
|
||||
/* Loopen */
|
||||
foreach ( $objects as $object ) {
|
||||
/* Um Pfad erweitern */
|
||||
$object = $dir. DIRECTORY_SEPARATOR .$object;
|
||||
|
||||
/* Ordner/Datei */
|
||||
if ( is_dir($object) ) {
|
||||
$size += self::_dir_size($object);
|
||||
} else {
|
||||
$size += filesize($object);
|
||||
}
|
||||
}
|
||||
|
||||
return $size;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pfad der Cache-Datei
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @param string $path Request-URI oder Permalink [optional]
|
||||
* @return string $diff Pfad zur Cache-Datei
|
||||
*/
|
||||
|
||||
private static function _file_path($path = NULL)
|
||||
{
|
||||
$prefix = is_ssl() ? 'https-' : '';
|
||||
|
||||
$path = sprintf(
|
||||
'%s%s%s%s%s',
|
||||
CACHIFY_CACHE_DIR,
|
||||
DIRECTORY_SEPARATOR,
|
||||
$prefix,
|
||||
parse_url(
|
||||
'http://' .strtolower($_SERVER['HTTP_HOST']),
|
||||
PHP_URL_HOST
|
||||
),
|
||||
parse_url(
|
||||
( $path ? $path : $_SERVER['REQUEST_URI'] ),
|
||||
PHP_URL_PATH
|
||||
)
|
||||
);
|
||||
|
||||
if ( validate_file($path) > 0 ) {
|
||||
wp_die('Invalide file path.');
|
||||
}
|
||||
|
||||
return trailingslashit($path);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pfad der HTML-Datei
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return string $diff Pfad zur HTML-Datei
|
||||
*/
|
||||
|
||||
private static function _file_html()
|
||||
{
|
||||
return self::_file_path(). 'index.html';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pfad der GZIP-Datei
|
||||
*
|
||||
* @since 2.0
|
||||
* @change 2.0
|
||||
*
|
||||
* @return string $diff Pfad zur GZIP-Datei
|
||||
*/
|
||||
|
||||
private static function _file_gzip()
|
||||
{
|
||||
return self::_file_path(). 'index.html.gz';
|
||||
}
|
||||
}
|
@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
|
||||
/* Quit */
|
||||
defined('ABSPATH') OR exit;
|
||||
|
||||
|
||||
/**
|
||||
* Cachify_MEMCACHED
|
||||
*/
|
||||
|
||||
final class Cachify_MEMCACHED {
|
||||
|
||||
|
||||
/**
|
||||
* Memcached-Object
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @var object
|
||||
*/
|
||||
|
||||
private static $_memcached;
|
||||
|
||||
|
||||
/**
|
||||
* Availability check
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return boolean true/false TRUE when installed
|
||||
*/
|
||||
|
||||
public static function is_available()
|
||||
{
|
||||
return class_exists('Memcached') && isset($_SERVER['SERVER_SOFTWARE']) && strpos(strtolower($_SERVER['SERVER_SOFTWARE']), 'nginx') !== false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Caching method as string
|
||||
*
|
||||
* @since 2.1.2
|
||||
* @change 2.1.2
|
||||
*
|
||||
* @return string Caching method
|
||||
*/
|
||||
|
||||
public static function stringify_method() {
|
||||
return 'Memcached';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Speicherung im Cache
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $data Inhalt des Eintrags
|
||||
* @param integer $lifetime Lebensdauer des Eintrags
|
||||
*/
|
||||
|
||||
public static function store_item($hash, $data, $lifetime)
|
||||
{
|
||||
/* Empty? */
|
||||
if ( empty($data) ) {
|
||||
wp_die('MEMCACHE add item: Empty input.');
|
||||
}
|
||||
|
||||
/* Server connect */
|
||||
if ( ! self::_connect_server() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Add item */
|
||||
self::$_memcached->set(
|
||||
self::_file_path(),
|
||||
$data . self::_cache_signatur(),
|
||||
$lifetime
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Lesen aus dem Cache
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @return mixed $diff Wert des Eintrags
|
||||
*/
|
||||
|
||||
public static function get_item($hash)
|
||||
{
|
||||
/* Server connect */
|
||||
if ( ! self::_connect_server() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get item */
|
||||
return self::$_memcached->get(
|
||||
self::_file_path()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Entfernung aus dem Cache
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @param string $hash Hash des Eintrags
|
||||
* @param string $url URL des Eintrags [optional]
|
||||
*/
|
||||
|
||||
public static function delete_item($hash, $url = '')
|
||||
{
|
||||
/* Server connect */
|
||||
if ( ! self::_connect_server() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Delete */
|
||||
self::$_memcached->delete(
|
||||
self::_file_path($url)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Leerung des Cache
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*/
|
||||
|
||||
public static function clear_cache()
|
||||
{
|
||||
/* Server connect */
|
||||
if ( ! self::_connect_server() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Flush */
|
||||
@self::$_memcached->flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ausgabe des Cache
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*/
|
||||
|
||||
public static function print_cache()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Ermittlung der Cache-Größe
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return mixed $diff Cache-Größe
|
||||
*/
|
||||
|
||||
public static function get_stats()
|
||||
{
|
||||
/* Server connect */
|
||||
if ( ! self::_connect_server() ) {
|
||||
wp_die('MEMCACHE: Not enabled.');
|
||||
}
|
||||
|
||||
/* Infos */
|
||||
$data = self::$_memcached->getStats();
|
||||
|
||||
/* No stats? */
|
||||
if ( empty($data) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Get first key */
|
||||
$data = $data[key($data)];
|
||||
|
||||
/* Leer */
|
||||
if ( empty($data['bytes']) ) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return $data['bytes'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generierung der Signatur
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @return string $diff Signatur als String
|
||||
*/
|
||||
|
||||
private static function _cache_signatur()
|
||||
{
|
||||
return sprintf(
|
||||
"\n\n<!-- %s\n%s @ %s -->",
|
||||
'Cachify | http://cachify.de',
|
||||
'Memcached',
|
||||
date_i18n(
|
||||
'd.m.Y H:i:s',
|
||||
current_time('timestamp')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pfad der Cache-Datei
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.0.7
|
||||
*
|
||||
* @param string $path Request-URI oder Permalink [optional]
|
||||
* @return string $diff Pfad zur Cache-Datei
|
||||
*/
|
||||
|
||||
private static function _file_path($path = NULL)
|
||||
{
|
||||
return trailingslashit(
|
||||
sprintf(
|
||||
'%s%s',
|
||||
$_SERVER['HTTP_HOST'],
|
||||
parse_url(
|
||||
( $path ? $path : $_SERVER['REQUEST_URI'] ),
|
||||
PHP_URL_PATH
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connect to Memcached server
|
||||
*
|
||||
* @since 2.0.7
|
||||
* @change 2.1.8
|
||||
*
|
||||
* @hook array cachify_memcached_servers Array with memcached servers
|
||||
*
|
||||
* @return boolean true/false TRUE bei Erfolg
|
||||
*/
|
||||
|
||||
private static function _connect_server()
|
||||
{
|
||||
/* Not enabled? */
|
||||
if ( ! self::is_available() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Already connected */
|
||||
if ( is_object(self::$_memcached) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Init */
|
||||
self::$_memcached = new Memcached();
|
||||
|
||||
/* Set options */
|
||||
if ( defined('HHVM_VERSION') ) {
|
||||
self::$_memcached->setOption( Memcached::OPT_COMPRESSION, false );
|
||||
self::$_memcached->setOption( Memcached::OPT_BUFFER_WRITES, true );
|
||||
self::$_memcached->setOption( Memcached::OPT_BINARY_PROTOCOL, true );
|
||||
} else {
|
||||
self::$_memcached->setOptions(
|
||||
array(
|
||||
Memcached::OPT_COMPRESSION => false,
|
||||
Memcached::OPT_BUFFER_WRITES => true,
|
||||
Memcached::OPT_BINARY_PROTOCOL => true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* Connect */
|
||||
self::$_memcached->addServers(
|
||||
(array)apply_filters(
|
||||
'cachify_memcached_servers',
|
||||
array(
|
||||
array(
|
||||
'127.0.0.1',
|
||||
11211
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user