44namespace ScriptFUSION \Porter \Connector ;
55
66use Psr \Cache \CacheItemPoolInterface ;
7- use ScriptFUSION \Porter \Cache \CacheKeyGenerator ;
87use ScriptFUSION \Porter \Cache \InvalidCacheKeyException ;
9- use ScriptFUSION \Porter \Cache \JsonCacheKeyGenerator ;
108use ScriptFUSION \Porter \Cache \MemoryCache ;
119
1210/**
1614 */
1715class CachingConnector implements Connector, ConnectorWrapper
1816{
17+ public const RESERVED_CHARACTERS = '{}()/\@: ' ;
18+
1919 /**
2020 * @var Connector
2121 */
@@ -26,19 +26,12 @@ class CachingConnector implements Connector, ConnectorWrapper
2626 */
2727 private $ cache ;
2828
29- /**
30- * @var CacheKeyGenerator
31- */
32- private $ cacheKeyGenerator ;
33-
3429 public function __construct (
3530 Connector $ connector ,
36- CacheItemPoolInterface $ cache = null ,
37- CacheKeyGenerator $ cacheKeyGenerator = null
31+ CacheItemPoolInterface $ cache = null
3832 ) {
3933 $ this ->connector = $ connector ;
4034 $ this ->cache = $ cache ?: new MemoryCache ;
41- $ this ->cacheKeyGenerator = $ cacheKeyGenerator ?: new JsonCacheKeyGenerator ;
4235 }
4336
4437 public function __clone ()
@@ -50,18 +43,11 @@ public function __clone()
5043 }
5144
5245 /**
53- * @param string $source
54- *
55- * @return mixed
56- *
5746 * @throws InvalidCacheKeyException Cache key contains invalid data.
5847 */
59- public function fetch (string $ source )
48+ public function fetch (DataSource $ source )
6049 {
61- $ options = $ this ->connector instanceof ConnectorOptions ? $ this ->connector ->getOptions ()->copy () : [];
62- ksort ($ options );
63-
64- $ this ->validateCacheKey ($ key = $ this ->cacheKeyGenerator ->generateCacheKey ($ source , $ options ));
50+ $ this ->validateCacheKey ($ key = $ source ->computeHash ());
6551
6652 if ($ this ->cache ->hasItem ($ key )) {
6753 return $ this ->cache ->getItem ($ key )->get ();
@@ -79,11 +65,11 @@ public function fetch(string $source)
7965 */
8066 private function validateCacheKey (string $ key ): void
8167 {
82- if (strpbrk ($ key , CacheKeyGenerator ::RESERVED_CHARACTERS ) !== false ) {
68+ if (strpbrk ($ key , self ::RESERVED_CHARACTERS ) !== false ) {
8369 throw new InvalidCacheKeyException (sprintf (
8470 'Cache key "%s" contains one or more reserved characters: "%s". ' ,
8571 $ key ,
86- CacheKeyGenerator ::RESERVED_CHARACTERS
72+ self ::RESERVED_CHARACTERS
8773 ));
8874 }
8975 }
0 commit comments