Implements: Cache_Arithmetic | Kohana_Cache_Arithmetic
This class is a transparent base class for Cache_Apc and should not be accessed directly.
Kohana Cache APC driver. Provides an opcode based driver for the Kohana Cache library.
Below is an example of an apc server configuration.
return array(
'apc' => array( // Driver group
'driver' => 'apc', // using APC driver
),
)
In cases where only one cache group is required, if the group is named default
there is
no need to pass the group name when instantiating a cache instance.
Below are the settings available to all types of cache driver.
Name | Required | Description |
---|---|---|
driver | YES | (string) The driver type to use |
Class declared in MODPATH/cache/classes/Kohana/Cache/Apc.php on line 39.
integer 3600
string
$defaultdefault driver to use
string(4) "file"
Kohana_Cache
$instancesinstances
array(0)
Config
$_configarray(0)
Decrements a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.
string
$id
required - Id of cache entry to decrementint
$step
= integer 1 - Step value to decrement byinteger
boolean
public function decrement($id, $step = 1)
{
return apc_dec($id, $step);
}
Delete a cache entry based on id
// Delete 'foo' entry from the apc group
Cache::instance('apc')->delete('foo');
string
$id
required - Id to remove from cacheboolean
public function delete($id)
{
return apc_delete($this->_sanitize_id($id));
}
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 apc group
Cache::instance('apc')->delete_all();
boolean
public function delete_all()
{
return apc_clear_cache('user');
}
Retrieve a cached value entry by id.
// Retrieve cache entry from apc group
$data = Cache::instance('apc')->get('foo');
// Retrieve cache entry from apc group and return 'bar' if miss
$data = Cache::instance('apc')->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)
{
$data = apc_fetch($this->_sanitize_id($id), $success);
return $success ? $data : $default;
}
Increments a given value by the step value supplied. Useful for shared counters and other persistent integer based tracking.
string
$id
required - Id of cache entry to incrementint
$step
= integer 1 - Step value to increment byinteger
boolean
public function increment($id, $step = 1)
{
return apc_inc($id, $step);
}
Set a value to cache with id and lifetime
$data = 'bar';
// Set 'bar' to 'foo' in apc group, using default expiry
Cache::instance('apc')->set('foo', $data);
// Set 'bar' to 'foo' in apc group for 30 seconds
Cache::instance('apc')->set('foo', $data, 30);
string
$id
required - Id of cache entrystring
$data
required - Data to set to cacheinteger
$lifetime
= NULL - Lifetime in secondsboolean
public function set($id, $data, $lifetime = NULL)
{
if ($lifetime === NULL)
{
$lifetime = Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
}
return apc_store($this->_sanitize_id($id), $data, $lifetime);
}
Overload the __clone() method to prevent cloning
void
final public function __clone()
{
throw new Cache_Exception('Cloning of Kohana_Cache objects is forbidden');
}
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);
mixed
$key
= NULL - Key to set to array, either array or config pathmixed
$value
= NULL - Value to associate with keymixed
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;
}
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->load('cache');
if ( ! $config->offsetExists($group))
{
throw new 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];
}
Check for existence of the APC extension This method cannot be invoked externally. The driver must
be instantiated using the Cache::instance()
method.
array
$config
required - Configurationprotected function __construct(array $config)
{
if ( ! extension_loaded('apc'))
{
throw new Cache_Exception('PHP APC extension is not available.');
}
parent::__construct($config);
}
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);
}