1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
3 * SQLite-based Cache driver.
5 * $Id: Sqlite.php 3917 2009-01-21 03:06:22Z zombor $
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, $tags, $lifetime)
100 $data = sqlite_escape_string($data);
103 $tags = sqlite_escape_string(implode(',', $tags));
105 // Cache Sqlite driver expects unix timestamp
111 $query = $this->exists($id)
112 ?
"UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$lifetime', cache = '$data' WHERE id = '$id'"
113 : "INSERT INTO caches VALUES('$id', '$hash', '$tags', '$lifetime', '$data')";
116 $this->db
->unbufferedQuery($query, SQLITE_BOTH
, $error);
118 empty($error) or self
::log_error($error);
120 return empty($error);
124 * Finds an array of ids for a given tag.
126 * @param string tag name
127 * @return array of ids that match the tag
129 public function find($tag)
131 $query = "SELECT id FROM caches WHERE tags LIKE '%{$tag}%'";
132 $query = $this->db
->query($query, SQLITE_BOTH
, $error);
134 empty($error) or self
::log_error($error);
136 if (empty($error) AND $query->numRows() > 0)
139 while ($row = $query->fetchObject())
141 // Add each id to the array
151 * Fetches a cache item. This will delete the item if it is expired or if
152 * the hash does not match the stored hash.
154 * @param string cache id
157 public function get($id)
159 $query = "SELECT id, hash, expiration, cache FROM caches WHERE id = '{$id}' LIMIT 0, 1";
160 $query = $this->db
->query($query, SQLITE_BOTH
, $error);
162 empty($error) or self
::log_error($error);
164 if (empty($error) AND $cache = $query->fetchObject())
166 // Make sure the expiration is valid and that the hash matches
167 if (($cache->expiration
!= 0 AND $cache->expiration
<= time()) OR $cache->hash
!== sha1($cache->cache
))
169 // Cache is not valid, delete it now
170 $this->delete($cache->id
);
174 // Return the valid cache data
175 return $cache->cache
;
179 // No valid cache found
184 * Deletes a cache item by id or tag
186 * @param string cache id or tag, or TRUE for "all items"
187 * @param bool use tags
190 public function delete($id, $tag = FALSE)
197 elseif ($tag == FALSE)
200 $where = "id = '{$id}'";
205 $where = "tags LIKE '%{$tag}%'";
208 $this->db
->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH
, $error);
210 empty($error) or self
::log_error($error);
212 return empty($error);
216 * Deletes all cache files that are older than the current time.
218 public function delete_expired()
220 // Delete all expired caches
221 $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time();
223 $this->db
->unbufferedQuery($query);
228 } // End Cache SQLite Driver