PHP code example of knplabs / knp-zend-cache-bundle

1. Go to this page and download the library: Download knplabs/knp-zend-cache-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

knplabs / knp-zend-cache-bundle example snippets



// app/autoload.php
$loader->registerNamespaces(array(
    'Knp'                       => __DIR__.'/../vendor/bundles',
    'Zend'                      => __DIR__.'/../vendor',
    // ...
));


// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\ZendCacheBundle\KnpZendCacheBundle(),
        // ...
    );
}



// Given you have access to the container $container
$cache = $container->get('knp_zend_cache.manager')->getCache('tweets_and_stuff');

// Or if you are in a controller:
$cache = $this->get('knp_zend_cache.manager')->getCache('tweets_and_stuff');

// see if a cache already exists:
if (false === ($myLastTweets = $cache->load('last_tweets'))) {

    // cache miss: call the webservice and do stuff
    $myLastTweets = $tweetService->doComplexStuff();

    $cache->save($myLastTweets, 'last_tweets');
}

return $myLastTweets;