Skip to content

Commit

Permalink
Rename DefaultCache to ApcCache, and fallback to noop when apc is mis…
Browse files Browse the repository at this point in the history
…sing
  • Loading branch information
erwan committed Feb 2, 2015
1 parent ca6694b commit c6efce1
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
18 changes: 15 additions & 3 deletions src/Prismic/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Ivory\HttpAdapter\Event\Subscriber\StatusCodeSubscriber;
use Ivory\HttpAdapter\HttpAdapterInterface;
use \Prismic\Cache\CacheInterface;
use \Prismic\Cache\DefaultCache;
use \Prismic\Cache\ApcCache;
use \Prismic\Cache\NoCache;

const PREVIEW_COOKIE = "io.prismic.preview";

Expand Down Expand Up @@ -63,7 +64,7 @@ private function __construct($data, $accessToken = null, HttpAdapterInterface $h
$this->data = $data;
$this->accessToken = $accessToken;
$this->httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter;
$this->cache = is_null($cache) ? new DefaultCache() : $cache;
$this->cache = is_null($cache) ? self::defaultCache() : $cache;
}

/**
Expand Down Expand Up @@ -291,7 +292,7 @@ public function getHttpAdapter()
*/
public static function get($action, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null)
{
$cache = is_null($cache) ? new DefaultCache() : $cache;
$cache = is_null($cache) ? self::defaultCache() : $cache;
$cacheKey = $action . (is_null($accessToken) ? "" : ("#" . $accessToken));
$apiData = $cache->get($cacheKey);
$api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpAdapter, $cache) : null;
Expand Down Expand Up @@ -333,6 +334,17 @@ function ($ref) {
}
}

/**
* Use the APC cache if APC is activated on the server, otherwise fallback to the noop cache (no cache)
*/
public static function defaultCache()
{
if (extension_loaded('apc') && ini_get('apc.enabled')) {
return new ApcCache();
}
return new NoCache();
}

/**
* The default configuration of the HTTP adapter used in the kit; this is entirely overridable by passing
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* The default implementation that is passed in the Api object when created:
* it is based on APC, and therefore requires APC to be installed on the server.
*/
class DefaultCache implements CacheInterface
class ApcCache implements CacheInterface
{
/**
* Tests whether the cache has a value for a particular key
Expand Down
3 changes: 2 additions & 1 deletion tests/Prismic/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Prismic\Test;

use Prismic\Api;
use Prismic\Cache\ApcCache;

class ApiTest extends \PHPUnit_Framework_TestCase
{

protected function setUp()
{
$cache = new \Prismic\Cache\DefaultCache();
$cache = new ApcCache();
$cache->clear();
}

Expand Down
4 changes: 3 additions & 1 deletion tests/Prismic/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Prismic\Test;

use Prismic\Cache\ApcCache;

class CacheTest extends \PHPUnit_Framework_TestCase
{
private $cache;

protected function setUp()
{
$this->cache = new \Prismic\Cache\DefaultCache();
$this->cache = new ApcCache();
}

public function testSetGetValue()
Expand Down
4 changes: 2 additions & 2 deletions tests/Prismic/DocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DateTime;
use Ivory\HttpAdapter\HttpAdapterException;
use Prismic\Api;
use Prismic\Cache\DefaultCache;
use Prismic\Cache\ApcCache;
use Prismic\Document;
use Prismic\Predicates;

Expand Down Expand Up @@ -337,7 +337,7 @@ public function testCache() {
// startgist:e0098caad8be5db00db7:prismic-cache.php
// You can pass any class implementing the CacheInterface to the Api creation
// http://prismicio.github.io/php-kit/classes/Prismic.Cache.CacheInterface.html
$fileCache = new DefaultCache();
$fileCache = new ApcCache();
$api = Api::get("https://lesbonneschoses.cdn.prismic.io/api", null /* token */, null /* client */, $fileCache);
// endgist
$this->assertNotNull($api);
Expand Down
4 changes: 2 additions & 2 deletions tests/Prismic/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Prismic\Test;

use Prismic\Cache\DefaultCache;
use Prismic\Cache\ApcCache;
use Prismic\Document;
use Prismic\Api;

Expand All @@ -16,7 +16,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$cache = new DefaultCache();
$cache = new ApcCache();
$cache->clear();
$search = json_decode(file_get_contents(__DIR__.'/../fixtures/search.json'));
$this->document = Document::parse($search[0]);
Expand Down
3 changes: 2 additions & 1 deletion tests/Prismic/LesBonnesChosesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Prismic\Test;

use Prismic\Api;
use Prismic\Cache\ApcCache;
use Prismic\Response;
use Prismic\Predicates;

Expand All @@ -14,7 +15,7 @@ class LesBonnesChosesTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$cache = new \Prismic\Cache\DefaultCache();
$cache = new ApcCache();
$cache->clear();
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Prismic/LinkResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Prismic\Test;

use Prismic\API;
use Prismic\Cache\ApcCache;
use Prismic\Document;
use Prismic\Fragment\Link\DocumentLink;

Expand All @@ -12,7 +13,7 @@ class LinkResolverTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$cache = new \Prismic\Cache\DefaultCache();
$cache = new ApcCache();
$cache->clear();
$this->linkResolver = new FakeLinkResolver();
$this->id = 'Ue0EDd_mqb8Dhk3j';
Expand Down

0 comments on commit c6efce1

Please sign in to comment.