@@ -24,6 +24,13 @@ class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface
24
24
{
25
25
private $ propertyInfoExtractor ;
26
26
private $ cacheItemPool ;
27
+
28
+ /**
29
+ * A cache of property information, first keyed by the method called and
30
+ * then by the serialized method arguments.
31
+ *
32
+ * @var array
33
+ */
27
34
private $ arrayCache = [];
28
35
29
36
public function __construct (PropertyInfoExtractorInterface $ propertyInfoExtractor , CacheItemPoolInterface $ cacheItemPool )
@@ -98,22 +105,34 @@ private function extract($method, array $arguments)
98
105
}
99
106
100
107
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys
101
- $ key = rawurlencode ($ method .'. ' .$ serializedArguments );
102
-
103
- if (\array_key_exists ($ key , $ this ->arrayCache )) {
104
- return $ this ->arrayCache [$ key ];
108
+ $ encodedMethod = \rawurlencode ($ method );
109
+ if (\array_key_exists ($ encodedMethod , $ this ->arrayCache ) && \array_key_exists ($ serializedArguments , $ this ->arrayCache [$ encodedMethod ])) {
110
+ return $ this ->arrayCache [$ encodedMethod ][$ serializedArguments ];
105
111
}
106
112
107
- $ item = $ this ->cacheItemPool ->getItem ($ key );
113
+ $ item = $ this ->cacheItemPool ->getItem ($ encodedMethod );
108
114
115
+ $ data = $ item ->get ();
109
116
if ($ item ->isHit ()) {
110
- return $ this ->arrayCache [$ key ] = $ item ->get ();
117
+ $ this ->arrayCache [$ encodedMethod ] = $ data [$ encodedMethod ];
118
+ // Only match if the specific arguments have been cached.
119
+ if (\array_key_exists ($ serializedArguments , $ data [$ encodedMethod ])) {
120
+ return $ this ->arrayCache [$ encodedMethod ][$ serializedArguments ];
121
+ }
122
+ }
123
+
124
+ // It's possible that the method has been called, but with different
125
+ // arguments, in which case $data will already be initialized.
126
+ if (!$ data ) {
127
+ $ data = [];
111
128
}
112
129
113
130
$ value = \call_user_func_array ([$ this ->propertyInfoExtractor , $ method ], $ arguments );
114
- $ item ->set ($ value );
131
+ $ data [$ encodedMethod ][$ serializedArguments ] = $ value ;
132
+ $ this ->arrayCache [$ encodedMethod ][$ serializedArguments ] = $ value ;
133
+ $ item ->set ($ data );
115
134
$ this ->cacheItemPool ->save ($ item );
116
135
117
- return $ this ->arrayCache [$ key ] = $ value ;
136
+ return $ this ->arrayCache [$ encodedMethod ][ $ serializedArguments ] ;
118
137
}
119
138
}
0 commit comments