2015-10-08 11:40:12 -07:00
< ? php
namespace Doctrine\Common\Cache ;
2017-04-13 15:53:35 +01:00
use Predis\ClientInterface ;
2018-11-23 12:29:20 +00:00
use function array_combine ;
use function array_filter ;
use function array_map ;
use function call_user_func_array ;
use function serialize ;
use function unserialize ;
2015-10-08 11:40:12 -07:00
/**
* Predis cache provider .
*/
class PredisCache extends CacheProvider
{
2018-11-23 12:29:20 +00:00
/** @var ClientInterface */
2015-10-08 11:40:12 -07:00
private $client ;
2017-04-13 15:53:35 +01:00
public function __construct ( ClientInterface $client )
2015-10-08 11:40:12 -07:00
{
$this -> client = $client ;
}
/**
* { @ inheritdoc }
*/
protected function doFetch ( $id )
{
$result = $this -> client -> get ( $id );
2018-11-23 12:29:20 +00:00
if ( $result === null ) {
2015-10-08 11:40:12 -07:00
return false ;
}
return unserialize ( $result );
}
/**
* { @ inheritdoc }
*/
protected function doFetchMultiple ( array $keys )
{
2018-11-23 12:29:20 +00:00
$fetchedItems = call_user_func_array ([ $this -> client , 'mget' ], $keys );
2015-10-08 11:40:12 -07:00
2017-04-13 15:53:35 +01:00
return array_map ( 'unserialize' , array_filter ( array_combine ( $keys , $fetchedItems )));
2015-10-08 11:40:12 -07:00
}
2017-04-13 15:53:35 +01:00
/**
* { @ inheritdoc }
*/
protected function doSaveMultiple ( array $keysAndValues , $lifetime = 0 )
{
if ( $lifetime ) {
$success = true ;
// Keys have lifetime, use SETEX for each of them
foreach ( $keysAndValues as $key => $value ) {
2018-11-23 12:29:20 +00:00
$response = ( string ) $this -> client -> setex ( $key , $lifetime , serialize ( $value ));
2017-04-13 15:53:35 +01:00
2018-11-23 12:29:20 +00:00
if ( $response == 'OK' ) {
continue ;
2017-04-13 15:53:35 +01:00
}
2018-11-23 12:29:20 +00:00
$success = false ;
2017-04-13 15:53:35 +01:00
}
return $success ;
}
// No lifetime, use MSET
$response = $this -> client -> mset ( array_map ( function ( $value ) {
return serialize ( $value );
}, $keysAndValues ));
return ( string ) $response == 'OK' ;
}
2015-10-08 11:40:12 -07:00
/**
* { @ inheritdoc }
*/
protected function doContains ( $id )
{
2017-04-13 15:53:35 +01:00
return ( bool ) $this -> client -> exists ( $id );
2015-10-08 11:40:12 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doSave ( $id , $data , $lifeTime = 0 )
{
$data = serialize ( $data );
if ( $lifeTime > 0 ) {
$response = $this -> client -> setex ( $id , $lifeTime , $data );
} else {
$response = $this -> client -> set ( $id , $data );
}
return $response === true || $response == 'OK' ;
}
/**
* { @ inheritdoc }
*/
protected function doDelete ( $id )
{
2017-04-13 15:53:35 +01:00
return $this -> client -> del ( $id ) >= 0 ;
2015-10-08 11:40:12 -07:00
}
2018-11-23 12:29:20 +00:00
/**
* { @ inheritdoc }
*/
protected function doDeleteMultiple ( array $keys )
{
return $this -> client -> del ( $keys ) >= 0 ;
}
2015-10-08 11:40:12 -07:00
/**
* { @ inheritdoc }
*/
protected function doFlush ()
{
$response = $this -> client -> flushdb ();
return $response === true || $response == 'OK' ;
}
/**
* { @ inheritdoc }
*/
protected function doGetStats ()
{
$info = $this -> client -> info ();
2018-11-23 12:29:20 +00:00
return [
2015-10-08 11:40:12 -07:00
Cache :: STATS_HITS => $info [ 'Stats' ][ 'keyspace_hits' ],
Cache :: STATS_MISSES => $info [ 'Stats' ][ 'keyspace_misses' ],
Cache :: STATS_UPTIME => $info [ 'Server' ][ 'uptime_in_seconds' ],
Cache :: STATS_MEMORY_USAGE => $info [ 'Memory' ][ 'used_memory' ],
2018-11-23 12:29:20 +00:00
Cache :: STATS_MEMORY_AVAILABLE => false ,
];
2015-10-08 11:40:12 -07:00
}
}