Removed dep on API
[ninja.git] / system / libraries / drivers / Cache / File.php
blobb7a0646055d84956fab8c1c4b21a576024c6aa40
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * File-based Cache driver.
5 * $Id: File.php 3917 2009-01-21 03:06:22Z zombor $
7 * @package Cache
8 * @author Kohana Team
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class Cache_File_Driver implements Cache_Driver {
14 protected $directory = '';
16 /**
17 * Tests that the storage location is a directory and is writable.
19 public function __construct($directory)
21 // Find the real path to the directory
22 $directory = str_replace('\\', '/', realpath($directory)).'/';
24 // Make sure the cache directory is writable
25 if ( ! is_dir($directory) OR ! is_writable($directory))
26 throw new Kohana_Exception('cache.unwritable', $directory);
28 // Directory is valid
29 $this->directory = $directory;
32 /**
33 * Finds an array of files matching the given id or tag.
35 * @param string cache id or tag
36 * @param bool search for tags
37 * @return array of filenames matching the id or tag
38 * @return void if no matching files are found
40 public function exists($id, $tag = FALSE)
42 if ($id === TRUE)
44 // Find all the files
45 $files = glob($this->directory.'*~*~*');
47 elseif ($tag == TRUE)
49 // Find all the files that have the tag name
50 $files = glob($this->directory.'*~*'.$id.'*~*');
52 // Find all tags matching the given tag
53 foreach ($files as $i => $file)
55 // Split the files
56 $tags = explode('~', $file);
58 // Find valid tags
59 if (count($tags) !== 3 OR empty($tags[1]))
60 continue;
62 // Split the tags by plus signs, used to separate tags
63 $tags = explode('+', $tags[1]);
65 if ( ! in_array($tag, $tags))
67 // This entry does not match the tag
68 unset($files[$i]);
72 else
74 // Find all the files matching the given id
75 $files = glob($this->directory.$id.'~*');
78 return empty($files) ? NULL : $files;
81 /**
82 * Sets a cache item to the given data, tags, and lifetime.
84 * @param string cache id to set
85 * @param string data in the cache
86 * @param array cache tags
87 * @param integer lifetime
88 * @return bool
90 public function set($id, $data, $tags, $lifetime)
92 // Remove old cache files
93 $this->delete($id);
95 // Cache File driver expects unix timestamp
96 if ($lifetime !== 0)
98 $lifetime += time();
101 // Construct the filename
102 $filename = $id.'~'.implode('+', $tags).'~'.$lifetime;
104 // Write the file, appending the sha1 signature to the beginning of the data
105 return (bool) file_put_contents($this->directory.$filename, sha1($data).$data);
109 * Finds an array of ids for a given tag.
111 * @param string tag name
112 * @return array of ids that match the tag
114 public function find($tag)
116 if ($files = $this->exists($tag, TRUE))
118 // Length of directory name
119 $offset = strlen($this->directory);
121 // Find all the files with the given tag
122 $array = array();
123 foreach ($files as $file)
125 // Get the id from the filename
126 $array[] = substr(current(explode('~', $file)), $offset);
129 return $array;
132 return FALSE;
136 * Fetches a cache item. This will delete the item if it is expired or if
137 * the hash does not match the stored hash.
139 * @param string cache id
140 * @return mixed|NULL
142 public function get($id)
144 if ($file = $this->exists($id))
146 // Always process the first result
147 $file = current($file);
149 // Validate that the cache has not expired
150 if ($this->expired($file))
152 // Remove this cache, it has expired
153 $this->delete($id);
155 else
157 $data = file_get_contents($file);
159 // Find the hash of the data
160 $hash = substr($data, 0, 40);
162 // Remove the hash from the data
163 $data = substr($data, 40);
165 if ($hash !== sha1($data))
167 // Remove this cache, it doesn't validate
168 $this->delete($id);
170 // Unset data to prevent it from being returned
171 unset($data);
176 // Return NULL if there is no data
177 return isset($data) ? $data : NULL;
181 * Deletes a cache item by id or tag
183 * @param string cache id or tag, or TRUE for "all items"
184 * @param boolean use tags
185 * @return boolean
187 public function delete($id, $tag = FALSE)
189 $files = $this->exists($id, $tag);
191 if (empty($files))
192 return FALSE;
194 // Disable all error reporting while deleting
195 $ER = error_reporting(0);
197 foreach ($files as $file)
199 // Remove the cache file
200 if ( ! unlink($file))
201 Kohana::log('error', 'Cache: Unable to delete cache file: '.$file);
204 // Turn on error reporting again
205 error_reporting($ER);
207 return TRUE;
211 * Deletes all cache files that are older than the current time.
213 * @return void
215 public function delete_expired()
217 if ($files = $this->exists(TRUE))
219 foreach ($files as $file)
221 if ($this->expired($file))
223 // The cache file has already expired, delete it
224 @unlink($file) or Kohana::log('error', 'Cache: Unable to delete cache file: '.$file);
231 * Check if a cache file has expired by filename.
233 * @param string filename
234 * @return bool
236 protected function expired($file)
238 // Get the expiration time
239 $expires = (int) substr($file, strrpos($file, '~') + 1);
241 // Expirations of 0 are "never expire"
242 return ($expires !== 0 AND $expires <= time());
245 } // End Cache File Driver