Skip to content

Instantly share code, notes, and snippets.

@entr
Forked from markjaquith/gist:2653957
Last active October 28, 2015 09:39
Show Gist options
  • Save entr/4e33f9a81ee3e90c079d to your computer and use it in GitHub Desktop.
Save entr/4e33f9a81ee3e90c079d to your computer and use it in GitHub Desktop.

Revisions

  1. Zlatko Zlatev revised this gist Oct 28, 2015. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    {
    "name": "markjaquith/cws-fragment-cache",
    "description": "WordPress Fragment Caching convenience wrapper",
    "require": {
    "johnpbloch/wordpress": ">=3.0.0"
    },
    "license": "GPL-2.0+",
    "authors": [
    {
    "name": "Mark Jaquith",
    "email": "[email protected]"
    },
    {
    "name": "WPTailor",
    "email": "[email protected]"
    }
    ],
    "minimum-stability": "dev",
    "autoload": {
    "files": ["class-cws-fragment-cache.php"]
    }
    }
  2. Zlatko Zlatev revised this gist Oct 28, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions class-cws-fragment-cache.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,12 @@
    <?php
    /**
    * WordPress Fragment Caching convenience wrapper.
    *
    * @author Mark Jaquith, packed by WPTailor
    */

    defined('ABSPATH') or exit;

    /*
    Usage:
    $frag = new CWS_Fragment_Cache( 'unique-key', 3600 ); // Second param is TTL
  3. Zlatko Zlatev renamed this gist Oct 28, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @markjaquith markjaquith renamed this gist May 10, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @markjaquith markjaquith created this gist May 10, 2012.
    40 changes: 40 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <?php
    /*
    Usage:
    $frag = new CWS_Fragment_Cache( 'unique-key', 3600 ); // Second param is TTL
    if ( !$frag->output() ) { // NOTE, testing for a return of false
    functions_that_do_stuff_live();
    these_should_echo();
    // IMPORTANT
    $frag->store();
    // YOU CANNOT FORGET THIS. If you do, the site will break.
    }
    */

    class CWS_Fragment_Cache {
    const GROUP = 'cws-fragments';
    var $key;
    var $ttl;

    public function __construct( $key, $ttl ) {
    $this->key = $key;
    $this->ttl = $ttl;
    }

    public function output() {
    $output = wp_cache_get( $this->key, self::GROUP );
    if ( !empty( $output ) ) {
    // It was in the cache
    echo $output;
    return true;
    } else {
    ob_start();
    return false;
    }
    }

    public function store() {
    $output = ob_get_flush(); // Flushes the buffers
    wp_cache_add( $this->key, $output, self::GROUP, $this->ttl );
    }
    }