2011-01-13 14:49:56 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct script access.');
|
|
|
|
/**
|
|
|
|
* [Kohana Cache](api/Kohana_Cache) APC driver. Provides an opcode based
|
|
|
|
* driver for the Kohana Cache library.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* ### Configuration example
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* Below is an example of an _apc_ server configuration.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* return array(
|
|
|
|
* 'apc' => array( // Driver group
|
|
|
|
* 'driver' => 'apc', // using APC driver
|
|
|
|
* ),
|
|
|
|
* )
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* 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.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* #### General cache group configuration settings
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* Below are the settings available to all types of cache driver.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* Name | Required | Description
|
|
|
|
* -------------- | -------- | ---------------------------------------------------------------
|
|
|
|
* driver | __YES__ | (_string_) The driver type to use
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* ### System requirements
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* * Kohana 3.0.x
|
|
|
|
* * PHP 5.2.4 or greater
|
|
|
|
* * APC PHP extension
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* @package Kohana/Cache
|
|
|
|
* @category Base
|
|
|
|
* @author Kohana Team
|
2012-11-22 03:25:06 +00:00
|
|
|
* @copyright (c) 2009-2012 Kohana Team
|
2011-01-13 14:49:56 +00:00
|
|
|
* @license http://kohanaphp.com/license
|
|
|
|
*/
|
2012-11-22 03:25:06 +00:00
|
|
|
class Kohana_Cache_Apc extends Cache implements Cache_Arithmetic {
|
2011-01-13 14:49:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for existence of the APC extension This method cannot be invoked externally. The driver must
|
|
|
|
* be instantiated using the `Cache::instance()` method.
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @param array $config configuration
|
|
|
|
* @throws Cache_Exception
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
protected function __construct(array $config)
|
|
|
|
{
|
|
|
|
if ( ! extension_loaded('apc'))
|
|
|
|
{
|
2012-11-22 03:25:06 +00:00
|
|
|
throw new Cache_Exception('PHP APC extension is not available.');
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a cached value entry by id.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Retrieve cache entry from apc group
|
|
|
|
* $data = Cache::instance('apc')->get('foo');
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Retrieve cache entry from apc group and return 'bar' if miss
|
|
|
|
* $data = Cache::instance('apc')->get('foo', 'bar');
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @param string $id id of cache to entry
|
|
|
|
* @param string $default default value to return if cache miss
|
2011-01-13 14:49:56 +00:00
|
|
|
* @return mixed
|
2012-11-22 03:25:06 +00:00
|
|
|
* @throws Cache_Exception
|
2011-01-13 14:49:56 +00:00
|
|
|
*/
|
|
|
|
public function get($id, $default = NULL)
|
|
|
|
{
|
2011-05-16 12:47:16 +00:00
|
|
|
$data = apc_fetch($this->_sanitize_id($id), $success);
|
|
|
|
|
|
|
|
return $success ? $data : $default;
|
2011-01-13 14:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a value to cache with id and lifetime
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* $data = 'bar';
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Set 'bar' to 'foo' in apc group, using default expiry
|
|
|
|
* Cache::instance('apc')->set('foo', $data);
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Set 'bar' to 'foo' in apc group for 30 seconds
|
|
|
|
* Cache::instance('apc')->set('foo', $data, 30);
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @param string $id id of cache entry
|
|
|
|
* @param string $data data to set to cache
|
|
|
|
* @param integer $lifetime lifetime in seconds
|
2011-01-13 14:49:56 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a cache entry based on id
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Delete 'foo' entry from the apc group
|
|
|
|
* Cache::instance('apc')->delete('foo');
|
|
|
|
*
|
2012-11-22 03:25:06 +00:00
|
|
|
* @param string $id id to remove from cache
|
2011-01-13 14:49:56 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
|
|
|
return apc_delete($this->_sanitize_id($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all cache entries.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* Beware of using this method when
|
|
|
|
* using shared memory cache systems, as it will wipe every
|
|
|
|
* entry within the system for all clients.
|
2012-11-22 03:25:06 +00:00
|
|
|
*
|
2011-01-13 14:49:56 +00:00
|
|
|
* // Delete all cache entries in the apc group
|
|
|
|
* Cache::instance('apc')->delete_all();
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function delete_all()
|
|
|
|
{
|
|
|
|
return apc_clear_cache('user');
|
|
|
|
}
|
2012-11-22 03:25:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increments a given value by the step value supplied.
|
|
|
|
* Useful for shared counters and other persistent integer based
|
|
|
|
* tracking.
|
|
|
|
*
|
|
|
|
* @param string id of cache entry to increment
|
|
|
|
* @param int step value to increment by
|
|
|
|
* @return integer
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function increment($id, $step = 1)
|
|
|
|
{
|
|
|
|
return apc_inc($id, $step);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrements a given value by the step value supplied.
|
|
|
|
* Useful for shared counters and other persistent integer based
|
|
|
|
* tracking.
|
|
|
|
*
|
|
|
|
* @param string id of cache entry to decrement
|
|
|
|
* @param int step value to decrement by
|
|
|
|
* @return integer
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function decrement($id, $step = 1)
|
|
|
|
{
|
|
|
|
return apc_dec($id, $step);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End Kohana_Cache_Apc
|