Changeset 1421
- Timestamp:
- 12/05/2007 12:41:08 AM (13 months ago)
- Location:
- trunk/system
- Files:
-
- 1 added
- 2 modified
-
config/cache_sqlite.php (added)
-
i18n/en_US/cache.php (modified) (1 diff)
-
libraries/drivers/Cache_Sqlite.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/system/i18n/en_US/cache.php
r1297 r1421 6 6 'unwritable' => 'The configured storage location, <tt>%s</tt>, is not writable.', 7 7 'resources' => 'Caching of resources is impossible, because resources cannot be serialized.', 8 'driver_error' => '%s', 8 9 ); -
trunk/system/libraries/drivers/Cache_Sqlite.php
r1420 r1421 17 17 18 18 /** 19 * Logs an SQLite error. 20 */ 21 protected static function log_error($code) 22 { 23 // Log an error 24 Log::add('error', 'Cache: SQLite error: '.sqlite_error_string($error)); 25 } 26 27 /** 19 28 * Tests that the storage location is a directory and is writable. 20 29 */ … … 23 32 // Find the real path to the directory 24 33 $filename = str_replace('\\', '/', realpath($filename)); 25 26 if ( ! is_file($filename) OR ! is_writable($filename))27 throw new Kohana_Exception('cache.unwritable', $filename);28 34 29 35 // Get the filename from the directory … … 34 40 throw new Kohana_Exception('cache.unwritable', $directory); 35 41 36 // Open the database37 $this->db = sqlite_factory($filename, '0666', $error);42 // Open up an instance of the database 43 $this->db = new SQLiteDatabase($filename, '0666', $error); 38 44 39 45 // Throw an exception if there's an error … … 41 47 throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); 42 48 43 // Directory is valid 44 $this->directory = $directory; 49 $query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'caches'"; 50 $tables = $this->db->query($query, SQLITE_BOTH, $error); 51 52 // Throw an exception if there's an error 53 if ( ! empty($error)) 54 throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error)); 55 56 if ($tables->numRows() == 0) 57 { 58 Log::add('error', 'Cache: Initializing new SQLite cache database'); 59 60 // Issue a CREATE TABLE command 61 $this->db->unbufferedQuery(Config::item('cache_sqlite.schema')); 62 } 45 63 } 46 64 … … 54 72 { 55 73 // Find the id that matches 56 $query = $this->db->query('SELECT id FROM caches WHERE id = "'.$id.'"', SQLITE_BOTH, $error);57 58 return ($ query->numRows() > 0);74 $query = "SELECT id FROM caches WHERE id = '$id'"; 75 76 return ($this->db->query($query)->numRows() > 0); 59 77 } 60 78 … … 79 97 $tags = sqlite_escape_string(implode(',', $tags)); 80 98 81 if ($this->exists($id)) 82 { 83 $this->db->unbufferedQuery("UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$expiration', data = '$data' WHERE id = '$id'", SQLITE_BOTH, $error); 84 } 85 else 86 { 87 $this->db->unbufferedQuery("INSERT INTO caches VALUES('$id', '$hash', '$tags', '$expiration', '$data')", SQLITE_BOTH, $error); 88 } 89 90 // Log errors 91 empty($error) or Log::add('error', 'Cache: unable to write '.$id.' to cache database'); 99 $query = $this->exists($id) 100 ? "UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$expiration', cache = '$data' WHERE id = '$id'" 101 : "INSERT INTO caches VALUES('$id', '$hash', '$tags', '$expiration', '$data')"; 102 103 // Run the query 104 $this->db->unbufferedQuery($query, SQLITE_BOTH, $error); 105 106 empty($error) or self::log_error($error); 92 107 93 108 return empty($error); … … 102 117 public function find($tag) 103 118 { 104 $query = $this->db->query("SELECT id FROM caches WHERE tags LIKE '%$tag%'", SQLITE_BOTH, $error); 119 $query = "SELECT id FROM caches WHERE tags LIKE '%{$tag}%'"; 120 $query = $this->db->query($query, SQLITE_BOTH, $error); 121 122 empty($error) or self::log_error($error); 105 123 106 124 if (empty($error) AND $query->numRows() > 0) … … 127 145 public function get($id) 128 146 { 129 $query = $this->db->unbufferedQuery("SELECT id, hash, expiration, data FROM caches WHERE id = '$id' LIMIT 1", SQLITE_BOTH, $error); 147 $query = "SELECT id, hash, expiration, cache FROM caches WHERE id = '{$id}' LIMIT 0, 1"; 148 $query = $this->db->query($query, SQLITE_BOTH, $error); 149 150 empty($error) or self::log_error($error); 130 151 131 152 if (empty($error) AND $cache = $query->fetchObject()) 132 153 { 133 154 // Make sure the expiration is valid and that the hash matches 134 if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache-> data))155 if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache->cache)) 135 156 { 136 // Cache is not valid 157 // Cache is not valid, delete it now 137 158 $this->del($cache->id); 138 return NULL;139 159 } 140 } 141 else 142 { 143 // Nothing found 144 return NULL; 145 } 146 147 return $cache->data; 160 else 161 { 162 // Return the valid cache data 163 return $cache->cache; 164 } 165 } 166 167 // No valid cache foud 168 return NULL; 148 169 } 149 170 … … 160 181 { 161 182 // Delete all caches 162 $ this->db->unbufferedQuery('DELETE FROM caches WHERE 1', SQLITE_BOTH, $error);183 $where = '1'; 163 184 } 164 185 elseif ($tag == FALSE) 165 186 { 166 187 // Delete by id 167 $ this->db->unbufferedQuery('DELETE FROM caches WHERE id = "'.$id.'"', SQLITE_BOTH, $error);188 $where = "id = '{$id}'"; 168 189 } 169 190 else 170 191 { 171 // Delete by tags 172 $this->db->unbufferedQuery("DELETE FROM caches WHERE tags LIKE '%$tag%'", SQLITE_BOTH, $error); 173 } 174 175 // Log errors 176 empty($error) or Log::add('error', 'Cache: Unable to delete cache: '.$id); 192 // Delete by tag 193 $where = "tags LIKE '%{$tag}%'"; 194 } 195 196 $this->db->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH, $error); 197 198 empty($error) or self::log_error($error); 177 199 178 200 return empty($error); … … 185 207 { 186 208 // Delete all expired caches 187 $this->db->unbufferedQuery('DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time(), SQLITE_BOTH, $error); 209 $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time(); 210 211 $this->db->unbufferedQuery($query); 188 212 189 213 return TRUE;
