/src/lastfmapi/Lib/Cache.php

https://github.com/matto1990/PHP-Last.fm-API · PHP · 252 lines · 142 code · 22 blank · 88 comment · 50 complexity · 7de78b471296a2611e3c65df0ad4c80c MD5 · raw file

  1. <?php
  2. namespace LastFmApi\Lib;
  3. /**
  4. * Stores the caching methods
  5. */
  6. /**
  7. * Allows access to the caching methods to cache data when api calls are made
  8. */
  9. class Cache
  10. {
  11. /**
  12. * Stores the batabase class
  13. * @var class
  14. */
  15. private $db;
  16. /**
  17. * Stores the batabase type
  18. * @var string
  19. */
  20. private $type;
  21. /**
  22. * Stores the error details
  23. * @var array
  24. */
  25. public $error;
  26. /**
  27. * Stores the path to the sqlite database
  28. * @var string
  29. */
  30. private $path;
  31. /**
  32. * Stores the amount of time to cahce for
  33. * @var integer
  34. */
  35. private $cache_length;
  36. /**
  37. * Stores the config array
  38. * @var array
  39. */
  40. private $config;
  41. /**
  42. * States if caching is enabled or not
  43. * @var boolean
  44. */
  45. private $enabled;
  46. /**
  47. * Run when the class is created
  48. * @param array $config The config array
  49. * @uses lastfmApiDatabase
  50. */
  51. function __construct($config)
  52. {
  53. $this->config = $config;
  54. if (isset($this->config['cache_type'])) {
  55. $this->type = $this->config['cache_type'];
  56. } else {
  57. $this->type = 'sqlite';
  58. }
  59. $this->check_if_enabled();
  60. if ($this->enabled == true) {
  61. if ($this->type == 'sqlite') {
  62. $this->db = new Sqlite($this->config['path'] . '/phplastfmapi.sqlite3');
  63. } else {
  64. if (isset($this->config['database']['host']) && isset($this->config['database']['username']) && isset($this->config['database']['password']) && isset($this->config['database']['name'])) {
  65. $this->db = new MySql($this->config['database']['host'], $this->config['database']['username'], $this->config['database']['password'], $this->config['database']['name']);
  66. } else {
  67. $this->error = 'Not all mysql database variables were supplied';
  68. return false;
  69. }
  70. }
  71. if (!empty($this->db->error)) {
  72. $this->error = $this->db->error;
  73. return false;
  74. } else {
  75. $this->check_table_exists();
  76. }
  77. //$this->show_all();
  78. }
  79. }
  80. /**
  81. * Internal method to chack if caching is enabled
  82. * @access private
  83. * @return void
  84. */
  85. private function check_if_enabled()
  86. {
  87. if ($this->config['enabled'] == true && extension_loaded('sqlite3')) {
  88. $this->enabled = true;
  89. } else {
  90. $this->enabled = false;
  91. }
  92. }
  93. /**
  94. * Internal method to check if the table exists
  95. * @access private
  96. * @return void
  97. */
  98. private function check_table_exists()
  99. {
  100. if ($this->type == 'sqlite') {
  101. $query = "SELECT count(*) FROM sqlite_master WHERE name='cache'";
  102. $result = $this->db->query($query);
  103. $numbers = $result->fetchAll();
  104. if ($numbers[0]['count(*)'] > 0) {
  105. // Ok
  106. } else {
  107. $this->create_table();
  108. }
  109. } else {
  110. $query = "show tables like 'cache'";
  111. $result = $this->db->query($query);
  112. if ($result->size() > 0) {
  113. // Ok
  114. } else {
  115. $this->create_table();
  116. }
  117. }
  118. }
  119. /**
  120. * Internal method to create the table if it doesn't exist
  121. * @access private
  122. * @return void
  123. */
  124. private function create_table()
  125. {
  126. if ($this->type == 'sqlite') {
  127. $auto_increase = '';
  128. } else {
  129. $auto_increase = ' AUTO_INCREMENT';
  130. }
  131. $query = "CREATE TABLE cache (cache_id INTEGER PRIMARY KEY" . $auto_increase . ", unique_vars TEXT, expires INT, body TEXT)";
  132. if ($this->db->query($query)) {
  133. // Ok
  134. } else {
  135. // TODO: Handle error
  136. echo $this->db->error;
  137. }
  138. }
  139. /**
  140. * Searches the database for the cahce date. Returns an array if it exists or false if it doesn't
  141. * @access public
  142. * @param array $unique_vars Array of variables that are unique to this piece of cached data
  143. * @return string
  144. */
  145. public function get($unique_vars)
  146. {
  147. if ($this->enabled == true) {
  148. $query = "SELECT expires, body FROM cache WHERE unique_vars='" . htmlentities(serialize($unique_vars), ENT_QUOTES, 'UTF-8') . "' LIMIT 1";
  149. if ($result = $this->db->query($query)) {
  150. if ($result->size() > 0) {
  151. $row = $result->fetch();
  152. if ($row['expires'] < time()) {
  153. $this->del($unique_vars);
  154. return false;
  155. } else {
  156. //print_r(unserialize(html_entity_decode($row['body'], ENT_QUOTES, 'UTF-8')));
  157. return unserialize(html_entity_decode($row['body'], ENT_QUOTES, 'UTF-8'));
  158. }
  159. } else {
  160. return false;
  161. }
  162. } else {
  163. // TODO: Handle error
  164. return false;
  165. }
  166. } else {
  167. return false;
  168. }
  169. }
  170. /**
  171. * Adds new cache data to the database
  172. * @access public
  173. * @param array $unique_vars Array of variables that are unique to this piece of cached data
  174. * @param string $body The contents of the cache to put into the database
  175. * @return boolean
  176. */
  177. public function set($unique_vars, $body)
  178. {
  179. if ($this->enabled == true) {
  180. $expire = time() + $this->config['cache_length'];
  181. $query = "INSERT INTO cache (unique_vars, expires, body) VALUES ('" . htmlentities(serialize($unique_vars), ENT_QUOTES, 'UTF-8') . "', '" . $expire . "', \"" . htmlentities(serialize($body), ENT_QUOTES, 'UTF-8') . "\")";
  182. if ($this->db->query($query)) {
  183. return true;
  184. } else {
  185. // TODO: Handle error
  186. return false;
  187. }
  188. } else {
  189. return false;
  190. }
  191. }
  192. /**
  193. * Internal method to delete unneeded cache data
  194. * @access private
  195. * @param array $unique_vars Array of variables that are unique to this piece of cached data
  196. * @return boolean
  197. */
  198. private function del($unique_vars)
  199. {
  200. $query = "DELETE FROM cache WHERE unique_vars='" . htmlentities(serialize($unique_vars), ENT_QUOTES, 'UTF-8') . "'";
  201. if ($this->db->query($query)) {
  202. return true;
  203. } else {
  204. // TODO: Handle error
  205. return false;
  206. }
  207. }
  208. /**
  209. * Internal method to show all cached data (used for debugging)
  210. * @access private
  211. * @return boolean
  212. */
  213. private function show_all()
  214. {
  215. $query = "SELECT expires, body FROM cache";
  216. if ($result = $this->db->query($query)) {
  217. if ($result->size() > 0) {
  218. $results = $result->fetchAll();
  219. echo '<pre>';
  220. print_r($results);
  221. echo '</pre>';
  222. } else {
  223. return false;
  224. }
  225. } else {
  226. // TODO: Handle error
  227. return false;
  228. }
  229. }
  230. }