22
33namespace 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 */
1012class 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 *
0 commit comments