@@ -178,11 +178,13 @@ protected function startTiming($prefix) {
178
178
}
179
179
}
180
180
181
- protected function stopTiming ($ prefix , $ size ) {
181
+ protected function stopTiming ($ prefix , $ size = false ) {
182
182
if ($ this ->metric ) {
183
183
$ this ->metric ->endTiming ($ prefix );
184
- // I know that is not a timing but statsd make for timing min max
185
- $ this ->metric ->timing ('size. ' . $ prefix , $ size );
184
+ if ($ size ) {
185
+ // I know that is not a timing but statsd make for timing min max
186
+ $ this ->metric ->timing ('size. ' . $ prefix , $ size );
187
+ }
186
188
}
187
189
}
188
190
@@ -234,6 +236,7 @@ public function save($entryName, $data, $nonExpiration = FALSE){
234
236
$ this ->startTiming ($ metricPrefix );
235
237
$ saveRes = false ;
236
238
$ this ->store ($ entryName , $ data );
239
+ $ size = false ;
237
240
switch ($ this ->driver ){
238
241
case 'redis ' :
239
242
case 'memcached ' :
@@ -254,10 +257,12 @@ public function save($entryName, $data, $nonExpiration = FALSE){
254
257
$ aCachedData = array ('lifetime ' => $ lifetime , 'data ' => $ data , 'lock ' => true );
255
258
if ('redis ' == $ this ->driver ){
256
259
$ saveOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR ;
260
+ $ serializedCashData = json_encode ($ aCachedData , $ saveOptions );
261
+ $ size = strlen ($ serializedCashData );
257
262
if ($ nonExpiration ){
258
- $ saveRes = $ this ->oDriver ->set ($ entryName , json_encode ( $ aCachedData , $ saveOptions ) );
263
+ $ saveRes = $ this ->oDriver ->set ($ entryName , $ serializedCashData );
259
264
}else {
260
- $ saveRes = $ this ->oDriver ->set ($ entryName , json_encode ( $ aCachedData , $ saveOptions ) , 'ex ' , $ ttl );
265
+ $ saveRes = $ this ->oDriver ->set ($ entryName , $ serializedCashData , 'ex ' , $ ttl );
261
266
}
262
267
if ('OK ' !== (string )$ saveRes ){
263
268
error_log ("Write data to redis failed: " . $ saveRes . " Data: " . json_encode ($ aCachedData ) . " TTL: " . $ ttl );
@@ -273,11 +278,12 @@ public function save($entryName, $data, $nonExpiration = FALSE){
273
278
$ filename = $ this ->path . '/ ' . $ entryName . ".tmp " ;
274
279
//@unlink($filename);
275
280
$ json = json_encode ($ data , JSON_PRETTY_PRINT );
281
+ $ size = strlen ($ json );
276
282
$ saveRes = !!file_put_contents ($ filename , $ json );
277
283
break ;
278
284
}
279
285
if ($ this ->useLocks ) $ this ->deleteLock ($ entryName );
280
- $ this ->stopTiming ($ metricPrefix , strlen ( json_encode ( $ data )) );
286
+ $ this ->stopTiming ($ metricPrefix , $ size );
281
287
return $ saveRes ;
282
288
}
283
289
@@ -359,8 +365,15 @@ public function loadCachedData($entryName, $default = NULL, $cacheLifetime = FAL
359
365
$ this ->startTiming ($ prefix );
360
366
$ result = array ('data ' => $ default , 'expired ' => FALSE );
361
367
$ file = ('file ' === $ this ->driver );
368
+ $ size = false ;
362
369
if ('memcached ' === $ this ->driver || 'redis ' === $ this ->driver ){
363
- $ memcachedData = ('redis ' == $ this ->driver ) ? json_decode ($ this ->oDriver ->get ($ entryName ), TRUE ) : $ this ->oDriver ->get ($ entryName );
370
+ if ('redis ' == $ this ->driver ) {
371
+ $ cachedData = $ this ->oDriver ->get ($ entryName );
372
+ $ size = strlen ($ cachedData );
373
+ $ memcachedData = json_decode ($ this ->oDriver ->get ($ entryName ), TRUE );
374
+ } else {
375
+ $ memcachedData = $ this ->oDriver ->get ($ entryName );
376
+ }
364
377
if ($ memcachedData && isset ($ memcachedData ['lifetime ' ]) && isset ($ memcachedData ['data ' ])){
365
378
$ result ['data ' ] = $ memcachedData ['data ' ];
366
379
if ($ memcachedData ['lifetime ' ] > 0 && $ memcachedData ['lifetime ' ] < time ()){
@@ -388,12 +401,13 @@ public function loadCachedData($entryName, $default = NULL, $cacheLifetime = FAL
388
401
}
389
402
if (!$ isFileExpired || !$ result ['data ' ] || $ result ['expired ' ]){
390
403
$ contents = @file_get_contents ($ filename );
404
+ $ size = strlen ($ contents );
391
405
$ result ['data ' ] = json_decode ($ contents , TRUE );
392
406
$ result ['expired ' ] = $ isFileExpired ;
393
407
}
394
408
}
395
409
}
396
- $ this ->stopTiming ($ prefix , strlen ( json_encode ( $ result )) );
410
+ $ this ->stopTiming ($ prefix , $ size );
397
411
return $ result ;
398
412
}
399
413
0 commit comments