@@ -33,9 +33,18 @@ class Cache
33
33
NAMESPACES = 'namespaces ' ,
34
34
ALL = 'all ' ;
35
35
36
+ public const
37
+ EVENT_HIT = 'hit ' ,
38
+ EVENT_MISS = 'miss ' ,
39
+ EVENT_SAVE = 'save ' ,
40
+ EVENT_REMOVE = 'remove ' ;
41
+
36
42
/** @internal */
37
43
public const NAMESPACE_SEPARATOR = "\x00" ;
38
44
45
+ /** @var array */
46
+ public $ onEvent ;
47
+
39
48
/** @var Storage */
40
49
private $ storage ;
41
50
@@ -88,8 +97,9 @@ public function load($key, callable $generator = null)
88
97
{
89
98
$ storageKey = $ this ->generateKey ($ key );
90
99
$ data = $ this ->storage ->read ($ storageKey );
100
+ $ this ->onEvent ($ this , $ data === null ? self ::EVENT_MISS : self ::EVENT_HIT , $ key );
91
101
if ($ data === null && $ generator ) {
92
- $ this ->storage ->lock ($ storageKey );
102
+ $ this ->storage ->lock ($ storageKey );
93
103
try {
94
104
$ data = $ generator (...[&$ dependencies ]);
95
105
} catch (\Throwable $ e ) {
@@ -134,12 +144,14 @@ public function bulkLoad(array $keys, callable $generator = null): array
134
144
foreach ($ keys as $ i => $ key ) {
135
145
$ storageKey = $ storageKeys [$ i ];
136
146
if (isset ($ cacheData [$ storageKey ])) {
147
+ $ this ->onEvent ($ this , self ::EVENT_HIT , $ key );
137
148
$ result [$ key ] = $ cacheData [$ storageKey ];
138
149
} elseif ($ generator ) {
139
150
$ result [$ key ] = $ this ->load ($ key , function (&$ dependencies ) use ($ key , $ generator ) {
140
151
return $ generator (...[$ key , &$ dependencies ]);
141
152
});
142
153
} else {
154
+ $ this ->onEvent ($ this , self ::EVENT_MISS , $ key );
143
155
$ result [$ key ] = null ;
144
156
}
145
157
}
@@ -165,27 +177,30 @@ public function bulkLoad(array $keys, callable $generator = null): array
165
177
*/
166
178
public function save ($ key , $ data , array $ dependencies = null )
167
179
{
168
- $ key = $ this ->generateKey ($ key );
180
+ $ storageKey = $ this ->generateKey ($ key );
169
181
170
182
if ($ data instanceof \Closure) {
171
183
trigger_error (__METHOD__ . '() closure argument is deprecated. ' , E_USER_WARNING );
172
- $ this ->storage ->lock ($ key );
184
+ $ this ->storage ->lock ($ storageKey );
173
185
try {
174
186
$ data = $ data (...[&$ dependencies ]);
175
187
} catch (\Throwable $ e ) {
176
- $ this ->storage ->remove ($ key );
188
+ $ this ->storage ->remove ($ storageKey );
177
189
throw $ e ;
178
190
}
179
191
}
180
192
181
193
if ($ data === null ) {
182
- $ this ->storage ->remove ($ key );
194
+ $ this ->storage ->remove ($ storageKey );
195
+ $ this ->onEvent ($ this , self ::EVENT_REMOVE , $ key );
183
196
} else {
184
197
$ dependencies = $ this ->completeDependencies ($ dependencies );
185
198
if (isset ($ dependencies [self ::EXPIRATION ]) && $ dependencies [self ::EXPIRATION ] <= 0 ) {
186
- $ this ->storage ->remove ($ key );
199
+ $ this ->storage ->remove ($ storageKey );
200
+ $ this ->onEvent ($ this , self ::EVENT_REMOVE , $ key );
187
201
} else {
188
- $ this ->storage ->write ($ key , $ data , $ dependencies );
202
+ $ this ->storage ->write ($ storageKey , $ data , $ dependencies );
203
+ $ this ->onEvent ($ this , self ::EVENT_SAVE , $ key );
189
204
}
190
205
return $ data ;
191
206
}
0 commit comments