Skip to content

Instantly share code, notes, and snippets.

@MarcoMiltenburg
Forked from Geolim4/Caching Whole Webpage.md
Last active December 23, 2016 23:24
Show Gist options
  • Save MarcoMiltenburg/e0a1f32e85d7808bbe6c26b10c03dfea to your computer and use it in GitHub Desktop.
Save MarcoMiltenburg/e0a1f32e85d7808bbe6c26b10c03dfea to your computer and use it in GitHub Desktop.
    use phpFastCache\CacheManager;

    $cache = CacheManager::Memcached();

    $keyword_webpage = md5($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
    // try to get from Cache first.
    $resultsItem = $cache->getItem($keyword_webpage)

    if(!$resultsItem->isHit()) {
        ob_start();
        /*
            ALL OF YOUR CODE GO HERE
            RENDER YOUR PAGE, DB QUERY, WHATEVER
        */

        // GET HTML WEBPAGE
        $html = ob_get_contents();

        $resultsItem->set($html)->expiresAfter(1800);
        $cache->save($resultsItem);
    }

    echo $resultsItem->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment