33
44namespace MySQLReplication \Cache ;
55
6- use MySQLReplication \ Config \ Config ;
6+ use Composer \ InstalledVersions ;
77use Psr \SimpleCache \CacheInterface ;
88
9- class ArrayCache implements CacheInterface
10- {
9+ if (class_exists (InstalledVersions::class) && version_compare (InstalledVersions::getVersion ("psr/simple-cache " ), '3.0.0 ' , ">= " )) {
10+ class ArrayCache implements CacheInterface
11+ {
12+ use ArrayCacheTrait;
13+
14+ /**
15+ * @inheritDoc
16+ */
17+ public function get (string $ key , $ default = null ): mixed
18+ {
19+ return $ this ->has ($ key ) ? $ this ->tableMapCache [$ key ] : $ default ;
20+ }
21+
22+ /**
23+ * @inheritDoc
24+ */
25+ public function getMultiple (iterable $ keys , $ default = null ): iterable
26+ {
27+ $ data = [];
28+ foreach ($ keys as $ key ) {
29+ if ($ this ->has ($ key )) {
30+ $ data [$ key ] = $ this ->tableMapCache [$ key ];
31+ }
32+ }
33+
34+ return [] !== $ data ? $ data : $ default ;
35+ }
36+ }
37+ } else {
38+ class ArrayCache implements CacheInterface
39+ {
40+ use ArrayCacheTrait;
41+
42+ /**
43+ * @inheritDoc
44+ */
45+ public function get ($ key , $ default = null )
46+ {
47+ return $ this ->has ($ key ) ? $ this ->tableMapCache [$ key ] : $ default ;
48+ }
49+
50+ /**
51+ * @inheritDoc
52+ */
53+ public function getMultiple ($ keys , $ default = null )
54+ {
55+ $ data = [];
56+ foreach ($ keys as $ key ) {
57+ if ($ this ->has ($ key )) {
58+ $ data [$ key ] = $ this ->tableMapCache [$ key ];
59+ }
60+ }
61+
62+ return [] !== $ data ? $ data : $ default ;
63+ }
64+ }
65+ }
66+
67+ trait ArrayCacheTrait {
1168 private $ tableMapCache = [];
1269
1370 /**
@@ -20,13 +77,6 @@ public function __construct(int $tableCacheSize = 128)
2077 $ this ->tableCacheSize = $ tableCacheSize ;
2178 }
2279
23- /**
24- * @inheritDoc
25- */
26- public function get ($ key , $ default = null )
27- {
28- return $ this ->has ($ key ) ? $ this ->tableMapCache [$ key ] : $ default ;
29- }
3080
3181 /**
3282 * @inheritDoc
@@ -46,21 +96,6 @@ public function clear(): bool
4696 return true ;
4797 }
4898
49- /**
50- * @inheritDoc
51- */
52- public function getMultiple ($ keys , $ default = null )
53- {
54- $ data = [];
55- foreach ($ keys as $ key ) {
56- if ($ this ->has ($ key )) {
57- $ data [$ key ] = $ this ->tableMapCache [$ key ];
58- }
59- }
60-
61- return [] !== $ data ? $ data : $ default ;
62- }
63-
6499 /**
65100 * @inheritDoc
66101 */
@@ -114,4 +149,4 @@ public function delete($key): bool
114149
115150 return true ;
116151 }
117- }
152+ }
0 commit comments