Skip to content

Commit c6efce1

Browse files
committed
Rename DefaultCache to ApcCache, and fallback to noop when apc is missing
1 parent ca6694b commit c6efce1

File tree

8 files changed

+29
-12
lines changed

8 files changed

+29
-12
lines changed

src/Prismic/Api.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
use Ivory\HttpAdapter\Event\Subscriber\StatusCodeSubscriber;
1616
use Ivory\HttpAdapter\HttpAdapterInterface;
1717
use \Prismic\Cache\CacheInterface;
18-
use \Prismic\Cache\DefaultCache;
18+
use \Prismic\Cache\ApcCache;
19+
use \Prismic\Cache\NoCache;
1920

2021
const PREVIEW_COOKIE = "io.prismic.preview";
2122

@@ -63,7 +64,7 @@ private function __construct($data, $accessToken = null, HttpAdapterInterface $h
6364
$this->data = $data;
6465
$this->accessToken = $accessToken;
6566
$this->httpAdapter = is_null($httpAdapter) ? self::defaultHttpAdapter() : $httpAdapter;
66-
$this->cache = is_null($cache) ? new DefaultCache() : $cache;
67+
$this->cache = is_null($cache) ? self::defaultCache() : $cache;
6768
}
6869

6970
/**
@@ -291,7 +292,7 @@ public function getHttpAdapter()
291292
*/
292293
public static function get($action, $accessToken = null, HttpAdapterInterface $httpAdapter = null, CacheInterface $cache = null)
293294
{
294-
$cache = is_null($cache) ? new DefaultCache() : $cache;
295+
$cache = is_null($cache) ? self::defaultCache() : $cache;
295296
$cacheKey = $action . (is_null($accessToken) ? "" : ("#" . $accessToken));
296297
$apiData = $cache->get($cacheKey);
297298
$api = $apiData ? new Api(unserialize($apiData), $accessToken, $httpAdapter, $cache) : null;
@@ -333,6 +334,17 @@ function ($ref) {
333334
}
334335
}
335336

337+
/**
338+
* Use the APC cache if APC is activated on the server, otherwise fallback to the noop cache (no cache)
339+
*/
340+
public static function defaultCache()
341+
{
342+
if (extension_loaded('apc') && ini_get('apc.enabled')) {
343+
return new ApcCache();
344+
}
345+
return new NoCache();
346+
}
347+
336348
/**
337349
* The default configuration of the HTTP adapter used in the kit; this is entirely overridable by passing
338350
*/

src/Prismic/Cache/DefaultCache.php renamed to src/Prismic/Cache/ApcCache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* The default implementation that is passed in the Api object when created:
1515
* it is based on APC, and therefore requires APC to be installed on the server.
1616
*/
17-
class DefaultCache implements CacheInterface
17+
class ApcCache implements CacheInterface
1818
{
1919
/**
2020
* Tests whether the cache has a value for a particular key

tests/Prismic/ApiTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
namespace Prismic\Test;
44

55
use Prismic\Api;
6+
use Prismic\Cache\ApcCache;
67

78
class ApiTest extends \PHPUnit_Framework_TestCase
89
{
910

1011
protected function setUp()
1112
{
12-
$cache = new \Prismic\Cache\DefaultCache();
13+
$cache = new ApcCache();
1314
$cache->clear();
1415
}
1516

tests/Prismic/CacheTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Prismic\Test;
44

5+
use Prismic\Cache\ApcCache;
6+
57
class CacheTest extends \PHPUnit_Framework_TestCase
68
{
79
private $cache;
810

911
protected function setUp()
1012
{
11-
$this->cache = new \Prismic\Cache\DefaultCache();
13+
$this->cache = new ApcCache();
1214
}
1315

1416
public function testSetGetValue()

tests/Prismic/DocTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DateTime;
66
use Ivory\HttpAdapter\HttpAdapterException;
77
use Prismic\Api;
8-
use Prismic\Cache\DefaultCache;
8+
use Prismic\Cache\ApcCache;
99
use Prismic\Document;
1010
use Prismic\Predicates;
1111

@@ -337,7 +337,7 @@ public function testCache() {
337337
// startgist:e0098caad8be5db00db7:prismic-cache.php
338338
// You can pass any class implementing the CacheInterface to the Api creation
339339
// http://prismicio.github.io/php-kit/classes/Prismic.Cache.CacheInterface.html
340-
$fileCache = new DefaultCache();
340+
$fileCache = new ApcCache();
341341
$api = Api::get("https://lesbonneschoses.cdn.prismic.io/api", null /* token */, null /* client */, $fileCache);
342342
// endgist
343343
$this->assertNotNull($api);

tests/Prismic/DocumentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Prismic\Test;
44

5-
use Prismic\Cache\DefaultCache;
5+
use Prismic\Cache\ApcCache;
66
use Prismic\Document;
77
use Prismic\Api;
88

@@ -16,7 +16,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase
1616

1717
protected function setUp()
1818
{
19-
$cache = new DefaultCache();
19+
$cache = new ApcCache();
2020
$cache->clear();
2121
$search = json_decode(file_get_contents(__DIR__.'/../fixtures/search.json'));
2222
$this->document = Document::parse($search[0]);

tests/Prismic/LesBonnesChosesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Prismic\Test;
44

55
use Prismic\Api;
6+
use Prismic\Cache\ApcCache;
67
use Prismic\Response;
78
use Prismic\Predicates;
89

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

1516
protected function setUp()
1617
{
17-
$cache = new \Prismic\Cache\DefaultCache();
18+
$cache = new ApcCache();
1819
$cache->clear();
1920
}
2021

tests/Prismic/LinkResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Prismic\Test;
44

55
use Prismic\API;
6+
use Prismic\Cache\ApcCache;
67
use Prismic\Document;
78
use Prismic\Fragment\Link\DocumentLink;
89

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

1314
protected function setUp()
1415
{
15-
$cache = new \Prismic\Cache\DefaultCache();
16+
$cache = new ApcCache();
1617
$cache->clear();
1718
$this->linkResolver = new FakeLinkResolver();
1819
$this->id = 'Ue0EDd_mqb8Dhk3j';

0 commit comments

Comments
 (0)