3
3
4
4
namespace MySQLReplication \Cache ;
5
5
6
- use MySQLReplication \ Config \ Config ;
6
+ use Composer \ InstalledVersions ;
7
7
use Psr \SimpleCache \CacheInterface ;
8
8
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 {
11
68
private $ tableMapCache = [];
12
69
13
70
/**
@@ -20,13 +77,6 @@ public function __construct(int $tableCacheSize = 128)
20
77
$ this ->tableCacheSize = $ tableCacheSize ;
21
78
}
22
79
23
- /**
24
- * @inheritDoc
25
- */
26
- public function get ($ key , $ default = null )
27
- {
28
- return $ this ->has ($ key ) ? $ this ->tableMapCache [$ key ] : $ default ;
29
- }
30
80
31
81
/**
32
82
* @inheritDoc
@@ -46,21 +96,6 @@ public function clear(): bool
46
96
return true ;
47
97
}
48
98
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
-
64
99
/**
65
100
* @inheritDoc
66
101
*/
@@ -114,4 +149,4 @@ public function delete($key): bool
114
149
115
150
return true ;
116
151
}
117
- }
152
+ }
0 commit comments