123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- /**
- * Provides an abstracted cache
- */
- class cache {
- private $settings;
- private $syslog;
- private $location;
- private $method;
- /**
- * Called when the object is created
- */
- public function __construct(settings $settings = null) {
- //set defaults
- if ($settings === null) {
- $settings = new settings();
- }
- //get the settings
- $this->settings = $settings;
- $this->method = $this->setting('method');
- $this->syslog = $this->setting('syslog');
- $this->location = $this->setting('location');
- if (empty($this->method)) {
- $this->method = 'file';
- }
- if (empty($this->syslog)) {
- $this->syslog = 'false';
- }
- if (empty($this->location)) {
- $this->location = '/var/cache/fusionpbx';
- }
- }
- private function setting($subcategory) {
- return $this->settings->get('cache', $subcategory);
- }
- /**
- * Add a specific item in the cache
- * @var string $key the cache id
- * @var string $value string to be cached
- */
- public function set($key, $value) {
- //change the delimiter
- $key = str_replace(":", ".", $key);
- //save to memcache
- if ($this->method === "memcache") {
- //connect to event socket
- $esl = event_socket::create();
- if ($esl === false) {
- return false;
- }
- //run the memcache
- $command = "memcache set ".$key." ".$value;
- $result = event_socket::api($command);
- }
- //save to the file cache
- if ($this->method === "file") {
- $result = file_put_contents($this->location . "/" . $key, $value);
- }
- //return result
- return $result;
- }
- /**
- * Get a specific item from the cache
- * @var string $key cache id
- */
- public function get($key) {
- //change the delimiter
- $key = str_replace(":", ".", $key);
- //cache method memcache
- if ($this->method === "memcache") {
- // connect to event socket
- $esl = event_socket::create();
- if (!$esl->is_connected()) {
- return false;
- }
- //send a custom event
- //run the memcache
- $command = "memcache get ".$key;
- $result = event_socket::api($command);
- }
- //get the file cache
- if ($this->method === "file") {
- if (file_exists($this->location . "/" . $key)) {
- $result = file_get_contents($this->location . "/" . $key);
- }
- }
- //return result
- return $result ?? null;
- }
- /**
- * Delete a specific item from the cache
- * @var string $key cache id
- */
- public function delete($key) {
- //debug information
- if ($this->syslog === "true") {
- openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
- syslog(LOG_WARNING, "debug: cache: [key: ".$key.", script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
- closelog();
- }
- //cache method memcache
- if ($this->method === "memcache") {
- //connect to event socket
- $esl = event_socket::create();
- if ($esl === false) {
- return false;
- }
- //send a custom event
- $event = "sendevent CUSTOM\n";
- $event .= "Event-Name: CUSTOM\n";
- $event .= "Event-Subclass: fusion::memcache\n";
- $event .= "API-Command: memcache\n";
- $event .= "API-Command-Argument: delete ".$key."\n";
- event_socket::command($event);
- //run the memcache
- $command = "memcache delete ".$key;
- $result = event_socket::api($command);
- }
- //cache method file
- if ($this->method === "file") {
- //change the delimiter
- $key = str_replace(":", ".", $key);
- //connect to event socket
- $esl = event_socket::create();
- if ($esl === false) {
- return false;
- }
- //send a custom event
- $event = "sendevent CUSTOM\n";
- $event .= "Event-Name: CUSTOM\n";
- $event .= "Event-Subclass: fusion::file\n";
- $event .= "API-Command: cache\n";
- $event .= "API-Command-Argument: delete ".$key."\n";
- event_socket::command($event);
- //remove the local files
- foreach (glob($this->location . "/" . $key) as $file) {
- if (file_exists($file)) {
- unlink($file);
- }
- if (file_exists($file)) {
- unlink($file . ".tmp");
- }
- }
- }
- }
- /**
- * Delete the entire cache
- */
- public function flush() {
- //debug information
- if ($this->syslog === "true") {
- openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
- syslog(LOG_WARNING, "debug: cache: [flush: all, script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
- closelog();
- }
- //cache method memcache
- if ($this->method === "memcache") {
- // connect to event socket
- $esl = event_socket::create();
- if ($esl === false) {
- return false;
- }
- //send a custom event
- $event = "sendevent CUSTOM\n";
- $event .= "Event-Name: CUSTOM\n";
- $event .= "Event-Subclass: fusion::memcache\n";
- $event .= "API-Command: memcache\n";
- $event .= "API-Command-Argument: flush\n";
- event_socket::command($event);
- //run the memcache
- $command = "memcache flush";
- $result = event_socket::api($command);
- }
- //cache method file
- if ($this->method === "file") {
- // connect to event socket
- $esl = event_socket::create();
- if ($esl === false) {
- return false;
- }
- //send a custom event
- $event = "sendevent CUSTOM\n";
- $event .= "Event-Name: CUSTOM\n";
- $event .= "Event-Subclass: fusion::file\n";
- $event .= "API-Command: cache\n";
- $event .= "API-Command-Argument: flush\n";
- event_socket::command($event);
- //remove the cache
- recursive_delete($this->location);
- //set message
- $result = '+OK cache flushed';
- }
- //return result
- return $result;
- }
- }
- ?>
|