PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/aws/lib/cachecore/cachepdo.class.php

https://bitbucket.org/ardydedase/web-build-optimizer
PHP | 290 lines | 132 code | 38 blank | 120 comment | 9 complexity | 6ee8f4fe4dd609a9c67882065ff52e27 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Container for all PDO-based cache methods. Inherits additional methods from <CacheCore>. Adheres
  4. * to the ICacheCore interface.
  5. *
  6. * @version 2009.10.10
  7. * @copyright 2006-2010 Ryan Parman
  8. * @copyright 2006-2010 Foleeo, Inc.
  9. * @copyright 2008-2010 Contributors
  10. * @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
  11. * @link http://github.com/skyzyx/cachecore CacheCore
  12. * @link http://getcloudfusion.com CloudFusion
  13. * @link http://php.net/pdo PDO
  14. */
  15. class CachePDO extends CacheCore implements ICacheCore
  16. {
  17. /**
  18. * Reference to the PDO connection object.
  19. */
  20. var $pdo = null;
  21. /**
  22. * Holds the parsed URL components.
  23. */
  24. var $dsn = null;
  25. /**
  26. * Holds the PDO-friendly version of the connection string.
  27. */
  28. var $dsn_string = null;
  29. /**
  30. * Holds the prepared statement for creating an entry.
  31. */
  32. var $create = null;
  33. /**
  34. * Holds the prepared statement for reading an entry.
  35. */
  36. var $read = null;
  37. /**
  38. * Holds the prepared statement for updating an entry.
  39. */
  40. var $update = null;
  41. /**
  42. * Holds the prepared statement for resetting the expiry of an entry.
  43. */
  44. var $reset = null;
  45. /**
  46. * Holds the prepared statement for deleting an entry.
  47. */
  48. var $delete = null;
  49. /**
  50. * Holds the response of the read so we only need to fetch it once instead of doing
  51. * multiple queries.
  52. */
  53. var $store_read = null;
  54. /*%******************************************************************************************%*/
  55. // CONSTRUCTOR
  56. /**
  57. * Constructs a new instance of this class.
  58. *
  59. * Tested with [MySQL 5.0.x](http://mysql.com), [PostgreSQL](http://postgresql.com), and
  60. * [SQLite 3.x](http://sqlite.org). SQLite 2.x is assumed to work. No other PDO-supported databases have
  61. * been tested (e.g. Oracle, Microsoft SQL Server, IBM DB2, ODBC, Sybase, Firebird). Feel free to send
  62. * patches for additional database support.
  63. *
  64. * See <http://php.net/pdo> for more information.
  65. *
  66. * @param string $name (Required) A name to uniquely identify the cache object.
  67. * @param string $location (Required) The location to store the cache object in. This may vary by cache method.
  68. * @param integer $expires (Required) The number of seconds until a cache object is considered stale.
  69. * @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
  70. * @return object Reference to the cache object.
  71. */
  72. public function __construct($name, $location, $expires, $gzip = true)
  73. {
  74. // Make sure the name is no longer than 40 characters.
  75. $name = sha1($name);
  76. // Call parent constructor and set id.
  77. parent::__construct($name, $location, $expires, $gzip);
  78. $this->id = $this->name;
  79. $options = array();
  80. // Check if the location contains :// (e.g. mysql://user:pass@hostname:port/table)
  81. if (stripos($location, '://') === false)
  82. {
  83. // No? Just pass it through.
  84. $this->dsn = parse_url($location);
  85. $this->dsn_string = $location;
  86. }
  87. else
  88. {
  89. // Yes? Parse and set the DSN
  90. $this->dsn = parse_url($location);
  91. $this->dsn_string = $this->dsn['scheme'] . ':host=' . $this->dsn['host'] . ((isset($this->dsn['port'])) ? ';port=' . $this->dsn['port'] : '') . ';dbname=' . substr($this->dsn['path'], 1);
  92. }
  93. // Make sure that user/pass are defined.
  94. $user = isset($this->dsn['user']) ? $this->dsn['user'] : null;
  95. $pass = isset($this->dsn['pass']) ? $this->dsn['pass'] : null;
  96. // Set persistence for databases that support it.
  97. switch ($this->dsn['scheme'])
  98. {
  99. case 'mysql': // MySQL
  100. case 'pgsql': // PostgreSQL
  101. $options[PDO::ATTR_PERSISTENT] = true;
  102. break;
  103. }
  104. // Instantiate a new PDO object with a persistent connection.
  105. $this->pdo = new PDO($this->dsn_string, $user, $pass, $options);
  106. // Define prepared statements for improved performance.
  107. $this->create = $this->pdo->prepare("INSERT INTO cache (id, expires, data) VALUES (:id, :expires, :data)");
  108. $this->read = $this->pdo->prepare("SELECT id, expires, data FROM cache WHERE id = :id");
  109. $this->reset = $this->pdo->prepare("UPDATE cache SET expires = :expires WHERE id = :id");
  110. $this->delete = $this->pdo->prepare("DELETE FROM cache WHERE id = :id");
  111. }
  112. /**
  113. * Creates a new cache.
  114. *
  115. * @param mixed $data (Required) The data to cache.
  116. * @return boolean Whether the operation was successful.
  117. */
  118. public function create($data)
  119. {
  120. $data = serialize($data);
  121. $data = $this->gzip ? gzcompress($data) : $data;
  122. $this->create->bindParam(':id', $this->id);
  123. $this->create->bindParam(':data', $data);
  124. $this->create->bindParam(':expires', $this->generate_timestamp());
  125. return (bool) $this->create->execute();
  126. }
  127. /**
  128. * Reads a cache.
  129. *
  130. * @return mixed Either the content of the cache object, or boolean `false`.
  131. */
  132. public function read()
  133. {
  134. if (!$this->store_read)
  135. {
  136. $this->read->bindParam(':id', $this->id);
  137. $this->read->execute();
  138. $this->store_read = $this->read->fetch(PDO::FETCH_ASSOC);
  139. }
  140. if ($this->store_read)
  141. {
  142. $data = $this->store_read['data'];
  143. $data = $this->gzip ? gzuncompress($data) : $data;
  144. return unserialize($data);
  145. }
  146. return false;
  147. }
  148. /**
  149. * Updates an existing cache.
  150. *
  151. * @param mixed $data (Required) The data to cache.
  152. * @return boolean Whether the operation was successful.
  153. */
  154. public function update($data)
  155. {
  156. $this->delete();
  157. return $this->create($data);
  158. }
  159. /**
  160. * Deletes a cache.
  161. *
  162. * @return boolean Whether the operation was successful.
  163. */
  164. public function delete()
  165. {
  166. $this->delete->bindParam(':id', $this->id);
  167. return $this->delete->execute();
  168. }
  169. /**
  170. * Checks whether the cache object is expired or not.
  171. *
  172. * @return boolean Whether the cache is expired or not.
  173. */
  174. public function is_expired()
  175. {
  176. if ($this->timestamp() + $this->expires < time())
  177. {
  178. return true;
  179. }
  180. return false;
  181. }
  182. /**
  183. * Retrieves the timestamp of the cache.
  184. *
  185. * @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
  186. */
  187. public function timestamp()
  188. {
  189. if (!$this->store_read)
  190. {
  191. $this->read->bindParam(':id', $this->id);
  192. $this->read->execute();
  193. $this->store_read = $this->read->fetch(PDO::FETCH_ASSOC);
  194. }
  195. if ($this->store_read)
  196. {
  197. $value = $this->store_read['expires'];
  198. // If 'expires' isn't yet an integer, convert it into one.
  199. if (!is_numeric($value))
  200. {
  201. $value = strtotime($value);
  202. }
  203. $this->timestamp = date('U', $value);
  204. return $this->timestamp;
  205. }
  206. return false;
  207. }
  208. /**
  209. * Resets the freshness of the cache.
  210. *
  211. * @return boolean Whether the operation was successful.
  212. */
  213. public function reset()
  214. {
  215. $this->reset->bindParam(':id', $this->id);
  216. $this->reset->bindParam(':expires', $this->generate_timestamp());
  217. return (bool) $this->reset->execute();
  218. }
  219. /**
  220. * Returns a list of supported PDO database drivers. Identical to <PDO::getAvailableDrivers()>.
  221. *
  222. * @return array The list of supported database drivers.
  223. * @link http://php.net/pdo.getavailabledrivers PHP Method
  224. */
  225. public function get_drivers()
  226. {
  227. return PDO::getAvailableDrivers();
  228. }
  229. /**
  230. * Returns a timestamp value apropriate to the current database type.
  231. *
  232. * @return mixed Timestamp for MySQL and PostgreSQL, integer value for SQLite.
  233. */
  234. protected function generate_timestamp()
  235. {
  236. // Define 'expires' settings differently.
  237. switch ($this->dsn['scheme'])
  238. {
  239. // These support timestamps.
  240. case 'mysql': // MySQL
  241. case 'pgsql': // PostgreSQL
  242. $expires = date(DATE_FORMAT_MYSQL, time());
  243. break;
  244. // These support integers.
  245. case 'sqlite': // SQLite 3
  246. case 'sqlite2': // SQLite 2
  247. $expires = time();
  248. break;
  249. }
  250. return $expires;
  251. }
  252. }