PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/drivers/Cache/Sqlite.php

https://github.com/Toushi/flow
PHP | 228 lines | 119 code | 34 blank | 75 comment | 12 complexity | 9b0d1dd0f06f109298a8d2cae61b091e MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * SQLite-based Cache driver.
  4. *
  5. * $Id: Sqlite.php 3168 2008-07-21 01:34:36Z Shadowhand $
  6. *
  7. * @package Cache
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Cache_Sqlite_Driver implements Cache_Driver {
  13. // SQLite database instance
  14. protected $db;
  15. // Database error messages
  16. protected $error;
  17. /**
  18. * Logs an SQLite error.
  19. */
  20. protected static function log_error($code)
  21. {
  22. // Log an error
  23. Kohana::log('error', 'Cache: SQLite error: '.sqlite_error_string($error));
  24. }
  25. /**
  26. * Tests that the storage location is a directory and is writable.
  27. */
  28. public function __construct($filename)
  29. {
  30. // Get the directory name
  31. $directory = str_replace('\\', '/', realpath(pathinfo($filename, PATHINFO_DIRNAME))).'/';
  32. // Set the filename from the real directory path
  33. $filename = $directory.basename($filename);
  34. // Make sure the cache directory is writable
  35. if ( ! is_dir($directory) OR ! is_writable($directory))
  36. throw new Kohana_Exception('cache.unwritable', $directory);
  37. // Make sure the cache database is writable
  38. if (is_file($filename) AND ! is_writable($filename))
  39. throw new Kohana_Exception('cache.unwritable', $filename);
  40. // Open up an instance of the database
  41. $this->db = new SQLiteDatabase($filename, '0666', $error);
  42. // Throw an exception if there's an error
  43. if ( ! empty($error))
  44. throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error));
  45. $query = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'caches'";
  46. $tables = $this->db->query($query, SQLITE_BOTH, $error);
  47. // Throw an exception if there's an error
  48. if ( ! empty($error))
  49. throw new Kohana_Exception('cache.driver_error', sqlite_error_string($error));
  50. if ($tables->numRows() == 0)
  51. {
  52. Kohana::log('error', 'Cache: Initializing new SQLite cache database');
  53. // Issue a CREATE TABLE command
  54. $this->db->unbufferedQuery(Kohana::config('cache_sqlite.schema'));
  55. }
  56. }
  57. /**
  58. * Checks if a cache id is already set.
  59. *
  60. * @param string cache id
  61. * @return boolean
  62. */
  63. public function exists($id)
  64. {
  65. // Find the id that matches
  66. $query = "SELECT id FROM caches WHERE id = '$id'";
  67. return ($this->db->query($query)->numRows() > 0);
  68. }
  69. /**
  70. * Sets a cache item to the given data, tags, and lifetime.
  71. *
  72. * @param string cache id to set
  73. * @param string data in the cache
  74. * @param array cache tags
  75. * @param integer lifetime
  76. * @return bool
  77. */
  78. public function set($id, $data, $tags, $lifetime)
  79. {
  80. // Find the data hash
  81. $hash = sha1($data);
  82. // Escape the data
  83. $data = sqlite_escape_string($data);
  84. // Escape the tags
  85. $tags = sqlite_escape_string(implode(',', $tags));
  86. // Cache Sqlite driver expects unix timestamp
  87. if ($lifetime !== 0)
  88. {
  89. $lifetime += time();
  90. }
  91. $query = $this->exists($id)
  92. ? "UPDATE caches SET hash = '$hash', tags = '$tags', expiration = '$lifetime', cache = '$data' WHERE id = '$id'"
  93. : "INSERT INTO caches VALUES('$id', '$hash', '$tags', '$lifetime', '$data')";
  94. // Run the query
  95. $this->db->unbufferedQuery($query, SQLITE_BOTH, $error);
  96. empty($error) or self::log_error($error);
  97. return empty($error);
  98. }
  99. /**
  100. * Finds an array of ids for a given tag.
  101. *
  102. * @param string tag name
  103. * @return array of ids that match the tag
  104. */
  105. public function find($tag)
  106. {
  107. $query = "SELECT id FROM caches WHERE tags LIKE '%{$tag}%'";
  108. $query = $this->db->query($query, SQLITE_BOTH, $error);
  109. empty($error) or self::log_error($error);
  110. if (empty($error) AND $query->numRows() > 0)
  111. {
  112. $array = array();
  113. while ($row = $query->fetchObject())
  114. {
  115. // Add each id to the array
  116. $array[] = $row->id;
  117. }
  118. return $array;
  119. }
  120. return FALSE;
  121. }
  122. /**
  123. * Fetches a cache item. This will delete the item if it is expired or if
  124. * the hash does not match the stored hash.
  125. *
  126. * @param string cache id
  127. * @return mixed|NULL
  128. */
  129. public function get($id)
  130. {
  131. $query = "SELECT id, hash, expiration, cache FROM caches WHERE id = '{$id}' LIMIT 0, 1";
  132. $query = $this->db->query($query, SQLITE_BOTH, $error);
  133. empty($error) or self::log_error($error);
  134. if (empty($error) AND $cache = $query->fetchObject())
  135. {
  136. // Make sure the expiration is valid and that the hash matches
  137. if (($cache->expiration != 0 AND $cache->expiration <= time()) OR $cache->hash !== sha1($cache->cache))
  138. {
  139. // Cache is not valid, delete it now
  140. $this->delete($cache->id);
  141. }
  142. else
  143. {
  144. // Return the valid cache data
  145. return $cache->cache;
  146. }
  147. }
  148. // No valid cache found
  149. return NULL;
  150. }
  151. /**
  152. * Deletes a cache item by id or tag
  153. *
  154. * @param string cache id or tag, or TRUE for "all items"
  155. * @param bool use tags
  156. * @return bool
  157. */
  158. public function delete($id, $tag = FALSE)
  159. {
  160. if ($id === TRUE)
  161. {
  162. // Delete all caches
  163. $where = '1';
  164. }
  165. elseif ($tag == FALSE)
  166. {
  167. // Delete by id
  168. $where = "id = '{$id}'";
  169. }
  170. else
  171. {
  172. // Delete by tag
  173. $where = "tags LIKE '%{$tag}%'";
  174. }
  175. $this->db->unbufferedQuery('DELETE FROM caches WHERE '.$where, SQLITE_BOTH, $error);
  176. empty($error) or self::log_error($error);
  177. return empty($error);
  178. }
  179. /**
  180. * Deletes all cache files that are older than the current time.
  181. */
  182. public function delete_expired()
  183. {
  184. // Delete all expired caches
  185. $query = 'DELETE FROM caches WHERE expiration != 0 AND expiration <= '.time();
  186. $this->db->unbufferedQuery($query);
  187. return TRUE;
  188. }
  189. } // End Cache SQLite Driver