Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Commit 7805963

Browse files
authored
Merge pull request #68 from roberto-butti/feature/60-replace-apixcache-symfonycache
Feature/60 replace apixcache symfonycache
2 parents ca79980 + 243dc50 commit 7805963

File tree

11 files changed

+8969
-39
lines changed

11 files changed

+8969
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/vendor/
22
/tests/cache/*
3+
/cache/*
34
.git
45
.phpunit.result.cache
56
.php-cs-fixer.cache

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
$finder = PhpCsFixer\Finder::create()
44
->in([
55
__DIR__ . '/src',
6+
__DIR__ . '/tests',
67
])
78
->name('*.php')
89
->ignoreDotFiles(true)

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
"require": {
99
"php": ">=7.3.0",
1010
"guzzlehttp/guzzle": "^7.4",
11-
"apix/cache": "^1.3",
11+
"symfony/cache": "^4.0|^5.0|^6.0",
1212
"ext-json": "*"
1313
},
1414
"require-dev": {
1515
"friendsofphp/php-cs-fixer": "^3.4.0",
1616
"pestphp/pest": "1.21.2",
1717
"phpstan/phpstan": "1.9.x-dev"
1818
},
19+
"provide": {
20+
"psr/simple-cache-implementation": "1.0|2.0|3.0"
21+
},
1922
"authors": [
2023
{
2124
"name": "Alexander Feiglstorfer",

src/Storyblok/Client.php

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace Storyblok;
44

5-
use Apix\Cache as ApixCache;
5+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
6+
use Symfony\Component\Cache\Adapter\PdoAdapter;
7+
use Symfony\Component\Cache as SymfonyCache;
68

79
/**
810
* Storyblok Client.
911
*/
1012
class Client extends BaseClient
1113
{
12-
const CACHE_VERSION_KEY = 'storyblok:cv';
14+
const CACHE_VERSION_KEY = 'storyblok_cv';
1315
const EXCEPTION_GENERIC_HTTP_ERROR = 'An HTTP Error has occurred!';
1416

1517
/**
@@ -18,7 +20,7 @@ class Client extends BaseClient
1820
public $cacheVersion;
1921

2022
/**
21-
* @var Cache
23+
* @var SymfonyCache
2224
*/
2325
protected $cache;
2426

@@ -196,40 +198,60 @@ public function getApiParameters()
196198
public function setCache($driver, $options = [])
197199
{
198200
$options['serializer'] = 'php';
199-
$options['prefix_key'] = 'storyblok:';
200-
$options['prefix_tag'] = 'storyblok:';
201+
$options['prefix_key'] = 'storyblokcache';
202+
$options['prefix_tag'] = 'storyblokcachetag';
201203

202204
switch ($driver) {
203205
case 'mysql':
204206
$dbh = $options['pdo'];
205-
$this->cache = new ApixCache\Pdo\Mysql($dbh, $options);
207+
$this->cache = new PdoAdapter(
208+
$dbh,
209+
$options['prefix_key'],
210+
0,
211+
$options
212+
);
206213

207214
break;
208215

209216
case 'sqlite':
210217
$dbh = $options['pdo'];
211-
$this->cache = new ApixCache\Pdo\Sqlite($dbh, $options);
218+
$this->cache = new PdoAdapter(
219+
$dbh,
220+
$options['prefix_key'],
221+
0,
222+
$options
223+
);
212224

213225
break;
214226

215227
case 'postgres':
216228
$dbh = $options['pdo'];
217-
$this->cache = new ApixCache\Pdo\Pgsql($dbh, $options);
229+
$this->cache = new PdoAdapter(
230+
$dbh,
231+
$options['prefix_key'],
232+
0,
233+
$options
234+
);
218235

219236
break;
220237

221238
default:
222239
$options['directory'] = $options['path'];
223240

224-
$this->cache = new ApixCache\Files($options);
241+
$this->cache = new FilesystemAdapter(
242+
$options['prefix_key'],
243+
0,
244+
$options['directory']
245+
);
225246

226247
break;
227248
}
228249

229-
$this->cv = $this->cache->load(self::CACHE_VERSION_KEY);
250+
$this->cv = $this->cacheGet(self::CACHE_VERSION_KEY);
230251

231252
if (!$this->cv) {
232-
$this->setCacheVersion();
253+
$this->cacheSave('', self::CACHE_VERSION_KEY);
254+
// $this->setCacheVersion();
233255
}
234256

235257
return $this;
@@ -245,12 +267,12 @@ public function setCache($driver, $options = [])
245267
public function deleteCacheBySlug($slug)
246268
{
247269
$key = $this->_getCacheKey('stories/' . $slug);
248-
270+
$linksCacheKey = $this->_getCacheKey($this->linksPath);
249271
if ($this->cache) {
250272
$this->cache->delete($key);
251273

252274
// Always refresh cache of links
253-
$this->cache->delete($this->linksPath);
275+
$this->cache->delete($linksCacheKey);
254276
$this->setCacheVersion();
255277
}
256278

@@ -265,7 +287,7 @@ public function deleteCacheBySlug($slug)
265287
public function flushCache()
266288
{
267289
if ($this->cache) {
268-
$this->cache->flush();
290+
$this->cache->clear();
269291
$this->setCacheVersion();
270292
}
271293

@@ -282,7 +304,7 @@ public function setCacheVersion()
282304
if ($this->cache) {
283305
$res = $this->getStories(['per_page' => 1, 'version' => 'published']);
284306
$this->cv = $res->responseBody['cv'];
285-
$this->cache->save($this->cv, self::CACHE_VERSION_KEY);
307+
$this->cacheSave($this->cv, self::CACHE_VERSION_KEY);
286308
}
287309

288310
return $this;
@@ -366,9 +388,9 @@ public function getStories($options = [])
366388
$key = 'stories/' . serialize($this->_prepareOptionsForKey($options));
367389
$cachekey = $this->_getCacheKey($key);
368390

369-
$this->reCacheOnPublish($key);
391+
$this->reCacheOnPublish($cachekey);
370392

371-
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
393+
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cachekey)) {
372394
$this->_assignState($cachedItem);
373395
} else {
374396
$options = array_merge($options, $this->getApiParameters());
@@ -452,7 +474,7 @@ public function getTags($options = [])
452474

453475
$this->reCacheOnPublish($key);
454476

455-
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
477+
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cachekey)) {
456478
$this->_assignState($cachedItem);
457479
} else {
458480
$options = array_merge($options, $this->getApiParameters());
@@ -482,7 +504,7 @@ public function getDatasourceEntries($slug, $options = [])
482504

483505
$this->reCacheOnPublish($key);
484506

485-
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
507+
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cachekey)) {
486508
$this->_assignState($cachedItem);
487509
} else {
488510
$options = array_merge(
@@ -513,16 +535,16 @@ public function getDatasourceEntries($slug, $options = [])
513535
public function getLinks($options = [])
514536
{
515537
$key = $this->linksPath;
516-
$cachekey = $this->_getCacheKey($key);
538+
$cacheKey = $this->_getCacheKey($key . serialize($this->_prepareOptionsForKey($options)));
517539

518-
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
540+
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cacheKey)) {
519541
$this->_assignState($cachedItem);
520542
} else {
521543
$options = array_merge($options, $this->getApiParameters());
522544

523545
$response = $this->get($key, $options);
524546

525-
$this->_save($response, $cachekey, $this->getVersion());
547+
$this->_save($response, $cacheKey, $this->getVersion());
526548
}
527549

528550
return $this;
@@ -594,6 +616,11 @@ public function getAsTree()
594616
return $this->_generateTree($tree, 0);
595617
}
596618

619+
public function cacheClear()
620+
{
621+
$this->cache->clear();
622+
}
623+
597624
/**
598625
* Gets a list of stories.
599626
*
@@ -609,8 +636,8 @@ private function getStory($slug, $byUuid = false)
609636
$key = 'stories/' . $slug;
610637
$cachekey = $this->_getCacheKey($key);
611638

612-
$this->reCacheOnPublish($key);
613-
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cache->load($cachekey)) {
639+
$this->reCacheOnPublish($cachekey);
640+
if ('published' === $this->getVersion() && $this->cache && $cachedItem = $this->cacheGet($cachekey)) {
614641
if ($this->cacheNotFound && 404 === $cachedItem->httpResponseCode) {
615642
throw new ApiException(self::EXCEPTION_GENERIC_HTTP_ERROR, 404);
616643
}
@@ -652,8 +679,7 @@ private function getStory($slug, $byUuid = false)
652679
$result->httpResponseBody = [];
653680
$result->httpResponseCode = 404;
654681
$result->httpResponseHeaders = [];
655-
656-
$this->cache->save($result, $cachekey);
682+
$this->cacheSave($result, $cachekey);
657683
}
658684

659685
throw new ApiException(self::EXCEPTION_GENERIC_HTTP_ERROR . ' - ' . $e->getMessage(), $e->getCode());
@@ -676,7 +702,8 @@ private function reCacheOnPublish($key)
676702
$this->cache->delete($key);
677703

678704
// Always refresh cache of links
679-
$this->cache->delete($this->linksPath);
705+
$linksCacheKey = $this->_getCacheKey($this->linksPath);
706+
$this->cache->delete($linksCacheKey);
680707
$this->setCacheVersion();
681708
}
682709

@@ -714,22 +741,42 @@ private function _generateTree($items, $parent = 0)
714741
/**
715742
* Save's the current response in the cache if version is published.
716743
*
717-
* @param array $response
718-
* @param string $key
719-
* @param string $version
744+
* @param array|\stdClass $response
745+
* @param string $key
746+
* @param string $version
720747
*/
721748
private function _save($response, $key, $version)
722749
{
723750
$this->_assignState($response);
724-
725751
if ($this->cache
726752
&& 'published' === $version
727753
&& $response->httpResponseHeaders
728754
&& 200 === $response->httpResponseCode) {
729-
$this->cache->save($response, $key);
755+
$this->cacheSave($response, $key);
730756
}
731757
}
732758

759+
private function cacheSave($value, string $key)
760+
{
761+
if ($this->cache) {
762+
$cacheItem = $this->cache->getItem($key);
763+
$cacheItem->set($value);
764+
765+
return $this->cache->save($cacheItem);
766+
}
767+
768+
return false;
769+
}
770+
771+
private function cacheGet(string $key)
772+
{
773+
if ($this->cache) {
774+
return $this->cache->getItem($key)->get();
775+
}
776+
777+
return false;
778+
}
779+
733780
/**
734781
* Assigns the httpResponseBody and httpResponseHeader to '$this';.
735782
*

tests/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,23 @@
5959

6060
echo '<h2> Check: $client->cacheVersion </h2>
6161
<pre>';
62-
var_dump($client->cacheVersion);
62+
var_dump($client->getCacheVersion());
6363
echo '</pre>
6464
<hr>
6565
<p> Wait 2 Seconds </p>';
6666

6767
sleep(2);
6868

6969
echo '<pre>';
70-
var_dump($client->cacheVersion);
70+
var_dump($client->getCacheVersion());
7171
echo '</pre>
7272
<hr>
7373
<h2> deleteCacheBySlug "home" | flushCache </h2>';
7474

7575
$client->deleteCacheBySlug('home');
7676

7777
echo '<pre>';
78-
var_dump($client->cacheVersion);
78+
var_dump($client->getCacheVersion());
7979
echo '</pre>';
8080

8181
$client->flushCache();

tests/Data/v1/tags.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"name": "spicy",
99
"taggings_count": 3
1010
}
11-
]
11+
],
12+
"cv": "123456789"
1213
}

tests/Data/v2/stories.json

Lines changed: 4411 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)