cache.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Provides an abstracted cache
  4. */
  5. class cache {
  6. private $settings;
  7. private $syslog;
  8. private $location;
  9. private $method;
  10. /**
  11. * Called when the object is created
  12. */
  13. public function __construct(settings $settings = null) {
  14. //set defaults
  15. if ($settings === null) {
  16. $settings = new settings();
  17. }
  18. //get the settings
  19. $this->settings = $settings;
  20. $this->method = $this->setting('method');
  21. $this->syslog = $this->setting('syslog');
  22. $this->location = $this->setting('location');
  23. if (empty($this->method)) {
  24. $this->method = 'file';
  25. }
  26. if (empty($this->syslog)) {
  27. $this->syslog = 'false';
  28. }
  29. if (empty($this->location)) {
  30. $this->location = '/var/cache/fusionpbx';
  31. }
  32. }
  33. private function setting($subcategory) {
  34. return $this->settings->get('cache', $subcategory);
  35. }
  36. /**
  37. * Add a specific item in the cache
  38. * @var string $key the cache id
  39. * @var string $value string to be cached
  40. */
  41. public function set($key, $value) {
  42. //change the delimiter
  43. $key = str_replace(":", ".", $key);
  44. //save to memcache
  45. if ($this->method === "memcache") {
  46. //connect to event socket
  47. $esl = event_socket::create();
  48. if ($esl === false) {
  49. return false;
  50. }
  51. //run the memcache
  52. $command = "memcache set ".$key." ".$value;
  53. $result = event_socket::api($command);
  54. }
  55. //save to the file cache
  56. if ($this->method === "file") {
  57. $result = file_put_contents($this->location . "/" . $key, $value);
  58. }
  59. //return result
  60. return $result;
  61. }
  62. /**
  63. * Get a specific item from the cache
  64. * @var string $key cache id
  65. */
  66. public function get($key) {
  67. //change the delimiter
  68. $key = str_replace(":", ".", $key);
  69. //cache method memcache
  70. if ($this->method === "memcache") {
  71. // connect to event socket
  72. $esl = event_socket::create();
  73. if (!$esl->is_connected()) {
  74. return false;
  75. }
  76. //send a custom event
  77. //run the memcache
  78. $command = "memcache get ".$key;
  79. $result = event_socket::api($command);
  80. }
  81. //get the file cache
  82. if ($this->method === "file") {
  83. if (file_exists($this->location . "/" . $key)) {
  84. $result = file_get_contents($this->location . "/" . $key);
  85. }
  86. }
  87. //return result
  88. return $result ?? null;
  89. }
  90. /**
  91. * Delete a specific item from the cache
  92. * @var string $key cache id
  93. */
  94. public function delete($key) {
  95. //debug information
  96. if ($this->syslog === "true") {
  97. openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
  98. syslog(LOG_WARNING, "debug: cache: [key: ".$key.", script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
  99. closelog();
  100. }
  101. //cache method memcache
  102. if ($this->method === "memcache") {
  103. //connect to event socket
  104. $esl = event_socket::create();
  105. if ($esl === false) {
  106. return false;
  107. }
  108. //send a custom event
  109. $event = "sendevent CUSTOM\n";
  110. $event .= "Event-Name: CUSTOM\n";
  111. $event .= "Event-Subclass: fusion::memcache\n";
  112. $event .= "API-Command: memcache\n";
  113. $event .= "API-Command-Argument: delete ".$key."\n";
  114. event_socket::command($event);
  115. //run the memcache
  116. $command = "memcache delete ".$key;
  117. $result = event_socket::api($command);
  118. }
  119. //cache method file
  120. if ($this->method === "file") {
  121. //change the delimiter
  122. $key = str_replace(":", ".", $key);
  123. //connect to event socket
  124. $esl = event_socket::create();
  125. if ($esl === false) {
  126. return false;
  127. }
  128. //send a custom event
  129. $event = "sendevent CUSTOM\n";
  130. $event .= "Event-Name: CUSTOM\n";
  131. $event .= "Event-Subclass: fusion::file\n";
  132. $event .= "API-Command: cache\n";
  133. $event .= "API-Command-Argument: delete ".$key."\n";
  134. event_socket::command($event);
  135. //remove the local files
  136. foreach (glob($this->location . "/" . $key) as $file) {
  137. if (file_exists($file)) {
  138. unlink($file);
  139. }
  140. if (file_exists($file)) {
  141. unlink($file . ".tmp");
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Delete the entire cache
  148. */
  149. public function flush() {
  150. //debug information
  151. if ($this->syslog === "true") {
  152. openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
  153. syslog(LOG_WARNING, "debug: cache: [flush: all, script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
  154. closelog();
  155. }
  156. //cache method memcache
  157. if ($this->method === "memcache") {
  158. // connect to event socket
  159. $esl = event_socket::create();
  160. if ($esl === false) {
  161. return false;
  162. }
  163. //send a custom event
  164. $event = "sendevent CUSTOM\n";
  165. $event .= "Event-Name: CUSTOM\n";
  166. $event .= "Event-Subclass: fusion::memcache\n";
  167. $event .= "API-Command: memcache\n";
  168. $event .= "API-Command-Argument: flush\n";
  169. event_socket::command($event);
  170. //run the memcache
  171. $command = "memcache flush";
  172. $result = event_socket::api($command);
  173. }
  174. //cache method file
  175. if ($this->method === "file") {
  176. // connect to event socket
  177. $esl = event_socket::create();
  178. if ($esl === false) {
  179. return false;
  180. }
  181. //send a custom event
  182. $event = "sendevent CUSTOM\n";
  183. $event .= "Event-Name: CUSTOM\n";
  184. $event .= "Event-Subclass: fusion::file\n";
  185. $event .= "API-Command: cache\n";
  186. $event .= "API-Command-Argument: flush\n";
  187. event_socket::command($event);
  188. //remove the cache
  189. recursive_delete($this->location);
  190. //set message
  191. $result = '+OK cache flushed';
  192. }
  193. //return result
  194. return $result;
  195. }
  196. }
  197. ?>