0, 'hits' => 0, 'invalidations' => 0, ]; static $cache = []; static $count = [ 'misses' => 0, 'hits' => 0, 'invalidations' => 0, 'by_fn' => [], 'by_args' => [], ]; $key = $fn_name; if ( null !== $args ) { $key = $fn_name . '_' . wp_json_encode( $args ); } // Get log counts if enabled. if ( $log_counts && 'get_cacheit_counts' === $fn_name ) { return $count; } // Set up counters. if ( ! isset( $count['by_fn'][ $fn_name ] ) ) { $count['by_fn'][ $fn_name ] = $default_counts; } // Set up counters. if ( ! isset( $count['by_args'][ $key ] ) ) { $count['by_args'][ $key ] = $default_counts; } // Check and clear cache. if ( null === $callback || '__return_null' === $callback ) { unset( $cache[ $key ] ); if ( $log_counts ) { ++$count['invalidations']; ++$count['by_fn'][ $fn_name ]['invalidations']; ++$count['by_args'][ $key ]['invalidations']; } return null; } if ( ! isset( $cache[ $key ] ) ) { if ( false !== $callback ) { $cache[ $key ] = $callback(); } if ( $log_counts ) { ++$count['misses']; ++$count['by_fn'][ $fn_name ]['misses']; ++$count['by_args'][ $key ]['misses']; } } elseif ( $log_counts ) { ++$count['hits']; ++$count['by_fn'][ $fn_name ]['hits']; ++$count['by_args'][ $key ]['hits']; } return $cache[ $key ] ?? null; } /** * Track cache function counts. */ if ( defined( 'PUM_CACHEIT_COUNTS' ) && PUM_CACHEIT_COUNTS ) { add_action( 'plugins_loaded', function () { if ( class_exists( 'QM_Collectors' ) ) { // Register collector \QM_Collectors::add( new \PopupMaker\Integration\QueryMonitor\Collector\CacheFunc() ); // Register output handler add_filter( 'qm/outputter/html', function ( $output ) { $collector = \QM_Collectors::get( 'cache-func' ); if ( $collector ) { $output['cache-func'] = new \PopupMaker\Integration\QueryMonitor\Output\Html\CacheFunc( $collector ); } return $output; } ); } } ); } $value_history = []; // Add value history tracking to the earliest hook add_action( 'plugins_loaded', function () { global $value_history; $value_history = []; }, -999999 ); /** * Get value history. * * @return array */ function get_value_history() { global $value_history; return $value_history; }