Kohana Cache Eaccelerator driver. Provides an opcode based driver for the Kohana Cache library.
Below is an example of an eaccelerator server configuration.
return array(
'eaccelerator' => array( // Driver group
'driver' => 'eaccelerator', // using Eaccelerator 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/cache/eaccelerator.php on line 3.
integer 3600
string
$defaultdefault driver to use
string(4) "file"
Kohana_Cache
$instancesinstances
array(0)
Config
$_configDelete a cache entry based on id
// Delete 'foo' entry from the eaccelerator group
Cache::instance('eaccelerator')->delete('foo');
string
$id
required - Id to remove from cacheboolean
public function delete($id)
{
return eaccelerator_rm($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 eaccelerator group
Cache::instance('eaccelerator')->delete_all();
boolean
public function delete_all()
{
return eaccelerator_clean();
}
Retrieve a cached value entry by id.
// Retrieve cache entry from eaccelerator group
$data = Cache::instance('eaccelerator')->get('foo');
// Retrieve cache entry from eaccelerator group and return 'bar' if miss
$data = Cache::instance('eaccelerator')->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)
{
return (($data = eaccelerator_get($this->_sanitize_id($id))) === FALSE) ? $default : $data;
}
Set a value to cache with id and lifetime
$data = 'bar';
// Set 'bar' to 'foo' in eaccelerator group, using default expiry
Cache::instance('eaccelerator')->set('foo', $data);
// Set 'bar' to 'foo' in eaccelerator group for 30 seconds
Cache::instance('eaccelerator')->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 = time() + Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE);
}
return eaccelerator_put($this->_sanitize_id($id), $data, $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];
}
Check for existence of the eAccelerator 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('eaccelerator'))
{
throw new Kohana_Cache_Exception('PHP eAccelerator 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);
}