2018-04-17 04:11:51 +02:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Cache\Simple ;
2024-03-20 03:35:09 +01:00
use Psr\SimpleCache\CacheInterface as Psr16CacheInterface ;
use Symfony\Component\Cache\Adapter\TraceableAdapter ;
2018-04-17 04:11:51 +02:00
use Symfony\Component\Cache\PruneableInterface ;
use Symfony\Component\Cache\ResettableInterface ;
2024-03-20 03:35:09 +01:00
use Symfony\Contracts\Cache\CacheInterface ;
use Symfony\Contracts\Service\ResetInterface ;
@ trigger_error ( sprintf ( 'The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.' , TraceableCache :: class , TraceableAdapter :: class , CacheInterface :: class ), \E_USER_DEPRECATED );
2018-04-17 04:11:51 +02:00
/**
2024-03-20 03:35:09 +01:00
* @ deprecated since Symfony 4.3 , use TraceableAdapter and type - hint for CacheInterface instead .
2018-04-17 04:11:51 +02:00
*/
2024-03-20 03:35:09 +01:00
class TraceableCache implements Psr16CacheInterface , PruneableInterface , ResettableInterface
2018-04-17 04:11:51 +02:00
{
private $pool ;
private $miss ;
2024-01-12 06:08:24 +01:00
private $calls = [];
2018-04-17 04:11:51 +02:00
2024-03-20 03:35:09 +01:00
public function __construct ( Psr16CacheInterface $pool )
2018-04-17 04:11:51 +02:00
{
$this -> pool = $pool ;
$this -> miss = new \stdClass ();
}
/**
* { @ inheritdoc }
*/
public function get ( $key , $default = null )
{
2024-01-12 06:08:24 +01:00
$miss = null !== $default && \is_object ( $default ) ? $default : $this -> miss ;
2018-04-17 04:11:51 +02:00
$event = $this -> start ( __FUNCTION__ );
try {
$value = $this -> pool -> get ( $key , $miss );
} finally {
$event -> end = microtime ( true );
}
if ( $event -> result [ $key ] = $miss !== $value ) {
++ $event -> hits ;
} else {
++ $event -> misses ;
$value = $default ;
}
return $value ;
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function has ( $key )
{
$event = $this -> start ( __FUNCTION__ );
try {
return $event -> result [ $key ] = $this -> pool -> has ( $key );
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function delete ( $key )
{
$event = $this -> start ( __FUNCTION__ );
try {
return $event -> result [ $key ] = $this -> pool -> delete ( $key );
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function set ( $key , $value , $ttl = null )
{
$event = $this -> start ( __FUNCTION__ );
try {
return $event -> result [ $key ] = $this -> pool -> set ( $key , $value , $ttl );
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function setMultiple ( $values , $ttl = null )
{
$event = $this -> start ( __FUNCTION__ );
2024-01-12 06:08:24 +01:00
$event -> result [ 'keys' ] = [];
2018-04-17 04:11:51 +02:00
if ( $values instanceof \Traversable ) {
$values = function () use ( $values , $event ) {
foreach ( $values as $k => $v ) {
$event -> result [ 'keys' ][] = $k ;
yield $k => $v ;
}
};
$values = $values ();
2024-01-12 06:08:24 +01:00
} elseif ( \is_array ( $values )) {
2018-04-17 04:11:51 +02:00
$event -> result [ 'keys' ] = array_keys ( $values );
}
try {
return $event -> result [ 'result' ] = $this -> pool -> setMultiple ( $values , $ttl );
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return iterable
2018-04-17 04:11:51 +02:00
*/
public function getMultiple ( $keys , $default = null )
{
2024-01-12 06:08:24 +01:00
$miss = null !== $default && \is_object ( $default ) ? $default : $this -> miss ;
2018-04-17 04:11:51 +02:00
$event = $this -> start ( __FUNCTION__ );
try {
$result = $this -> pool -> getMultiple ( $keys , $miss );
} finally {
$event -> end = microtime ( true );
}
$f = function () use ( $result , $event , $miss , $default ) {
2024-01-12 06:08:24 +01:00
$event -> result = [];
2018-04-17 04:11:51 +02:00
foreach ( $result as $key => $value ) {
if ( $event -> result [ $key ] = $miss !== $value ) {
++ $event -> hits ;
} else {
++ $event -> misses ;
$value = $default ;
}
yield $key => $value ;
}
};
return $f ();
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function clear ()
{
$event = $this -> start ( __FUNCTION__ );
try {
return $event -> result = $this -> pool -> clear ();
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
2024-03-20 03:35:09 +01:00
*
* @ return bool
2018-04-17 04:11:51 +02:00
*/
public function deleteMultiple ( $keys )
{
$event = $this -> start ( __FUNCTION__ );
if ( $keys instanceof \Traversable ) {
$keys = $event -> result [ 'keys' ] = iterator_to_array ( $keys , false );
} else {
$event -> result [ 'keys' ] = $keys ;
}
try {
return $event -> result [ 'result' ] = $this -> pool -> deleteMultiple ( $keys );
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
*/
public function prune ()
{
if ( ! $this -> pool instanceof PruneableInterface ) {
return false ;
}
$event = $this -> start ( __FUNCTION__ );
try {
return $event -> result = $this -> pool -> prune ();
} finally {
$event -> end = microtime ( true );
}
}
/**
* { @ inheritdoc }
*/
public function reset ()
{
2024-03-20 03:35:09 +01:00
if ( ! $this -> pool instanceof ResetInterface ) {
2018-04-17 04:11:51 +02:00
return ;
}
$event = $this -> start ( __FUNCTION__ );
try {
$this -> pool -> reset ();
} finally {
$event -> end = microtime ( true );
}
}
public function getCalls ()
{
try {
return $this -> calls ;
} finally {
2024-01-12 06:08:24 +01:00
$this -> calls = [];
2018-04-17 04:11:51 +02:00
}
}
2024-03-20 03:35:09 +01:00
private function start ( string $name ) : TraceableCacheEvent
2018-04-17 04:11:51 +02:00
{
$this -> calls [] = $event = new TraceableCacheEvent ();
$event -> name = $name ;
$event -> start = microtime ( true );
return $event ;
}
}
class TraceableCacheEvent
{
public $name ;
public $start ;
public $end ;
public $result ;
public $hits = 0 ;
public $misses = 0 ;
}