| 1 |
<?php |
| 2 |
/** |
| 3 |
* APCu handling |
| 4 |
* |
| 5 |
* Handles all APCu operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace OPcacheManager\System; |
| 13 |
|
| 14 |
use OPcacheManager\System\Logger; |
| 15 |
use OPcacheManager\System\Option; |
| 16 |
use OPcacheManager\System\File; |
| 17 |
|
| 18 |
/** |
| 19 |
* Define the APCu functionality. |
| 20 |
* |
| 21 |
* Handles all APCu operations and detection. |
| 22 |
* |
| 23 |
* @package System |
| 24 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
class APCu { |
| 28 |
|
| 29 |
/** |
| 30 |
* The list of status. |
| 31 |
* |
| 32 |
* @since 1.0.0 |
| 33 |
* @var array $status Maintains the status list. |
| 34 |
*/ |
| 35 |
public static $status = [ 'disabled', 'enabled', 'recycle_in_progress' ]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Initializes the class and set its properties. |
| 39 |
* |
| 40 |
* @since 1.0.0 |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Sets APCu identification hook. |
| 47 |
* |
| 48 |
* @since 1.0.0 |
| 49 |
*/ |
| 50 |
public static function init() { |
| 51 |
add_filter( 'perfopsone_apcu_info', [ self::class, 'perfopsone_apcu_info' ] ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Adds APCu identification. |
| 56 |
* |
| 57 |
* @param array $apcu The already set identifiers. |
| 58 |
* @return array The extended identifiers if needed. |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public static function perfopsone_apcu_info( $apcu ) { |
| 62 |
$apcu[ OPCM_SLUG ] = [ |
| 63 |
'name' => OPCM_PRODUCT_NAME, |
| 64 |
]; |
| 65 |
return $apcu; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get name and version. |
| 70 |
* |
| 71 |
* @return string The name and version of the product. |
| 72 |
* @since 1.0.0 |
| 73 |
*/ |
| 74 |
public static function name() { |
| 75 |
$result = ''; |
| 76 |
if ( function_exists( 'apcu_cache_info' ) ) { |
| 77 |
$result = 'APCu'; |
| 78 |
$info = apcu_cache_info( false ); |
| 79 |
if ( array_key_exists( 'memory_type', $info ) ) { |
| 80 |
$result .= ' (' . $info['memory_type'] . ')'; |
| 81 |
} |
| 82 |
$result .= ' ' . phpversion( 'apcu' ); |
| 83 |
} |
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Delete cached objects. |
| 89 |
* |
| 90 |
* @param array $objects List of objects to delete. |
| 91 |
* @return integer The number of deleted objects. |
| 92 |
* @since 1.0.0 |
| 93 |
*/ |
| 94 |
public static function delete( $objects ) { |
| 95 |
$cpt = 0; |
| 96 |
if ( function_exists( 'apcu_delete' ) ) { |
| 97 |
$prefix = md5( ABSPATH ) . '_'; |
| 98 |
foreach ( $objects as $object ) { |
| 99 |
if ( false !== apcu_delete( $prefix . $object ) ) { |
| 100 |
$cpt++; |
| 101 |
} |
| 102 |
} |
| 103 |
\DecaLog\Engine::eventsLogger( OPCM_SLUG )->info( sprintf( '%d object(s) deleted.', $cpt ) ); |
| 104 |
} |
| 105 |
return $cpt; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Clear the cache. |
| 110 |
* |
| 111 |
* @since 1.0.0 |
| 112 |
*/ |
| 113 |
public static function reset() { |
| 114 |
if ( function_exists( 'apcu_cache_info' ) && function_exists( 'apcu_delete' ) ) { |
| 115 |
$prefix = md5( ABSPATH ) . '_'; |
| 116 |
$infos = apcu_cache_info( false ); |
| 117 |
if ( array_key_exists( 'cache_list', $infos ) && is_array( $infos['cache_list'] ) ) { |
| 118 |
foreach ( $infos['cache_list'] as $script ) { |
| 119 |
if ( 0 === strpos( $script['info'], $prefix ) ) { |
| 120 |
apcu_delete( $script['info'] ); |
| 121 |
$result++; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
\DecaLog\Engine::eventsLogger( OPCM_SLUG )->notice( 'Cache cleared.' ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|