/usr/share/php/kohana3.1/modules/cache/classes/kohana/cache/sqlite.php is in libkohana3.1-mod-cache-php 3.1.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | <?php defined('SYSPATH') or die('No direct script access.');
/**
* Kohana Cache Sqlite Driver
*
* Requires SQLite3 and PDO
*
* @package Kohana/Cache
* @category Base
* @author Kohana Team
* @copyright (c) 2009-2010 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Cache_Sqlite extends Cache implements Kohana_Cache_Tagging, Kohana_Cache_GarbageCollect {
/**
* Database resource
*
* @var PDO
*/
protected $_db;
/**
* Sets up the PDO SQLite table and
* initialises the PDO connection
*
* @param array configuration
* @throws Kohana_Cache_Exception
*/
protected function __construct(array $config)
{
parent::__construct($config);
$database = Arr::get($this->_config, 'database', NULL);
if ($database === NULL)
{
throw new Kohana_Cache_Exception('Database path not available in Kohana Cache configuration');
}
// Load new Sqlite DB
$this->_db = new PDO('sqlite:'.$database);
// Test for existing DB
$result = $this->_db->query("SELECT * FROM sqlite_master WHERE name = 'caches' AND type = 'table'")->fetchAll();
// If there is no table, create a new one
if (0 == count($result))
{
$database_schema = Arr::get($this->_config, 'schema', NULL);
if ($database_schema === NULL)
{
throw new Kohana_Cache_Exception('Database schema not found in Kohana Cache configuration');
}
try
{
// Create the caches table
$this->_db->query(Arr::get($this->_config, 'schema', NULL));
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('Failed to create new SQLite caches table with the following error : :error', array(':error' => $e->getMessage()));
}
}
}
/**
* Retrieve a value based on an id
*
* @param string id
* @param string default [Optional] Default value to return if id not found
* @return mixed
* @throws Kohana_Cache_Exception
*/
public function get($id, $default = NULL)
{
// Prepare statement
$statement = $this->_db->prepare('SELECT id, expiration, cache FROM caches WHERE id = :id LIMIT 0, 1');
// Try and load the cache based on id
try
{
$statement->execute(array(':id' => $this->_sanitize_id($id)));
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
if ( ! $result = $statement->fetch(PDO::FETCH_OBJ))
{
return $default;
}
// If the cache has expired
if ($result->expiration != 0 and $result->expiration <= time())
{
// Delete it and return default value
$this->delete($id);
return $default;
}
// Otherwise return cached object
else
{
// Disable notices for unserializing
$ER = error_reporting(~E_NOTICE);
// Return the valid cache data
$data = unserialize($result->cache);
// Turn notices back on
error_reporting($ER);
// Return the resulting data
return $data;
}
}
/**
* Set a value based on an id. Optionally add tags.
*
* @param string id
* @param mixed data
* @param integer lifetime [Optional]
* @return boolean
*/
public function set($id, $data, $lifetime = NULL)
{
return (bool) $this->set_with_tags($id, $data, $lifetime);
}
/**
* Delete a cache entry based on id
*
* @param string id
* @param integer timeout [Optional]
* @return boolean
* @throws Kohana_Cache_Exception
*/
public function delete($id)
{
// Prepare statement
$statement = $this->_db->prepare('DELETE FROM caches WHERE id = :id');
// Remove the entry
try
{
$statement->execute(array(':id' => $this->_sanitize_id($id)));
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
return (bool) $statement->rowCount();
}
/**
* Delete all cache entries
*
* @return boolean
*/
public function delete_all()
{
// Prepare statement
$statement = $this->_db->prepare('DELETE FROM caches');
// Remove the entry
try
{
$statement->execute();
}
catch (PDOException $e)
{
throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
return (bool) $statement->rowCount();
}
/**
* Set a value based on an id. Optionally add tags.
*
* @param string id
* @param mixed data
* @param integer lifetime [Optional]
* @param array tags [Optional]
* @return boolean
* @throws Kohana_Cache_Exception
*/
public function set_with_tags($id, $data, $lifetime = NULL, array $tags = NULL)
{
// Serialize the data
$data = serialize($data);
// Normalise tags
$tags = (NULL === $tags) ? NULL : ('<'.implode('>,<', $tags).'>');
// Setup lifetime
if ($lifetime === NULL)
{
$lifetime = (0 === Arr::get('default_expire', NULL)) ? 0 : (Arr::get($this->_config, 'default_expire', Cache::DEFAULT_EXPIRE) + time());
}
else
{
$lifetime = (0 === $lifetime) ? 0 : ($lifetime + time());
}
// Prepare statement
// $this->exists() may throw Kohana_Cache_Exception, no need to catch/rethrow
$statement = $this->exists($id) ? $this->_db->prepare('UPDATE caches SET expiration = :expiration, cache = :cache, tags = :tags WHERE id = :id') : $this->_db->prepare('INSERT INTO caches (id, cache, expiration, tags) VALUES (:id, :cache, :expiration, :tags)');
// Try to insert
try
{
$statement->execute(array(':id' => $this->_sanitize_id($id), ':cache' => $data, ':expiration' => $lifetime, ':tags' => $tags));
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
return (bool) $statement->rowCount();
}
/**
* Delete cache entries based on a tag
*
* @param string tag
* @param integer timeout [Optional]
* @return boolean
* @throws Kohana_Cache_Exception
*/
public function delete_tag($tag)
{
// Prepare the statement
$statement = $this->_db->prepare('DELETE FROM caches WHERE tags LIKE :tag');
// Try to delete
try
{
$statement->execute(array(':tag' => "%<{$tag}>%"));
}
catch (PDOException $e)
{
throw new Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
return (bool) $statement->rowCount();
}
/**
* Find cache entries based on a tag
*
* @param string tag
* @return array
* @throws Kohana_Cache_Exception
*/
public function find($tag)
{
// Prepare the statement
$statement = $this->_db->prepare('SELECT id, cache FROM caches WHERE tags LIKE :tag');
// Try to find
try
{
if ( ! $statement->execute(array(':tag' => "%<{$tag}>%")))
{
return array();
}
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
$result = array();
while ($row = $statement->fetchObject())
{
// Disable notices for unserializing
$ER = error_reporting(~E_NOTICE);
$result[$row->id] = unserialize($row->cache);
// Turn notices back on
error_reporting($ER);
}
return $result;
}
/**
* Garbage collection method that cleans any expired
* cache entries from the cache.
*
* @return void
*/
public function garbage_collect()
{
// Create the sequel statement
$statement = $this->_db->prepare('DELETE FROM caches WHERE expiration < :expiration');
try
{
$statement->execute(array(':expiration' => time()));
}
catch (PDOException $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
}
/**
* Tests whether an id exists or not
*
* @param string id
* @return boolean
* @throws Kohana_Cache_Exception
*/
protected function exists($id)
{
$statement = $this->_db->prepare('SELECT id FROM caches WHERE id = :id');
try
{
$statement->execute(array(':id' => $this->_sanitize_id($id)));
}
catch (PDOExeption $e)
{
throw new Kohana_Cache_Exception('There was a problem querying the local SQLite3 cache. :error', array(':error' => $e->getMessage()));
}
return (bool) $statement->fetchAll();
}
}
|