Title: Optimize output buffer handler
Last modified: April 8, 2021

---

# Optimize output buffer handler

 *  [Nextendweb](https://wordpress.org/support/users/nextendweb/)
 * (@nextendweb)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/optimize-output-buffer-handler/)
 * Hi,
    In the class named `Cachify`, 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: [http://sandbox.onlinephpfunctions.com/code/4dc11a69b0da536768f211199dd8c31d1ca50609](http://sandbox.onlinephpfunctions.com/code/4dc11a69b0da536768f211199dd8c31d1ca50609)

The topic ‘Optimize output buffer handler’ is closed to new replies.

 * ![](https://ps.w.org/cachify/assets/icon-256x256.png?rev=2505413)
 * [Cachify](https://wordpress.org/plugins/cachify/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/cachify/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/cachify/)
 * [Active Topics](https://wordpress.org/support/plugin/cachify/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/cachify/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/cachify/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [Nextendweb](https://wordpress.org/support/users/nextendweb/)
 * Last activity: [5 years, 1 month ago](https://wordpress.org/support/topic/optimize-output-buffer-handler/)
 * Status: not resolved