예제 #1
0
echo "Programmes Index" . PHP_EOL;
echo '----------------' . PHP_EOL;
// Get the index of programmes
$programmes = $pp->get_programmes_index('2014', 'ug');
echo 'In total there are ' . count((array) $programmes) . ' programmes' . PHP_EOL;
echo PHP_EOL . "Single Programme" . PHP_EOL;
echo '----------------' . PHP_EOL;
// Get a single programme
$programme = $pp->get_programme('2014', 'ug', 1);
echo 'Got ' . $programme->programme_title . PHP_EOL;
unset($programme, $pp);
/**
 * Caching functionality.
 */
echo PHP_EOL . "Disk cache" . PHP_EOL;
echo '------------' . PHP_EOL;
$pp = new ProgrammesPlant\API($api_url);
$pp->with_cache('file')->directory('/tmp/cache');
// Get a single programme.
$programme = $pp->get_programme('2014', 'ug', 1);
echo 'Managed to get ' . $programme->programme_title . PHP_EOL;
// We should now be able to get this direct from the cache without making a request.
use Guzzle\Http\Message\Request;
// Work out where it is stored in the cache.
$request = new Request('GET', $api_url . '2014/ug/programmes/1');
$key_provider = new \Guzzle\Plugin\Cache\DefaultCacheKeyProvider();
$key = $key_provider->getCacheKey($request);
$data = $pp->cache_object->fetch($key);
if (json_decode($data[2]) == $programme) {
    echo 'Cache is working! Got ' . $programme->programme_title . ' from disk!' . PHP_EOL;
}
예제 #2
0
 /**
  * Serve the response from cache.
  * 
  * @return bool|array  False if we haven't got this in the cache, the cache array if not.
  */
 public function serve_from_cache()
 {
     // Work out how this would be cached.
     $key_provider = new \Guzzle\Plugin\Cache\DefaultCacheKeyProvider();
     $cache_key = $key_provider->getCacheKey($this->request);
     // Attempt to get cache object.
     $cached = $this->cache_object->fetch($cache_key);
     return $cached;
 }
 public function testEnsureNonJsonIsntCached()
 {
     $pp = new PP('http://example.com');
     $pp->with_cache('file')->directory(static::$cache_directory);
     $pp->prepare();
     // Simulate the events for the cache object.
     $request = new Request('GET', 'http://foo.com');
     $response = new Response(200, array(), 'Foo');
     // Fake the request.
     $pp->cache_plugin->onRequestBeforeSend(new Event(array('request' => $request)));
     // Fake the response to that request.
     $pp->cache_plugin->onRequestSent(new Event(array('request' => $request, 'response' => $response)));
     $key_provider = new \Guzzle\Plugin\Cache\DefaultCacheKeyProvider();
     $data = $pp->cache_object->fetch($key_provider->getCacheKey($request));
     $this->assertEmpty($data, "Non json responses should not be cached.");
 }