1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 * SQLite-based Cache driver.
5 * $Id: Sqlite.php 4046 2009-03-05 19:23:29Z Shadowhand $
9 * @copyright (c) 2007-2008 Kohana Team
10 * @license http://kohanaphp.com/license.html
12 class Cache_Sqlite_Driver implements Cache_Driver {
14 // SQLite database instance
17 // Database error messages
21 * Logs an SQLite error.
23 protected static function log_error($code)
26 Kohana::log('error', 'Cache: SQLite error: '.sqlite_error_string($error));
30 * Tests that the storage location is a directory and is writable.
32 public function __construct($filename)
34 // Get the directory name
35 $directory = str_replace('\\', '/', realpath(pathinfo($filename, PATHINFO_DIRNAME))).'/';
37 // Set the filename from the real directory path
38 $filename = $directory.basename($filename);
40 // Make sure the cache directory is writable
41 if ( ! is_dir($directory) OR ! is_writable($directory))
42 throw new Kohana_Exception('cache.unwritable', $directory);
44 // Make sure the cache database is writable
45 if (is_file($filename) AND ! is_writable($filename))
46 throw new Kohana_Exception('cache.unwritable', $filename);
48 // Open up an instance of the database
49 $this->db = new SQLiteDatabase($filename, '0666', $error);
51 // Throw an exception if there's an error
53 throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error));
55 $query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'caches'";
56 $tables = $this->db->query($query, SQLITE_BOTH, $error);
58 // Throw an exception if there's an error
60 throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error));
62 if ($tables->numRows() == 0)
64 Kohana::log('error', 'Cache: Initializing new SQLite cache database');
66 // Issue a CREATE TABLE command
67 $this->db->unbufferedQuery(Kohana::config('cache_sqlite.schema'));
72 * Checks if a cache id is already set.
74 * @param string cache id
77 public function exists($id)
79 // Find the id that matches
80 $query = "SELECT id FROM caches WHERE id = '$id'";
82 return ($this->db->query($query)->numRows() > 0);
86 * Sets a cache item to the given data, tags, and lifetime.
88 * @param string cache id to set
89 * @param string data in the cache
90 * @param array cache tags
91 * @param integer lifetime
94 public function set($id, $data, array $tags = NULL, $lifetime)
96 // Serialize and escape the data
97 $data = sqlite_escape_string(serialize($data));
101 // Escape the tags, adding brackets so the tag can be explicitly matched
102 $tags = sqlite_escape_string('<'.implode('>,<', $tags).'>');
105 // Cache Sqlite driver expects unix timestamp
111 $query = $this->exists($id)
112 ? "UPDATE caches SET tags = '$tags', expiration = '$lifetime', cache = '$data' WHERE id = '$id'"
113 : "INSERT INTO caches VALUES('$id', '$tags', '$lifetime', '$data')";
116 $this->db->unbufferedQuery($query, SQLITE_BOTH, $error);
118 if ( ! empty($error))
120 self::log_error($error);
130 * Finds an array of ids for a given tag.
132 * @param string tag name
133 * @return array of ids that match the tag
135 public function find($tag)
137 $query = "SELECT id,cache FROM caches WHERE tags LIKE '%<{$tag}>%'";
138 $query = $this->db->query($query, SQLITE_BOTH, $error);
140 // An array will always be returned
143 if ( ! empty($error))
145 self::log_error($error);
147 elseif ($query->numRows() > 0)
149 // Disable notices for unserializing
150 $ER = error_reporting(~E_NOTICE);
152 while ($row = $query->fetchObject())
154 // Add each cache to the array
155 $result[$row->id] = unserialize($row->cache);
158 // Turn notices back on
159 error_reporting($ER);
166 * Fetches a cache item. This will delete the item if it is expired or if
167 * the hash does not match the stored hash.
169 * @param string cache id
172 public function get($id)
174 $query = "SELECT id, expiration, cache FROM caches WHERE id = '$id' LIMIT 0, 1";
175 $query = $this->db->query($query, SQLITE_BOTH, $error);
177 if ( ! empty($error))
179 self::log_error($error);
181 elseif ($cache = $query->fetchObject())
183 // Make sure the expiration is valid and that the hash matches
184 if ($cache->expiration != 0 AND $cache->expiration <= time())
186 // Cache is not valid, delete it now
187 $this->delete($cache->id);
191 // Disable notices for unserializing
192 $ER = error_reporting(~E_NOTICE);
194 // Return the valid cache data
195 $data = $cache->cache;
197 // Turn notices back on
198 error_reporting($ER);
202 // No valid cache found
207 * Deletes a cache item by id or tag
209 * @param string cache id or tag, or TRUE for "all items"
210 * @param bool delete a tag
213 public function delete($id, $tag = FALSE)
220 elseif ($tag === TRUE)
223 $where = "tags LIKE '%<{$id}>%'";
228 $where = "id = '$id'";
231 $this->db->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH, $error);
233 if ( ! empty($error))
235 self::log_error($error);
245 * Deletes all cache files that are older than the current time.
247 public function delete_expired()
249 // Delete all expired caches
250 $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time();
252 $this->db->unbufferedQuery($query);
257 } // End Cache SQLite Driver