PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/Aws_v1/lib/cachecore/cachepdo.class.php

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