diff options
Diffstat (limited to 'kohana/libraries/drivers/Cache')
-rw-r--r-- | kohana/libraries/drivers/Cache/Apc.php | 53 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/Eaccelerator.php | 53 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/File.php | 245 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/Memcache.php | 78 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/Sqlite.php | 228 | ||||
-rw-r--r-- | kohana/libraries/drivers/Cache/Xcache.php | 116 |
6 files changed, 0 insertions, 773 deletions
diff --git a/kohana/libraries/drivers/Cache/Apc.php b/kohana/libraries/drivers/Cache/Apc.php deleted file mode 100644 index 1bf6beac..00000000 --- a/kohana/libraries/drivers/Cache/Apc.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * APC-based Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_Apc_Driver implements Cache_Driver { - - public function __construct() - { - if ( ! extension_loaded('apc')) - throw new Kohana_Exception('cache.extension_not_loaded', 'apc'); - } - - public function get($id) - { - return (($return = apc_fetch($id)) === FALSE) ? NULL : $return; - } - - public function set($id, $data, $tags, $lifetime) - { - count($tags) and Kohana::log('error', 'Cache: tags are unsupported by the APC driver'); - - return apc_store($id, $data, $lifetime); - } - - public function find($tag) - { - return FALSE; - } - - public function delete($id, $tag = FALSE) - { - if ($id === TRUE) - return apc_clear_cache('user'); - - if ($tag == FALSE) - return apc_delete($id); - - return TRUE; - } - - public function delete_expired() - { - return TRUE; - } - -} // End Cache APC Driver
\ No newline at end of file diff --git a/kohana/libraries/drivers/Cache/Eaccelerator.php b/kohana/libraries/drivers/Cache/Eaccelerator.php deleted file mode 100644 index f39be1ab..00000000 --- a/kohana/libraries/drivers/Cache/Eaccelerator.php +++ /dev/null @@ -1,53 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * Eaccelerator-based Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_Eaccelerator_Driver implements Cache_Driver { - - public function __construct() - { - if ( ! extension_loaded('eaccelerator')) - throw new Kohana_Exception('cache.extension_not_loaded', 'eaccelerator'); - } - - public function get($id) - { - return eaccelerator_get($id); - } - - public function find($tag) - { - return FALSE; - } - - public function set($id, $data, $tags, $lifetime) - { - count($tags) and Kohana::log('error', 'tags are unsupported by the eAccelerator driver'); - - return eaccelerator_put($id, $data, $lifetime); - } - - public function delete($id, $tag = FALSE) - { - if ($id === TRUE) - return eaccelerator_clean(); - - if ($tag == FALSE) - return eaccelerator_rm($id); - - return TRUE; - } - - public function delete_expired() - { - eaccelerator_gc(); - } - -} // End Cache eAccelerator Driver
\ No newline at end of file diff --git a/kohana/libraries/drivers/Cache/File.php b/kohana/libraries/drivers/Cache/File.php deleted file mode 100644 index 01d6c383..00000000 --- a/kohana/libraries/drivers/Cache/File.php +++ /dev/null @@ -1,245 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * File-based Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_File_Driver implements Cache_Driver { - - protected $directory = ''; - - /** - * Tests that the storage location is a directory and is writable. - */ - public function __construct($directory) - { - // Find the real path to the directory - $directory = str_replace('\\', '/', realpath($directory)).'/'; - - // Make sure the cache directory is writable - if ( ! is_dir($directory) OR ! is_writable($directory)) - throw new Kohana_Exception('cache.unwritable', $directory); - - // Directory is valid - $this->directory = $directory; - } - - /** - * Finds an array of files matching the given id or tag. - * - * @param string cache id or tag - * @param bool search for tags - * @return array of filenames matching the id or tag - * @return void if no matching files are found - */ - public function exists($id, $tag = FALSE) - { - if ($id === TRUE) - { - // Find all the files - $files = glob($this->directory.'*~*~*'); - } - elseif ($tag == TRUE) - { - // Find all the files that have the tag name - $files = glob($this->directory.'*~*'.$id.'*~*'); - - // Find all tags matching the given tag - foreach ($files as $i => $file) - { - // Split the files - $tags = explode('~', $file); - - // Find valid tags - if (count($tags) !== 3 OR empty($tags[1])) - continue; - - // Split the tags by plus signs, used to separate tags - $tags = explode('+', $tags[1]); - - if ( ! in_array($tag, $tags)) - { - // This entry does not match the tag - unset($files[$i]); - } - } - } - else - { - // Find all the files matching the given id - $files = glob($this->directory.$id.'~*'); - } - - return empty($files) ? NULL : $files; - } - - /** - * Sets a cache item to the given data, tags, and lifetime. - * - * @param string cache id to set - * @param string data in the cache - * @param array cache tags - * @param integer lifetime - * @return bool - */ - public function set($id, $data, $tags, $lifetime) - { - // Remove old cache files - $this->delete($id); - - // Cache File driver expects unix timestamp - if ($lifetime !== 0) - { - $lifetime += time(); - } - - // Construct the filename - $filename = $id.'~'.implode('+', $tags).'~'.$lifetime; - - // Write the file, appending the sha1 signature to the beginning of the data - return (bool) file_put_contents($this->directory.$filename, sha1($data).$data); - } - - /** - * Finds an array of ids for a given tag. - * - * @param string tag name - * @return array of ids that match the tag - */ - public function find($tag) - { - if ($files = $this->exists($tag, TRUE)) - { - // Length of directory name - $offset = strlen($this->directory); - - // Find all the files with the given tag - $array = array(); - foreach ($files as $file) - { - // Get the id from the filename - $array[] = substr(current(explode('~', $file)), $offset); - } - - return $array; - } - - return FALSE; - } - - /** - * Fetches a cache item. This will delete the item if it is expired or if - * the hash does not match the stored hash. - * - * @param string cache id - * @return mixed|NULL - */ - public function get($id) - { - if ($file = $this->exists($id)) - { - // Always process the first result - $file = current($file); - - // Validate that the cache has not expired - if ($this->expired($file)) - { - // Remove this cache, it has expired - $this->delete($id); - } - else - { - $data = file_get_contents($file); - - // Find the hash of the data - $hash = substr($data, 0, 40); - - // Remove the hash from the data - $data = substr($data, 40); - - if ($hash !== sha1($data)) - { - // Remove this cache, it doesn't validate - $this->delete($id); - - // Unset data to prevent it from being returned - unset($data); - } - } - } - - // Return NULL if there is no data - return isset($data) ? $data : NULL; - } - - /** - * Deletes a cache item by id or tag - * - * @param string cache id or tag, or TRUE for "all items" - * @param boolean use tags - * @return boolean - */ - public function delete($id, $tag = FALSE) - { - $files = $this->exists($id, $tag); - - if (empty($files)) - return FALSE; - - // Disable all error reporting while deleting - $ER = error_reporting(0); - - foreach ($files as $file) - { - // Remove the cache file - if ( ! unlink($file)) - Kohana::log('error', 'Cache: Unable to delete cache file: '.$file); - } - - // Turn on error reporting again - error_reporting($ER); - - return TRUE; - } - - /** - * Deletes all cache files that are older than the current time. - * - * @return void - */ - public function delete_expired() - { - if ($files = $this->exists(TRUE)) - { - foreach ($files as $file) - { - if ($this->expired($file)) - { - // The cache file has already expired, delete it - @unlink($file) or Kohana::log('error', 'Cache: Unable to delete cache file: '.$file); - } - } - } - } - - /** - * Check if a cache file has expired by filename. - * - * @param string filename - * @return bool - */ - protected function expired($file) - { - // Get the expiration time - $expires = (int) substr($file, strrpos($file, '~') + 1); - - // Expirations of 0 are "never expire" - return ($expires !== 0 AND $expires <= time()); - } - -} // End Cache File Driver
\ No newline at end of file diff --git a/kohana/libraries/drivers/Cache/Memcache.php b/kohana/libraries/drivers/Cache/Memcache.php deleted file mode 100644 index ef3e14e7..00000000 --- a/kohana/libraries/drivers/Cache/Memcache.php +++ /dev/null @@ -1,78 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * Memcache-based Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_Memcache_Driver implements Cache_Driver { - - // Cache backend object and flags - protected $backend; - protected $flags; - - public function __construct() - { - if ( ! extension_loaded('memcache')) - throw new Kohana_Exception('cache.extension_not_loaded', 'memcache'); - - $this->backend = new Memcache; - $this->flags = Kohana::config('cache_memcache.compression') ? MEMCACHE_COMPRESSED : 0; - - $servers = Kohana::config('cache_memcache.servers'); - - foreach ($servers as $server) - { - // Make sure all required keys are set - $server += array('host' => '127.0.0.1', 'port' => 11211, 'persistent' => FALSE); - - // Add the server to the pool - $this->backend->addServer($server['host'], $server['port'], (bool) $server['persistent']) - or Kohana::log('error', 'Cache: Connection failed: '.$server['host']); - } - } - - public function find($tag) - { - return FALSE; - } - - public function get($id) - { - return (($return = $this->backend->get($id)) === FALSE) ? NULL : $return; - } - - public function set($id, $data, $tags, $lifetime) - { - count($tags) and Kohana::log('error', 'Cache: Tags are unsupported by the memcache driver'); - - // Memcache driver expects unix timestamp - if ($lifetime !== 0) - { - $lifetime += time(); - } - - return $this->backend->set($id, $data, $this->flags, $lifetime); - } - - public function delete($id, $tag = FALSE) - { - if ($id === TRUE) - return $this->backend->flush(); - - if ($tag == FALSE) - return $this->backend->delete($id); - - return TRUE; - } - - public function delete_expired() - { - return TRUE; - } - -} // End Cache Memcache Driver diff --git a/kohana/libraries/drivers/Cache/Sqlite.php b/kohana/libraries/drivers/Cache/Sqlite.php deleted file mode 100644 index bc3ea7a7..00000000 --- a/kohana/libraries/drivers/Cache/Sqlite.php +++ /dev/null @@ -1,228 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * SQLite-based Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_Sqlite_Driver implements Cache_Driver { - - // SQLite database instance - protected $db; - - // Database error messages - protected $error; - - /** - * Logs an SQLite error. - */ - protected static function log_error($code) - { - // Log an error - Kohana::log('error', 'Cache: SQLite error: '.sqlite_error_string($error)); - } - - /** - * Tests that the storage location is a directory and is writable. - */ - public function __construct($filename) - { - // Get the directory name - $directory = str_replace('\\', '/', realpath(pathinfo($filename, PATHINFO_DIRNAME))).'/'; - - // Set the filename from the real directory path - $filename = $directory.basename($filename); - - // Make sure the cache directory is writable - if ( ! is_dir($directory) OR ! is_writable($directory)) - throw new Kohana_Exception('cache.unwritable', $directory); - - // Make sure the cache database is writable - if (is_file($filename) AND ! is_writable($filename)) - throw new Kohana_Exception('cache.unwritable', $filename); - - // Open up an instance of the database - $this->db = new SQLiteDatabase($filename, '0666', $error); - - // Throw an exception if there's an error - if ( ! empty($error)) - throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); - - $query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'caches'"; - $tables = $this->db->query($query, SQLITE_BOTH, $error); - - // Throw an exception if there's an error - if ( ! empty($error)) - throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); - - if ($tables->numRows() == 0) - { - Kohana::log('error', 'Cache: Initializing new SQLite cache database'); - - // Issue a CREATE TABLE command - $this->db->unbufferedQuery(Kohana::config('cache_sqlite.schema')); - } - } - - /** - * Checks if a cache id is already set. - * - * @param string cache id - * @return boolean - */ - public function exists($id) - { - // Find the id that matches - $query = "SELECT id FROM caches WHERE id = '$id'"; - - return ($this->db->query($query)->numRows() > 0); - } - - /** - * Sets a cache item to the given data, tags, and lifetime. - * - * @param string cache id to set - * @param string data in the cache - * @param array cache tags - * @param integer lifetime - * @return bool - */ - public function set($id, $data, $tags, $lifetime) - { - // Find the data hash - $hash = sha1($data); - - // Escape the data - $data = sqlite_escape_string($data); - - // Escape the tags - $tags = sqlite_escape_string(implode(',', $tags)); - - // Cache Sqlite driver expects unix timestamp - if ($lifetime !== 0) - { - $lifetime += time(); - } - - $query = $this->exists($id) - ? "UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$lifetime', cache = '$data' WHERE id = '$id'" - : "INSERT INTO caches VALUES('$id', '$hash', '$tags', '$lifetime', '$data')"; - - // Run the query - $this->db->unbufferedQuery($query, SQLITE_BOTH, $error); - - empty($error) or self::log_error($error); - - return empty($error); - } - - /** - * Finds an array of ids for a given tag. - * - * @param string tag name - * @return array of ids that match the tag - */ - public function find($tag) - { - $query = "SELECT id FROM caches WHERE tags LIKE '%{$tag}%'"; - $query = $this->db->query($query, SQLITE_BOTH, $error); - - empty($error) or self::log_error($error); - - if (empty($error) AND $query->numRows() > 0) - { - $array = array(); - while ($row = $query->fetchObject()) - { - // Add each id to the array - $array[] = $row->id; - } - return $array; - } - - return FALSE; - } - - /** - * Fetches a cache item. This will delete the item if it is expired or if - * the hash does not match the stored hash. - * - * @param string cache id - * @return mixed|NULL - */ - public function get($id) - { - $query = "SELECT id, hash, expiration, cache FROM caches WHERE id = '{$id}' LIMIT 0, 1"; - $query = $this->db->query($query, SQLITE_BOTH, $error); - - empty($error) or self::log_error($error); - - if (empty($error) AND $cache = $query->fetchObject()) - { - // Make sure the expiration is valid and that the hash matches - if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache->cache)) - { - // Cache is not valid, delete it now - $this->delete($cache->id); - } - else - { - // Return the valid cache data - return $cache->cache; - } - } - - // No valid cache found - return NULL; - } - - /** - * Deletes a cache item by id or tag - * - * @param string cache id or tag, or TRUE for "all items" - * @param bool use tags - * @return bool - */ - public function delete($id, $tag = FALSE) - { - if ($id === TRUE) - { - // Delete all caches - $where = '1'; - } - elseif ($tag == FALSE) - { - // Delete by id - $where = "id = '{$id}'"; - } - else - { - // Delete by tag - $where = "tags LIKE '%{$tag}%'"; - } - - $this->db->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH, $error); - - empty($error) or self::log_error($error); - - return empty($error); - } - - /** - * Deletes all cache files that are older than the current time. - */ - public function delete_expired() - { - // Delete all expired caches - $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time(); - - $this->db->unbufferedQuery($query); - - return TRUE; - } - -} // End Cache SQLite Driver
\ No newline at end of file diff --git a/kohana/libraries/drivers/Cache/Xcache.php b/kohana/libraries/drivers/Cache/Xcache.php deleted file mode 100644 index 98fec8bf..00000000 --- a/kohana/libraries/drivers/Cache/Xcache.php +++ /dev/null @@ -1,116 +0,0 @@ -<?php defined('SYSPATH') or die('No direct script access.'); -/** - * Xcache Cache driver. - * - * $Id$ - * - * @package Cache - * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html - */ -class Cache_Xcache_Driver implements Cache_Driver { - - public function __construct() - { - if ( ! extension_loaded('xcache')) - throw new Kohana_Exception('cache.extension_not_loaded', 'xcache'); - } - - public function get($id) - { - if (xcache_isset($id)) - return xcache_get($id); - - return NULL; - } - - public function set($id, $data, $tags, $lifetime) - { - count($tags) and Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver'); - - return xcache_set($id, $data, $lifetime); - } - - public function find($tag) - { - Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver'); - return FALSE; - } - - public function delete($id, $tag = FALSE) - { - if ($tag !== FALSE) - { - Kohana::log('error', 'Cache: tags are unsupported by the Xcache driver'); - return TRUE; - } - elseif ($id !== TRUE) - { - if (xcache_isset($id)) - return xcache_unset($id); - - return FALSE; - } - else - { - // Do the login - $this->auth(); - $result = TRUE; - for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) - { - if (xcache_clear_cache(XC_TYPE_VAR, $i) !== NULL) - { - $result = FALSE; - break; - } - } - - // Undo the login - $this->auth(TRUE); - return $result; - } - - return TRUE; - } - - public function delete_expired() - { - return TRUE; - } - - private function auth($reverse = FALSE) - { - static $backup = array(); - - $keys = array('PHP_AUTH_USER', 'PHP_AUTH_PW'); - - foreach ($keys as $key) - { - if ($reverse) - { - if (isset($backup[$key])) - { - $_SERVER[$key] = $backup[$key]; - unset($backup[$key]); - } - else - { - unset($_SERVER[$key]); - } - } - else - { - $value = getenv($key); - - if ( ! empty($value)) - { - $backup[$key] = $value; - } - - $_SERVER[$key] = Kohana::config('cache_xcache.'.$key); - } - } - } - -} // End Cache Xcache Driver |