Optimize output buffer handler
-
Hi,
In the class namedCachify
, you use ob_start with custom output buffer handler:ob_start( 'Cachify::set_cache' );
I suggest you replace the
set_cache
method with this one:/** * Assign the cache * * @since 0.1 * @change 2.0 * * @param string $data Content of the page. * @param int $phase Bitmask of PHP_OUTPUT_HANDLER_* constants. * @return string Content of the page. */ public static function set_cache( $data, $phase ) { if ($phase & PHP_OUTPUT_HANDLER_FINAL || $phase & PHP_OUTPUT_HANDLER_END) { /* Empty? */ if (empty($data)) { return ''; } /** * Filters whether the buffered data should actually be cached * * @param bool $should_cache Whether the data should be cached. * @param string $data The actual data. * @param object $method Instance of the selected caching method. * @param string $cache_hash The cache hash. * @param int $cache_expires Cache validity period. * * @since 2.3 * */ $should_cache = apply_filters('cachify_store_item', true, $data, self::$method, self::_cache_hash(), self::_cache_expires()); /* Save? */ if ($should_cache) { call_user_func(array( self::$method, 'store_item', ), self::_cache_hash(), self::_minify_cache($data), self::_cache_expires(), self::_signature_details()); } } return $data; }
Checking the phase parameter helps to leverage processing as the output callback function called multiple times when
ob_flush()
involved.
Example:
https://sandbox.onlinephpfunctions.com/code/4dc11a69b0da536768f211199dd8c31d1ca50609
- The topic ‘Optimize output buffer handler’ is closed to new replies.