Class Cache_MemcacheTag

Cache_MemcacheTag

extends Kohana_Cache_MemcacheTag
extends Cache_Memcache
extends Kohana_Cache_Memcache
extends Cache
extends Kohana_Cache

Implements: Kohana_Cache_Tagging | Cache_Tagging | Kohana_Cache_Arithmetic | Cache_Arithmetic

See Kohana_Cache_Memcache

package
Kohana/Cache
category
Base
version
2.0
author
Kohana Team
copyright
(c) Kohana Team
license
https://koseven.ga/LICENSE.md


Information

This class is a transparent base class for Kohana_Cache_MemcacheTag

Constants

CACHE_CEILING

integer 2592000

DEFAULT_EXPIRE

integer 3600

Properties

public static string $default

default driver to use

string(4) "file"

public static Kohana_Cache $instances

instances

array(0) 

protected Config $_config

Default value:
array(0) 

protected array $_default_config

The default configuration for the memcached server

Default value:
array(0) 

protected string $_flags

Flags to use when storing values

Default value:
NULL

protected Memcache $_memcache

Memcache resource

Default value:
NULL

Methods

public delete_tag(string $tag ) (defined in Kohana_Cache_MemcacheTag)

Delete cache entries based on a tag

Parameters

  • string $tag required - Tag

Return Values

  • boolean

Source Code

public function delete_tag($tag)
{
	return $this->_memcache->tag_delete($tag);
}

public find(string $tag ) (defined in Kohana_Cache_MemcacheTag)

Find cache entries based on a tag

Parameters

  • string $tag required - Tag

Tags

Return Values

  • void

Source Code

public function find($tag)
{
	throw new Cache_Exception('Memcached-tags does not support finding by tag');
}

public set_with_tags(string $id , mixed $data [, integer $lifetime = NULL , array $tags = NULL ] ) (defined in Kohana_Cache_MemcacheTag)

Set a value based on an id with tags

Parameters

  • string $id required - Id
  • mixed $data required - Data
  • integer $lifetime = NULL - Lifetime [Optional]
  • array $tags = NULL - Tags [Optional]

Return Values

  • boolean

Source Code

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;
}

public _failed_request(string $hostname , integer $port ) (defined in Kohana_Cache_Memcache)

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.

Parameters

  • string $hostname required - $hostname
  • integer $port required - $port

Tags

  • Since - 3.0.8

Return Values

  • void|boolean

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
			[$this, '_failed_request'
			]);
	}
}

public decrement(string $id [, int $step = integer 1 ] ) (defined in Kohana_Cache_Memcache)

Decrements a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.

Parameters

  • string $id required - Id of cache entry to decrement
  • int $step = integer 1 - Step value to decrement by

Return Values

  • integer
  • boolean

Source Code

public function decrement($id, $step = 1)
{
	return $this->_memcache->decrement($id, $step);
}

public delete(string $id [, integer $timeout = integer 0 ] ) (defined in Kohana_Cache_Memcache)

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);

Parameters

  • string $id required - Id of entry to delete
  • integer $timeout = integer 0 - Timeout of entry, if zero item is deleted immediately, otherwise the item will delete after the specified value in seconds

Return Values

  • boolean

Source Code

public function delete($id, $timeout = 0)
{
	// Delete the id
	return $this->_memcache->delete($this->_sanitize_id($id), $timeout);
}

public delete_all() (defined in Kohana_Cache_Memcache)

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();

Return Values

  • boolean

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(string $id [, string $default = NULL ] ) (defined in Kohana_Cache_Memcache)

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');

Parameters

  • string $id required - Id of cache to entry
  • string $default = NULL - Default value to return if cache miss

Tags

Return Values

  • mixed

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 increment(string $id [, int $step = integer 1 ] ) (defined in Kohana_Cache_Memcache)

Increments a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.

Parameters

  • string $id required - Id of cache entry to increment
  • int $step = integer 1 - Step value to increment by

Return Values

  • integer
  • boolean

Source Code

public function increment($id, $step = 1)
{
	return $this->_memcache->increment($id, $step);
}

public set(string $id , mixed $data [, integer $lifetime = integer 3600 ] ) (defined in Kohana_Cache_Memcache)

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
}

Parameters

  • string $id required - Id of cache entry
  • mixed $data required - Data to set to cache
  • integer $lifetime = integer 3600 - Lifetime in seconds, maximum value 2592000

Return Values

  • boolean

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);
}

final public __clone() (defined in Kohana_Cache)

Overload the __clone() method to prevent cloning

Tags

Return Values

  • void

Source Code

final public function __clone()
{
	throw new Cache_Exception('Cloning of Kohana_Cache objects is forbidden');
}

public config([ mixed $key = NULL , mixed $value = NULL ] ) (defined in Kohana_Cache)

Getter and setter for the configuration. If no argument provided, the current configuration is returned. Otherwise the configuration is set to this class.

// Overwrite all configuration
$cache->config(array('driver' => 'memcache', '...'));

// Set a new configuration setting
$cache->config('servers', array(
     'foo' => 'bar',
     '...'
     ));

// Get a configuration setting
$servers = $cache->config('servers);

Parameters

  • mixed $key = NULL - Key to set to array, either array or config path
  • mixed $value = NULL - Value to associate with key

Return Values

  • mixed

Source Code

public function config($key = NULL, $value = NULL)
{
	if ($key === NULL)
		return $this->_config;

	if (is_array($key))
	{
		$this->_config = $key;
	}
	else
	{
		if ($value === NULL)
			return Arr::get($this->_config, $key);

		$this->_config[$key] = $value;
	}

	return $this;
}

public static instance([ string $group = NULL ] ) (defined in Kohana_Cache)

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'];

Parameters

  • string $group = NULL - The name of the cache group to use [Optional]

Tags

Return Values

  • Cache

Source Code

public static function instance($group = NULL)
{
       // If there is no group supplied, try to get it from the config
       if ($group === NULL)
       {
           $group = Kohana::$config->load('cache.default');
       }

	// 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->load('cache');

	if ( ! $config->offsetExists($group))
	{
		throw new Cache_Exception(
			'Failed to load Kohana Cache group: :group',
			[':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];
}

protected __construct(array $config ) (defined in Kohana_Cache_MemcacheTag)

Constructs the memcache object

Parameters

  • array $config required - Configuration

Tags

Source Code

protected function __construct(array $config)
{
	parent::__construct($config);

	if ( ! method_exists($this->_memcache, 'tag_add'))
	{
		throw new Cache_Exception('Memcached-tags PHP plugin not present. Please see http://code.google.com/p/memcached-tags/ for more information');
	}
}

protected _sanitize_id(string $id ) (defined in Kohana_Cache)

Replaces troublesome characters with underscores and adds prefix to avoid duplicates

// Sanitize a cache id
$id = $this->_sanitize_id($id);

Parameters

  • string $id required - Id of cache to sanitize

Return Values

  • string

Source Code

protected function _sanitize_id($id)
{

    // adding cache prefix to avoid duplicates
    $prefix = '';
    // configuration for the specific cache group
    if (isset($this->_config['prefix']) AND $this->_config['prefix'] !== NULL)
    {
        $prefix = $this->_config['prefix'];
    }
    // prefix general configuration cache
    else
    {
        $prefix = Kohana::$config->load('cache.prefix');
    }

    // sha1 the id makes sure name is not too long and has not any not allowed characters
    return $prefix.sha1($id);
}

Do you want to contribute to Koseven?

We need YOUR help!

This project is open source. What does this mean? YOU can help:
  • Found a bug? Report it on Github
  • Need a feature? Add it Here
  • Want to help? Join the Forum
Go to Github