cache.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * cache class provides an abstracted cache
  4. *
  5. * @method string set
  6. * @method string get
  7. * @method string delete
  8. * @method string flush
  9. */
  10. class cache {
  11. /**
  12. * Called when the object is created
  13. */
  14. public function __construct() {
  15. //place holder
  16. }
  17. /**
  18. * Called when there are no references to a particular object
  19. * unset the variables used in the class
  20. */
  21. public function __destruct() {
  22. foreach ($this as $key => $value) {
  23. unset($this->$key);
  24. }
  25. }
  26. /**
  27. * Add a specific item in the cache
  28. * @var string $key the cache id
  29. * @var string $value string to be cached
  30. */
  31. public function set($key, $value) {
  32. //save to memcache
  33. if ($_SESSION['cache']['method']['text'] == "memcache") {
  34. //connect to event socket
  35. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  36. if ($fp === false) {
  37. return false;
  38. }
  39. //run the memcache
  40. $command = "memcache set ".$key." ".$value;
  41. $result = event_socket_request($fp, 'api '.$command);
  42. //close event socket
  43. fclose($fp);
  44. }
  45. //save to the file cache
  46. if ($_SESSION['cache']['method']['text'] == "file") {
  47. if (file_exists($_SESSION['cache']['location']['text'] . "/" . $key)) {
  48. $result = file_put_contents($_SESSION['cache']['location']['text'] . "/" . $key, $value);
  49. }
  50. }
  51. //return result
  52. return $result;
  53. }
  54. /**
  55. * Get a specific item from the cache
  56. * @var string $key cache id
  57. */
  58. public function get($key) {
  59. //cache method memcache
  60. if ($_SESSION['cache']['method']['text'] == "memcache") {
  61. // connect to event socket
  62. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  63. if ($fp === false) {
  64. return false;
  65. }
  66. //send a custom event
  67. //run the memcache
  68. $command = "memcache get ".$key;
  69. $result = event_socket_request($fp, 'api '.$command);
  70. //close event socket
  71. fclose($fp);
  72. }
  73. //get the file cache
  74. if ($_SESSION['cache']['method']['text'] == "file") {
  75. if (file_exists($_SESSION['cache']['location']['text'] . "/" . $key)) {
  76. $result = file_get_contents($_SESSION['cache']['location']['text'] . "/" . $key);
  77. }
  78. }
  79. //return result
  80. return $result;
  81. }
  82. /**
  83. * Delete a specific item from the cache
  84. * @var string $key cache id
  85. */
  86. public function delete($key) {
  87. //cache method memcache
  88. if ($_SESSION['cache']['method']['text'] == "memcache") {
  89. // connect to event socket
  90. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  91. if ($fp === false) {
  92. return false;
  93. }
  94. //send a custom event
  95. $event = "sendevent CUSTOM\n";
  96. $event .= "Event-Name: CUSTOM\n";
  97. $event .= "Event-Subclass: fusion::memcache\n";
  98. $event .= "API-Command: memcache\n";
  99. $event .= "API-Command-Argument: delete ".$key."\n";
  100. event_socket_request($fp, $event);
  101. //run the memcache
  102. $command = "memcache delete ".$key;
  103. $result = event_socket_request($fp, 'api '.$command);
  104. //close event socket
  105. fclose($fp);
  106. }
  107. //cache method file
  108. if ($_SESSION['cache']['method']['text'] == "file") {
  109. //change the delimiter
  110. $key = str_replace(":", ".", $key);
  111. //connect to event socket
  112. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  113. if ($fp === false) {
  114. return false;
  115. }
  116. //send a custom event
  117. $event = "sendevent CUSTOM\n";
  118. $event .= "Event-Name: CUSTOM\n";
  119. $event .= "Event-Subclass: fusion::file\n";
  120. $event .= "API-Command: cache\n";
  121. $event .= "API-Command-Argument: delete ".$key."\n";
  122. event_socket_request($fp, $event);
  123. //remove the local files
  124. if (file_exists($_SESSION['cache']['location']['text'] . "/" . $key)) {
  125. unlink($_SESSION['cache']['location']['text'] . "/" . $key);
  126. }
  127. if (file_exists($_SESSION['cache']['location']['text'] . "/" . $key . ".tmp")) {
  128. unlink($_SESSION['cache']['location']['text'] . "/" . $key . ".tmp");
  129. }
  130. }
  131. // return result
  132. return $result;
  133. }
  134. /**
  135. * Delete the entire cache
  136. */
  137. public function flush() {
  138. //cache method memcache
  139. if ($_SESSION['cache']['method']['text'] == "memcache") {
  140. // connect to event socket
  141. $fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
  142. if ($fp === false) {
  143. return false;
  144. }
  145. //send a custom event
  146. $event = "sendevent CUSTOM\n";
  147. $event .= "Event-Name: CUSTOM\n";
  148. $event .= "Event-Subclass: fusion::memcache\n";
  149. $event .= "API-Command: memcache\n";
  150. $event .= "API-Command-Argument: flush\n";
  151. event_socket_request($fp, $event);
  152. //run the memcache
  153. $command = "memcache flush";
  154. $result = event_socket_request($fp, 'api '.$command);
  155. //close event socket
  156. fclose($fp);
  157. }
  158. //cache method file
  159. if ($_SESSION['cache']['method']['text'] == "file") {
  160. //send a custom event
  161. $event = "sendevent CUSTOM\n";
  162. $event .= "Event-Name: CUSTOM\n";
  163. $event .= "Event-Subclass: fusion::file\n";
  164. $event .= "API-Command: cache\n";
  165. $event .= "API-Command-Argument: flush\n";
  166. event_socket_request($fp, $event);
  167. //remove the cache
  168. recursive_delete($_SESSION['cache']['location']['text']);
  169. //set message
  170. $result = '+OK cache flushed';
  171. }
  172. //return result
  173. return $result;
  174. }
  175. }
  176. ?>