Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic built-in cache #32

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php": ">=5.3.3",
"ext-curl": "*",
"ext-json": "*",
"guzzle/guzzle": "~3.7"
"guzzle/guzzle": "~3.7",
"guzzle/plugin-cache": "v3.9.0"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
Expand Down
23 changes: 18 additions & 5 deletions src/Prismic/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace Prismic;

use Guzzle\Http\Client;
use Prismic\FieldForm;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\DefaultCacheStorage;

class Api
{
Expand Down Expand Up @@ -142,15 +143,27 @@ public function getData()
/**
* This method is static to respect others API
*
* @param string $action
* @param string $accessToken
* @param string $action the URL endpoint of the repository's API
* @param string $accessToken an optional permanent access token (leave null is you're not sure what you're doing)
* @param string $client the client being used, or if null, will used a default client
* @param bool|Guzzle\Cache\CacheAdapterInterface $cache the cache adapter being used, which must implement CacheAdapterInterface; set to true (default value) if you want the out-of-the box cache (a LRU cache of 100 entries); set to false if you want to disable the cache. If you want an LRU of 1000 entries, for instance, you can pass new LruCacheAdapter(1000).
*
* @return Api
*/
public static function get($action, $accessToken = null, $client = null)
public static function get($action, $accessToken = null, $client = null, $cache = true)
{
$url = $action . ($accessToken ? '?access_token=' . $accessToken : '');
$client = isset($client) ? $client : self::defaultClient();

// Adding the cache in case it's provided
if (is_bool($cache)) {
if ($cache) {
$client->addSubscriber( new CachePlugin(array('storage' => new DefaultCacheStorage(new LruCacheAdapter(100)))) );
}
} else {
$client->addSubscriber( new CachePlugin(array('storage' => new DefaultCacheStorage($cache))) );
}

$request = $client->get($url);
$response = $request->send();

Expand Down Expand Up @@ -185,7 +198,7 @@ public static function defaultClient()
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
CURLOPT_USERAGENT => 'prismic-php-0.1',
CURLOPT_USERAGENT => 'prismic-php-kit',
CURLOPT_HTTPHEADER => array('Accept: application/json')
)
));
Expand Down
3 changes: 2 additions & 1 deletion src/Prismic/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function get($field)
* @param string $field
*
* @return bool
* */
* */
public function has($field)
{
return array_key_exists($field, $this->fragments);
Expand Down Expand Up @@ -180,6 +180,7 @@ public function getBoolean($field)
$fragment = $this->get($field);
if (isset($fragment) && $fragment instanceof Text) {
$value = strtolower($fragment->getValue());

return in_array(strtolower($fragment->getValue()), array(
'yes',
'true',
Expand Down
3 changes: 3 additions & 0 deletions src/Prismic/Fragment/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function asHtml($linkResolver = null)
"</section>";
}
}

return $string;
}

Expand All @@ -43,6 +44,7 @@ public function asText()
$string .= $subfragment->asText();
}
}

return $string;
}

Expand All @@ -60,6 +62,7 @@ public static function parseSubfragmentList($json)
$subfragments[$subfragment_name] = $subfragment;
}
}

return $subfragments;
}

Expand Down
1 change: 1 addition & 0 deletions src/Prismic/Fragment/ImageView.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function asHtml($linkResolver = null, $attributes = array())
$img->setAttribute($key, $value);
}
$doc->appendChild($img);

return trim($doc->saveHTML()); // trim removes trailing newline
}

Expand Down
4 changes: 2 additions & 2 deletions src/Prismic/Fragment/StructuredText.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ public static function asHtmlText($text, $spans, $linkResolver = null)
$attributes = array();
if ($span instanceof StrongSpan) {
$nodeName = 'strong';
} else if ($span instanceof EmSpan) {
} elseif ($span instanceof EmSpan) {
$nodeName = 'em';
} else if ($span instanceof HyperlinkSpan) {
} elseif ($span instanceof HyperlinkSpan) {
$nodeName = 'a';
if ($span->getLink() instanceof DocumentLink) {
$attributes['href'] = $linkResolver ? $linkResolver($span->getLink()) : '';
Expand Down
77 changes: 77 additions & 0 deletions src/Prismic/LruCacheAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Prismic PHP SDK
*
* Copyright 2013 Zengularity (http://www.zengularity.com).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Prismic;

use Guzzle\Cache\CacheAdapterInterface;

class LruCacheAdapter implements CacheAdapterInterface
{

protected $maxSize;
protected $cache;

public function __construct($maxSize)
{
$this->maxSize = $maxSize;
$this->cache = array();
}

public function contains($id, array $options = null)
{
// check if contained
return array_key_exists($id, $this->cache);
}

public function delete($id, array $options = null)
{
$existed = $this->contains($id);
unset($this->cache[$id]);

return $existed;
}

public function fetch($id, array $options = null)
{
if ($this->contains($id)) {
// fetch data
$data = $this->cache[$id];
// delete entry to put it back at the end of the array (and let it live longer)
unset($this->cache[$id]);
$this->cache[$id] = $data;
// return the data
return $data;
} else {
return null;
}
}

public function save($id, $data, $lifeTime = false, array $options = null)
{
// save value at the end of the array
$this->cache[$id] = $data;
// checks if size too big --> shift the array
if ($this->size() > $this->maxSize) {
array_shift($this->cache);
}
}

public function size()
{
return count($this->cache);
}

public function toArray()
{
return $this->cache;
}

}
50 changes: 50 additions & 0 deletions tests/Prismic/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Prismic\Test;

use Prismic\LruCacheAdapter;

class CacheTest extends \PHPUnit_Framework_TestCase
{
protected $cache;

protected function setUp()
{
$this->cache = new LruCacheAdapter(5);
}

public function testCheckInitializationIsRight()
{
$this->assertEquals(0, $this->cache->size());
}

public function testCheckAddingCacheEntrie()
{
$this->cache->save('key1', 'value1');
$this->cache->save('key2', 'value2');
$this->cache->save('key3', 'value3');
$this->cache->save('key4', 'value4');
$this->cache->save('key5', 'value5');
$this->cache->save('key6', 'value6');
$this->assertEquals(5, $this->cache->size());

// checking that key1 is the one that's gone
$this->assertTrue($this->cache->contains('key2') && $this->cache->contains('key3') && $this->cache->contains('key4') && $this->cache->contains('key5') && $this->cache->contains('key6') && !$this->cache->contains('key1'));
}

public function testFetchingAndReordering()
{
$this->cache->save('key1', 'value1');
$this->cache->save('key2', 'value2');
$this->cache->save('key3', 'value3');
$this->cache->save('key4', 'value4');
$this->assertEquals('value1', $this->cache->fetch('key1')); // fetching key1 now puts it back at the end of the array, so it doesn't get deleted too soon
$this->cache->save('key5', 'value5');
$this->cache->save('key6', 'value6');
$this->assertEquals(5, $this->cache->size());

// checking that key2 is the one that's gone
$this->assertTrue($this->cache->contains('key1') && $this->cache->contains('key3') && $this->cache->contains('key4') && $this->cache->contains('key5') && $this->cache->contains('key6') && !$this->cache->contains('key2'));
}

}