2015-08-17 17:00:26 -07:00
< ? php
namespace Doctrine\Common\Cache ;
2018-11-23 12:29:20 +00:00
use Memcache ;
use function time ;
2015-08-17 17:00:26 -07:00
/**
* Memcache cache provider .
*
* @ link www . doctrine - project . org
2018-11-23 12:29:20 +00:00
*
* @ deprecated
2015-08-17 17:00:26 -07:00
*/
class MemcacheCache extends CacheProvider
{
2018-11-23 12:29:20 +00:00
/** @var Memcache|null */
2015-08-17 17:00:26 -07:00
private $memcache ;
/**
* Sets the memcache instance to use .
*
* @ return void
*/
public function setMemcache ( Memcache $memcache )
{
$this -> memcache = $memcache ;
}
/**
* Gets the memcache instance used by the cache .
*
* @ return Memcache | null
*/
public function getMemcache ()
{
return $this -> memcache ;
}
/**
* { @ inheritdoc }
*/
protected function doFetch ( $id )
{
return $this -> memcache -> get ( $id );
}
/**
* { @ inheritdoc }
*/
protected function doContains ( $id )
{
2015-10-08 11:40:12 -07:00
$flags = null ;
$this -> memcache -> get ( $id , $flags );
2018-11-23 12:29:20 +00:00
2015-10-08 11:40:12 -07:00
//if memcache has changed the value of "flags", it means the value exists
2018-11-23 12:29:20 +00:00
return $flags !== null ;
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doSave ( $id , $data , $lifeTime = 0 )
{
if ( $lifeTime > 30 * 24 * 3600 ) {
$lifeTime = time () + $lifeTime ;
}
return $this -> memcache -> set ( $id , $data , 0 , ( int ) $lifeTime );
}
/**
* { @ inheritdoc }
*/
protected function doDelete ( $id )
{
2017-04-13 15:53:35 +01:00
// Memcache::delete() returns false if entry does not exist
return $this -> memcache -> delete ( $id ) || ! $this -> doContains ( $id );
2015-08-17 17:00:26 -07:00
}
/**
* { @ inheritdoc }
*/
protected function doFlush ()
{
return $this -> memcache -> flush ();
}
/**
* { @ inheritdoc }
*/
protected function doGetStats ()
{
$stats = $this -> memcache -> getStats ();
2018-11-23 12:29:20 +00:00
return [
2015-08-17 17:00:26 -07:00
Cache :: STATS_HITS => $stats [ 'get_hits' ],
Cache :: STATS_MISSES => $stats [ 'get_misses' ],
Cache :: STATS_UPTIME => $stats [ 'uptime' ],
Cache :: STATS_MEMORY_USAGE => $stats [ 'bytes' ],
Cache :: STATS_MEMORY_AVAILABLE => $stats [ 'limit_maxbytes' ],
2018-11-23 12:29:20 +00:00
];
2015-08-17 17:00:26 -07:00
}
}