diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-26 11:24:50 -0800 |
commit | 3060a6f662da66008d57a461bf1c9b5b4aa2b002 (patch) | |
tree | 442fd290505817efc0324f2af6e01805cb7396aa /system/libraries/drivers/Cache/File.php | |
parent | 1cd6a615bb47a33794e4a4f690c87a348ab752d7 (diff) | |
parent | 32d25dafd5b033338b6a9bb8c7c53edab462543a (diff) |
Merge branch 'master' into talmdal_dev
Conflicts:
modules/gallery/controllers/albums.php
modules/gallery/controllers/movies.php
modules/gallery/controllers/photos.php
Diffstat (limited to 'system/libraries/drivers/Cache/File.php')
-rw-r--r-- | system/libraries/drivers/Cache/File.php | 268 |
1 files changed, 131 insertions, 137 deletions
diff --git a/system/libraries/drivers/Cache/File.php b/system/libraries/drivers/Cache/File.php index cc9d48d3..d6ec0378 100644 --- a/system/libraries/drivers/Cache/File.php +++ b/system/libraries/drivers/Cache/File.php @@ -1,55 +1,54 @@ <?php defined('SYSPATH') OR die('No direct access allowed.'); /** - * File-based Cache driver. + * Memcache-based Cache driver. * - * $Id: File.php 4046 2009-03-05 19:23:29Z Shadowhand $ + * $Id: File.php 4605 2009-09-14 17:22:21Z kiall $ * * @package Cache * @author Kohana Team - * @copyright (c) 2007-2008 Kohana Team - * @license http://kohanaphp.com/license.html + * @copyright (c) 2007-2009 Kohana Team + * @license http://kohanaphp.com/license */ -class Cache_File_Driver implements Cache_Driver { +class Cache_File_Driver extends Cache_Driver { + protected $config; + protected $backend; - protected $directory = ''; - - /** - * Tests that the storage location is a directory and is writable. - */ - public function __construct($directory) + public function __construct($config) { - // Find the real path to the directory - $directory = str_replace('\\', '/', realpath($directory)).'/'; + $this->config = $config; + $this->config['directory'] = str_replace('\\', '/', realpath($this->config['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; + if ( ! is_dir($this->config['directory']) OR ! is_writable($this->config['directory'])) + throw new Cache_Exception('The configured cache directory, :directory:, is not writable.', array(':directory:' => $this->config['directory'])); } /** * Finds an array of files matching the given id or tag. * - * @param string cache id or tag + * @param string cache key or tag * @param bool search for tags * @return array of filenames matching the id or tag */ - public function exists($id, $tag = FALSE) + public function exists($keys, $tag = FALSE) { - if ($id === TRUE) + if ($keys === TRUE) { // Find all the files - return glob($this->directory.'*~*~*'); + return glob($this->config['directory'].'*~*~*'); } elseif ($tag === TRUE) { // Find all the files that have the tag name - $paths = glob($this->directory.'*~*'.$id.'*~*'); + $paths = array(); + + foreach ( (array) $keys as $tag) + { + $paths = array_merge($paths, glob($this->config['directory'].'*~*'.$tag.'*~*')); + } // Find all tags matching the given tag $files = array(); + foreach ($paths as $path) { // Split the files @@ -60,12 +59,16 @@ class Cache_File_Driver implements Cache_Driver { continue; // Split the tags by plus signs, used to separate tags - $tags = explode('+', $tags[1]); + $item_tags = explode('+', $tags[1]); - if (in_array($tag, $tags)) + // Check each supplied tag, and match aginst the tags on each item. + foreach ($keys as $tag) { - // Add the file to the array, it has the requested tag - $files[] = $path; + if (in_array($tag, $item_tags)) + { + // Add the file to the array, it has the requested tag + $files[] = $path; + } } } @@ -73,98 +76,68 @@ class Cache_File_Driver implements Cache_Driver { } else { - // Find the file matching the given id - return glob($this->directory.$id.'~*'); + $paths = array(); + + foreach ( (array) $keys as $key) + { + // Find the file matching the given key + $paths = array_merge($paths, glob($this->config['directory'].str_replace(array('/', '\\', ' '), '_', $key).'~*')); + } + + return $paths; } } - /** - * 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, array $tags = NULL, $lifetime) + public function set($items, $tags = NULL, $lifetime = NULL) { - // Remove old cache files - $this->delete($id); - - // Cache File driver expects unix timestamp if ($lifetime !== 0) { + // File driver expects unix timestamp $lifetime += time(); } - if ( ! empty($tags)) + + if ( ! is_null($tags) AND ! empty($tags)) { // Convert the tags into a string list - $tags = implode('+', $tags); + $tags = implode('+', (array) $tags); } - // Write out a serialized cache - return (bool) file_put_contents($this->directory.$id.'~'.$tags.'~'.$lifetime, serialize($data)); - } + $success = TRUE; - /** - * 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) - { - // An array will always be returned - $result = array(); - - if ($paths = $this->exists($tag, TRUE)) + foreach ($items as $key => $value) { - // Length of directory name - $offset = strlen($this->directory); + if (is_resource($value)) + throw new Cache_Exception('Caching of resources is impossible, because resources cannot be serialised.'); - // Find all the files with the given tag - foreach ($paths as $path) - { - // Get the id from the filename - list($id, $junk) = explode('~', basename($path), 2); + // Remove old cache file + $this->delete($key); - if (($data = $this->get($id)) !== FALSE) - { - // Add the result to the array - $result[$id] = $data; - } + if ( ! (bool) file_put_contents($this->config['directory'].str_replace(array('/', '\\', ' '), '_', $key).'~'.$tags.'~'.$lifetime, serialize($value))) + { + $success = FALSE; } } - return $result; + return $success; } - /** - * 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) + public function get($keys, $single = FALSE) { - if ($file = $this->exists($id)) + $items = array(); + + if ($files = $this->exists($keys)) { - // Use the first file - $file = current($file); + // Turn off errors while reading the files + $ER = error_reporting(0); - // Validate that the cache has not expired - if ($this->expired($file)) - { - // Remove this cache, it has expired - $this->delete($id); - } - else + foreach ($files as $file) { - // Turn off errors while reading the file - $ER = error_reporting(0); + // Validate that the item has not expired + if ($this->expired($file)) + continue; + + list($key, $junk) = explode('~', basename($file), 2); if (($data = file_get_contents($file)) !== FALSE) { @@ -173,80 +146,102 @@ class Cache_File_Driver implements Cache_Driver { } else { - // Delete the data - unset($data); + $data = NULL; } - // Turn errors back on - error_reporting($ER); + $items[$key] = $data; } + + // Turn errors back on + error_reporting($ER); } - // Return NULL if there is no data - return isset($data) ? $data : NULL; + // Reutrn a single item if only one key was requested + if ($single) + { + return (count($items) > 0) ? current($items) : NULL; + } + else + { + return $items; + } } /** - * 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 + * Get cache items by tag + */ + public function get_tag($tags) + { + // An array will always be returned + $result = array(); + + if ($paths = $this->exists($tags, TRUE)) + { + // Find all the files with the given tag + foreach ($paths as $path) + { + // Get the id from the filename + list($key, $junk) = explode('~', basename($path), 2); + + if (($data = $this->get($key, TRUE)) !== FALSE) + { + // Add the result to the array + $result[$key] = $data; + } + } + } + + return $result; + } + + /** + * Delete cache items by keys or tags */ - public function delete($id, $tag = FALSE) + public function delete($keys, $tag = FALSE) { - $files = $this->exists($id, $tag); + $success = TRUE; - if (empty($files)) - return FALSE; + $paths = $this->exists($keys, $tag); // Disable all error reporting while deleting $ER = error_reporting(0); - foreach ($files as $file) + foreach ($paths as $path) { // Remove the cache file - if ( ! unlink($file)) - Kohana::log('error', 'Cache: Unable to delete cache file: '.$file); + if ( ! unlink($path)) + { + Kohana_Log::add('error', 'Cache: Unable to delete cache file: '.$path); + $success = FALSE; + } } // Turn on error reporting again error_reporting($ER); - return TRUE; + return $success; } /** - * Deletes all cache files that are older than the current time. - * - * @return void + * Delete cache items by tag */ - public function delete_expired() + public function delete_tag($tags) { - if ($files = $this->exists(TRUE)) - { - // Disable all error reporting while deleting - $ER = error_reporting(0); - - foreach ($files as $file) - { - if ($this->expired($file)) - { - // The cache file has already expired, delete it - if ( ! unlink($file)) - Kohana::log('error', 'Cache: Unable to delete cache file: '.$file); - } - } + return $this->delete($tags, TRUE); + } - // Turn on error reporting again - error_reporting($ER); - } + /** + * Empty the cache + */ + public function delete_all() + { + return $this->delete(TRUE); } /** * Check if a cache file has expired by filename. * - * @param string filename + * @param string|array array of filenames * @return bool */ protected function expired($file) @@ -257,5 +252,4 @@ class Cache_File_Driver implements Cache_Driver { // Expirations of 0 are "never expire" return ($expires !== 0 AND $expires <= time()); } - -} // End Cache File Driver
\ No newline at end of file +} // End Cache Memcache Driver |