Cache_Memcache
› Kohana_Cache_Memcache
› Cache
› Kohana_Cache
Class Contents
Constants
Properties
Class declared in MODPATH/cache/classes/cache/memcache.php on line 3.
Constants
- CACHE_CEILING
integer 2592000
- DEFAULT_EXPIRE
integer 3600
Properties
- public static
$default string(4) "file"- public static
$instances array(0)
Methods
protected __construct( )
› Kohana_Cache_Memcache
Source Code
protected function __construct(array $config)
{
// Check for the memcache extention
if ( ! extension_loaded('memcache'))
{
throw new Kohana_Cache_Exception('Memcache PHP extention not loaded');
}
parent::__construct($config);
// Setup Memcache
$this->_memcache = new Memcache;
// Load servers from configuration
$servers = Arr::get($this->_config, 'servers', NULL);
if ( ! $servers)
{
// Throw an exception if no server found
throw new Kohana_Cache_Exception('No Memcache servers defined in configuration');
}
// Setup default server configuration
$this->_default_config = array(
'host' => 'localhost',
'port' => 11211,
'persistent' => FALSE,
'weight' => 1,
'timeout' => 1,
'retry_interval' => 15,
'status' => TRUE,
'instant_death' => TRUE,
'failure_callback' => array($this, '_failed_request'),
);
// Add the memcache servers to the pool
foreach ($servers as $server)
{
// Merge the defined config with defaults
$server += $this->_default_config;
if ( ! $this->_memcache->addServer($server['host'], $server['port'], $server['persistent'], $server['weight'], $server['timeout'], $server['retry_interval'], $server['status'], $server['failure_callback']))
{
throw new Kohana_Cache_Exception('Memcache could not connect to host \':host\' using port \':port\'', array(':host' => $server['host'], ':port' => $server['port']));
}
}
// Setup the flags
$this->_flags = Arr::get($this->_config, 'compression', FALSE) ? MEMCACHE_COMPRESSED : FALSE;
}
public _failed_request( )
› Kohana_Cache_Memcache
Source Code
public function _failed_request($hostname, $port)
{
if ( ! $this->_config['instant_death'])
return;
// Setup non-existent host
$host = FALSE;
// Get host settings from configuration
foreach ($this->_config['servers'] as $server)
{
// Merge the defaults, since they won't always be set
$server += $this->_default_config;
// We're looking at the failed server
if ($hostname == $server['host'] and $port == $server['port'])
{
// Server to disable, since it failed
$host = $server;
continue;
}
}
if ( ! $host)
return;
else
{
return $this->_memcache->setServerParams(
$host['host'],
$host['port'],
$host['timeout'],
$host['retry_interval'],
FALSE, // Server is offline
array($this, '_failed_request'
));
}
}
public delete( )
› Kohana_Cache_Memcache
Source Code
public function delete($id, $timeout = 0)
{
// Delete the id
return $this->_memcache->delete($this->_sanitize_id($id), $timeout);
}
public delete_all( )
› Kohana_Cache_Memcache
Source Code
public function delete_all()
{
$result = $this->_memcache->flush();
// We must sleep after flushing, or overwriting will not work!
// @see http://php.net/manual/en/function.memcache-flush.php#81420
sleep(1);
return $result;
}
public get( )
› Kohana_Cache_Memcache
Source Code
public function get($id, $default = NULL)
{
// Get the value from Memcache
$value = $this->_memcache->get($this->_sanitize_id($id));
// If the value wasn't found, normalise it
if ($value === FALSE)
{
$value = (NULL === $default) ? NULL : $default;
}
// Return the value
return $value;
}
public set( )
› Kohana_Cache_Memcache
Source Code
public function set($id, $data, $lifetime = 3600)
{
// If the lifetime is greater than the ceiling
if ($lifetime > Cache_Memcache::CACHE_CEILING)
{
// Set the lifetime to maximum cache time
$lifetime = Cache_Memcache::CACHE_CEILING + time();
}
// Else if the lifetime is greater than zero
elseif ($lifetime > 0)
{
$lifetime += time();
}
// Else
else
{
// Normalise the lifetime
$lifetime = 0;
}
// Set the data to memcache
return $this->_memcache->set($this->_sanitize_id($id), $data, $this->_flags, $lifetime);
}
public __clone( )
› Kohana_Cache
Source Code
public function __clone()
{
throw new Kohana_Cache_Exception('Cloning of Kohana_Cache objects is forbidden');
}
protected _sanitize_id( )
› Kohana_Cache
Source Code
protected function _sanitize_id($id)
{
// Change slashes and spaces to underscores
return str_replace(array('/', '\\', ' '), '_', $id);
}
public static instance( )
› Kohana_Cache
Source Code
public static function instance($group = NULL)
{
// If there is no group supplied
if ($group === NULL)
{
// Use the default setting
$group = Cache::$default;
}
if (isset(Cache::$instances[$group]))
{
// Return the current group if initiated already
return Cache::$instances[$group];
}
$config = Kohana::config('cache');
if ( ! $config->offsetExists($group))
{
throw new Kohana_Cache_Exception('Failed to load Kohana Cache group: :group', array(':group' => $group));
}
$config = $config->get($group);
// Create a new cache type instance
$cache_class = 'Cache_'.ucfirst($config['driver']);
Cache::$instances[$group] = new $cache_class($config);
// Return the instance
return Cache::$instances[$group];
}