From eba717f95f586d2538007bd18da6e9b32b076c30 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 31 Oct 2008 22:12:14 +0000 Subject: Merge over vendor code. git-svn-id: http://gallery.svn.sourceforge.net/svnroot/gallery/trunk/eval/gx/gallery3/trunk@18408 57fcd75e-5312-0410-8df3-f5eb6fbb1595 --- kohana/libraries/drivers/Cache/Apc.php | 53 +++++ kohana/libraries/drivers/Cache/Eaccelerator.php | 53 +++++ kohana/libraries/drivers/Cache/File.php | 245 ++++++++++++++++++++++++ kohana/libraries/drivers/Cache/Memcache.php | 78 ++++++++ kohana/libraries/drivers/Cache/Sqlite.php | 228 ++++++++++++++++++++++ kohana/libraries/drivers/Cache/Xcache.php | 116 +++++++++++ 6 files changed, 773 insertions(+) create mode 100644 kohana/libraries/drivers/Cache/Apc.php create mode 100644 kohana/libraries/drivers/Cache/Eaccelerator.php create mode 100644 kohana/libraries/drivers/Cache/File.php create mode 100644 kohana/libraries/drivers/Cache/Memcache.php create mode 100644 kohana/libraries/drivers/Cache/Sqlite.php create mode 100644 kohana/libraries/drivers/Cache/Xcache.php (limited to 'kohana/libraries/drivers/Cache') diff --git a/kohana/libraries/drivers/Cache/Apc.php b/kohana/libraries/drivers/Cache/Apc.php new file mode 100644 index 00000000..1bf6beac --- /dev/null +++ b/kohana/libraries/drivers/Cache/Apc.php @@ -0,0 +1,53 @@ +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 new file mode 100644 index 00000000..ef3e14e7 --- /dev/null +++ b/kohana/libraries/drivers/Cache/Memcache.php @@ -0,0 +1,78 @@ +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 new file mode 100644 index 00000000..bc3ea7a7 --- /dev/null +++ b/kohana/libraries/drivers/Cache/Sqlite.php @@ -0,0 +1,228 @@ +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 new file mode 100644 index 00000000..98fec8bf --- /dev/null +++ b/kohana/libraries/drivers/Cache/Xcache.php @@ -0,0 +1,116 @@ +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 -- cgit v1.2.3