Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php defined('SYSPATH') or die('No direct script access.');
- class Cache_File extends Kohana_Cache_File {
- /**
- * Retrieve a cached value entry by id.
- *
- * // Retrieve cache entry from file group
- * $data = Cache::instance('file')->get('foo');
- *
- * // Retrieve cache entry from file group and return 'bar' if miss
- * $data = Cache::instance('file')->get('foo', 'bar');
- *
- * @param string id of cache to entry
- * @param string default value to return if cache miss
- * @return mixed
- * @throws Cache_Exception
- */
- public function get($id, $default = NULL)
- {
- $filename = Cache_File::filename($this->_sanitize_id($id));
- $directory = $this->_resolve_directory($filename);
- // Wrap operations in try/catch to handle notices
- try
- {
- // Open file
- $file = new SplFileInfo($directory.$filename);
- // If file does not exist
- if ( ! $file->isFile())
- {
- // Return default value
- return $default;
- }
- else
- {
- // Open the file and parse data
- $created = $file->getMTime();
- $data = $file->openFile();
- $lifetime = $data->fgets();
- // If we're at the EOF at this point, corrupted!
- if ($data->eof())
- {
- throw new Cache_Exception(__METHOD__.' corrupted cache file!');
- }
- $cache = '';
- while ($data->eof() === FALSE)
- {
- $cache .= $data->fgets();
- }
- // Test the expiry
- if (($created + (int) $lifetime) < time())
- {
- if (Kohana::$is_windows === TRUE)
- {
- // $data is a SplFileObject with a lock on the file
- // Unsetting it appears to release the lock, allowing
- // the file to be deleted
- unset($data);
- }
- // Delete the file
- $this->_delete_file($file, NULL, TRUE);
- return $default;
- }
- else
- {
- return unserialize($cache);
- }
- }
- }
- catch (ErrorException $e)
- {
- // Handle ErrorException caused by failed unserialization
- if ($e->getCode() === E_NOTICE)
- {
- throw new Cache_Exception(__METHOD__.' failed to unserialize cached object with message : '.$e->getMessage());
- }
- // Otherwise throw the exception
- throw $e;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement