Implements: Kohana_Cache_Tagging
Class declared in MODPATH/cache/classes/kohana/cache/memcachetag.php on line 12.
integer 2592000
integer 3600
string
$defaultdefault driver to use
string(4) "file"
Kohana_Cache
$instancesinstances
array(0)
Config
$_configarray
$_default_configThe default configuration for the memcached server
string
$_flagsFlags to use when storing values
Memcache
$_memcacheMemcache resource
Delete cache entries based on a tag
string
$tag
required - Tagboolean
public function delete_tag($tag)
{
return $this->_memcache->tag_delete($tag);
}
Find cache entries based on a tag
string
$tag
required - Tagvoid
public function find($tag)
{
throw new Kohana_Cache_Exception('Memcached-tags does not support finding by tag');
}
Set a value based on an id with tags
string
$id
required - Idmixed
$data
required - Datainteger
$lifetime
= NULL - Lifetime [Optional]array
$tags
= NULL - Tags [Optional]boolean
public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL)
{
$id = $this->_sanitize_id($id);
$result = $this->set($id, $data, $lifetime);
if ($result and $tags)
{
foreach ($tags as $tag)
{
$this->_memcache->tag_add($tag, $id);
}
}
return $result;
}
Callback method for Memcache::failure_callback to use if any Memcache call
on a particular server fails. This method switches off that instance of the
server if the configuration setting instant_death
is set to TRUE
.
string
$hostname
required - $hostnameinteger
$port
required - $portvoid|boolean
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'
));
}
}
Delete a cache entry based on id
// Delete the 'foo' cache entry immediately
Cache::instance('memcache')->delete('foo');
// Delete the 'bar' cache entry after 30 seconds
Cache::instance('memcache')->delete('bar', 30);
string
$id
required - Id of entry to deleteinteger
$timeout
= integer 0 - Timeout of entry, if zero item is deleted immediately, otherwise the item will delete after the specified value in secondsboolean
public function delete($id, $timeout = 0)
{
// Delete the id
return $this->_memcache->delete($this->_sanitize_id($id), $timeout);
}
Delete all cache entries.
Beware of using this method when using shared memory cache systems, as it will wipe every entry within the system for all clients.
// Delete all cache entries in the default group
Cache::instance('memcache')->delete_all();
boolean
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;
}
Retrieve a cached value entry by id.
// Retrieve cache entry from memcache group
$data = Cache::instance('memcache')->get('foo');
// Retrieve cache entry from memcache group and return 'bar' if miss
$data = Cache::instance('memcache')->get('foo', 'bar');
string
$id
required - Id of cache to entrystring
$default
= NULL - Default value to return if cache missmixed
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;
}
Set a value to cache with id and lifetime
$data = 'bar';
// Set 'bar' to 'foo' in memcache group for 10 minutes
if (Cache::instance('memcache')->set('foo', $data, 600))
{
// Cache was set successfully
return
}
string
$id
required - Id of cache entrymixed
$data
required - Data to set to cacheinteger
$lifetime
= integer 3600 - Lifetime in seconds, maximum value 2592000boolean
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);
}
Overload the __clone() method to prevent cloning
void
public function __clone()
{
throw new Kohana_Cache_Exception('Cloning of Kohana_Cache objects is forbidden');
}
Creates a singleton of a Kohana Cache group. If no group is supplied the default cache group is used.
// Create an instance of the default group
$default_group = Cache::instance();
// Create an instance of a group
$foo_group = Cache::instance('foo');
// Access an instantiated group directly
$foo_group = Cache::$instances['default'];
string
$group
= NULL - The name of the cache group to use [Optional]Cache
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];
}
Constructs the memcache object
array
$config
required - Configurationprotected function __construct(array $config)
{
parent::__construct($config);
if ( ! method_exists($this->_memcache, 'tag_add'))
{
throw new Kohana_Cache_Exception('Memcached-tags PHP plugin not present. Please see http://code.google.com/p/memcached-tags/ for more information');
}
}
Replaces troublesome characters with underscores.
// Sanitize a cache id
$id = $this->_sanitize_id($id);
string
$id
required - Id of cache to sanitizestring
protected function _sanitize_id($id)
{
// Change slashes and spaces to underscores
return str_replace(array('/', '\\', ' '), '_', $id);
}