2015-08-17 17:00:26 -07:00
< ? php
namespace Doctrine\Common\Cache ;
use Redis ;
2018-11-23 12:29:20 +00:00
use function array_combine ;
use function defined ;
use function extension_loaded ;
use function is_bool ;
2015-08-17 17:00:26 -07:00
/**
* Redis cache provider .
*
* @ link www . doctrine - project . org
*/
class RedisCache extends CacheProvider
{
2018-11-23 12:29:20 +00:00
/** @var Redis|null */
2015-08-17 17:00:26 -07:00
private $redis ;
/**
* Sets the redis instance to use .
*
* @ return void
*/
public function setRedis ( Redis $redis )
{
$redis -> setOption ( Redis :: OPT_SERIALIZER , $this -> getSerializerValue ());
$this -> redis = $redis ;
}
/**
* Gets the redis instance used by the cache .
*
* @ return Redis | null
*/
public function getRedis ()
{
return $this -> redis ;
}
/**
* { @ inheritdoc }
*/
protected function doFetch ( $id )
{
return $this -> redis -> get ( $id );
}
2015-10-08 11:40:12 -07:00
/**
* { @ inheritdoc }
*/
protected function doFetchMultiple ( array $keys )
{
2017-04-13 15:53:35 +01:00
$fetchedItems = array_combine ( $keys , $this -> redis -> mget ( $keys ));
// Redis mget returns false for keys that do not exist. So we need to filter those out unless it's the real data.
2018-11-23 12:29:20 +00:00
$foundItems = [];
2017-04-13 15:53:35 +01:00
foreach ( $fetchedItems as $key => $value ) {
2018-11-23 12:29:20 +00:00
if ( $value === false && ! $this -> redis -> exists ( $key )) {
continue ;
2017-04-13 15:53:35 +01:00
}
2018-11-23 12:29:20 +00:00
$foundItems [ $key ] = $value ;
2017-04-13 15:53:35 +01:00
}
return $foundItems ;
}
/**
* { @ 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
if ( $this -> redis -> setex ( $key , $lifetime , $value )) {
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
return ( bool ) $this -> redis -> mset ( $keysAndValues );
2015-10-08 11:40:12 -07:00
}
2015-08-17 17:00:26 -07:00
/**
* { @ inheritdoc }
*/
protected function doContains ( $id )
{
2018-11-23 12:29:20 +00:00
$exists = $this -> redis -> exists ( $id );
if ( is_bool ( $exists )) {
return $exists ;
}
return $exists > 0 ;
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doSave ( $id , $data , $lifeTime = 0 )
{
if ( $lifeTime > 0 ) {
return $this -> redis -> setex ( $id , $lifeTime , $data );
}
return $this -> redis -> set ( $id , $data );
}
/**
* { @ inheritdoc }
*/
protected function doDelete ( $id )
{
2017-04-13 15:53:35 +01:00
return $this -> redis -> delete ( $id ) >= 0 ;
2015-08-17 17:00:26 -07:00
}
2018-11-23 12:29:20 +00:00
/**
* { @ inheritdoc }
*/
protected function doDeleteMultiple ( array $keys )
{
return $this -> redis -> delete ( $keys ) >= 0 ;
}
2015-08-17 17:00:26 -07:00
/**
* { @ inheritdoc }
*/
protected function doFlush ()
{
return $this -> redis -> flushDB ();
}
/**
* { @ inheritdoc }
*/
protected function doGetStats ()
{
$info = $this -> redis -> info ();
2018-11-23 12:29:20 +00:00
return [
2015-10-08 11:40:12 -07:00
Cache :: STATS_HITS => $info [ 'keyspace_hits' ],
Cache :: STATS_MISSES => $info [ 'keyspace_misses' ],
2015-08-17 17:00:26 -07:00
Cache :: STATS_UPTIME => $info [ 'uptime_in_seconds' ],
Cache :: STATS_MEMORY_USAGE => $info [ 'used_memory' ],
2018-11-23 12:29:20 +00:00
Cache :: STATS_MEMORY_AVAILABLE => false ,
];
2015-08-17 17:00:26 -07:00
}
/**
* Returns the serializer constant to use . If Redis is compiled with
* igbinary support , that is used . Otherwise the default PHP serializer is
* used .
*
2018-11-23 12:29:20 +00:00
* @ return int One of the Redis :: SERIALIZER_ * constants
2015-08-17 17:00:26 -07:00
*/
protected function getSerializerValue ()
{
2017-04-13 15:53:35 +01:00
if ( defined ( 'Redis::SERIALIZER_IGBINARY' ) && extension_loaded ( 'igbinary' )) {
return Redis :: SERIALIZER_IGBINARY ;
}
return Redis :: SERIALIZER_PHP ;
2015-08-17 17:00:26 -07:00
}
}