2016-04-25 20:30:23 -05:00
|
|
|
<?php
|
|
|
|
|
|
2024-02-09 13:21:55 -04:00
|
|
|
|
2016-04-25 20:30:23 -05:00
|
|
|
/**
|
2024-02-09 13:21:55 -04:00
|
|
|
* Provides an abstracted cache
|
2016-04-25 20:30:23 -05:00
|
|
|
*/
|
|
|
|
|
class cache {
|
2024-02-09 13:21:55 -04:00
|
|
|
private $settings;
|
|
|
|
|
private $syslog;
|
|
|
|
|
private $location;
|
|
|
|
|
private $method;
|
2016-04-25 20:30:23 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called when the object is created
|
|
|
|
|
*/
|
2024-02-09 13:21:55 -04:00
|
|
|
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);
|
2016-04-25 20:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2021-10-26 00:30:30 -06:00
|
|
|
|
|
|
|
|
//change the delimiter
|
|
|
|
|
$key = str_replace(":", ".", $key);
|
|
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//save to memcache
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->method === "memcache") {
|
2019-07-30 00:11:50 -06:00
|
|
|
//connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if ($esl === false) {
|
2019-07-30 00:11:50 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache set ".$key." ".$value;
|
2023-12-02 20:16:18 -04:00
|
|
|
$result = event_socket::api($command);
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2016-04-25 20:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//save to the file cache
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->method === "file") {
|
|
|
|
|
$result = file_put_contents($this->location . "/" . $key, $value);
|
2019-07-30 00:11:50 -06:00
|
|
|
}
|
2016-04-25 20:30:23 -05:00
|
|
|
|
2019-10-15 13:54:55 -06:00
|
|
|
//return result
|
2016-04-25 20:30:23 -05:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific item from the cache
|
|
|
|
|
* @var string $key cache id
|
|
|
|
|
*/
|
|
|
|
|
public function get($key) {
|
2019-11-18 18:01:10 -07:00
|
|
|
|
2021-10-26 00:30:30 -06:00
|
|
|
//change the delimiter
|
|
|
|
|
$key = str_replace(":", ".", $key);
|
|
|
|
|
|
2024-02-09 13:21:55 -04:00
|
|
|
//cache method memcache
|
|
|
|
|
if ($this->method === "memcache") {
|
2019-07-30 00:11:50 -06:00
|
|
|
// connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if (!$esl->is_connected()) {
|
2019-07-30 00:11:50 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//send a custom event
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache get ".$key;
|
2023-12-02 20:16:18 -04:00
|
|
|
$result = event_socket::api($command);
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2016-04-25 20:30:23 -05:00
|
|
|
}
|
2019-10-15 13:54:55 -06:00
|
|
|
|
2019-07-30 00:11:50 -06:00
|
|
|
//get the file cache
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->method === "file") {
|
|
|
|
|
if (file_exists($this->location . "/" . $key)) {
|
|
|
|
|
$result = file_get_contents($this->location . "/" . $key);
|
2019-07-30 00:11:50 -06:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-15 13:54:55 -06:00
|
|
|
|
|
|
|
|
//return result
|
2023-07-07 19:41:48 +00:00
|
|
|
return $result ?? null;
|
2016-04-25 20:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete a specific item from the cache
|
|
|
|
|
* @var string $key cache id
|
|
|
|
|
*/
|
|
|
|
|
public function delete($key) {
|
|
|
|
|
|
2022-06-01 22:28:12 -06:00
|
|
|
//debug information
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->syslog === "true") {
|
2022-09-10 11:40:59 -06:00
|
|
|
openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
|
|
|
|
|
syslog(LOG_WARNING, "debug: cache: [key: ".$key.", script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
|
|
|
|
|
closelog();
|
|
|
|
|
}
|
2022-06-01 22:28:12 -06:00
|
|
|
|
2024-02-09 13:21:55 -04:00
|
|
|
//cache method memcache
|
|
|
|
|
if ($this->method === "memcache") {
|
2021-09-03 22:35:33 -06:00
|
|
|
//connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if ($esl === false) {
|
2017-07-22 13:18:03 -06:00
|
|
|
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";
|
2023-12-02 20:16:18 -04:00
|
|
|
event_socket::command($event);
|
2017-07-22 13:18:03 -06:00
|
|
|
|
|
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache delete ".$key;
|
2023-12-02 20:16:18 -04:00
|
|
|
$result = event_socket::api($command);
|
2017-07-22 13:18:03 -06:00
|
|
|
|
|
|
|
|
}
|
2016-04-25 20:30:23 -05:00
|
|
|
|
2017-07-22 15:52:45 -06:00
|
|
|
//cache method file
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->method === "file") {
|
2018-08-10 17:41:02 -06:00
|
|
|
//change the delimiter
|
|
|
|
|
$key = str_replace(":", ".", $key);
|
2017-09-20 18:19:56 -04:00
|
|
|
|
2019-11-18 18:01:10 -07:00
|
|
|
//connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if ($esl === false) {
|
2017-09-20 18:19:56 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-24 16:55:56 -05:00
|
|
|
|
2017-09-20 18:19:56 -04:00
|
|
|
//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";
|
2023-12-02 20:16:18 -04:00
|
|
|
event_socket::command($event);
|
2018-07-24 16:55:56 -05:00
|
|
|
|
2017-09-20 18:19:56 -04:00
|
|
|
//remove the local files
|
2024-02-09 13:21:55 -04:00
|
|
|
foreach (glob($this->location . "/" . $key) as $file) {
|
2020-11-05 11:29:41 -05:00
|
|
|
if (file_exists($file)) {
|
|
|
|
|
unlink($file);
|
|
|
|
|
}
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
|
unlink($file . ".tmp");
|
|
|
|
|
}
|
2017-09-20 18:19:56 -04:00
|
|
|
}
|
2017-07-22 13:18:03 -06:00
|
|
|
}
|
2016-04-25 20:30:23 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the entire cache
|
|
|
|
|
*/
|
|
|
|
|
public function flush() {
|
2022-06-01 22:28:12 -06:00
|
|
|
|
|
|
|
|
//debug information
|
2024-02-09 13:21:55 -04:00
|
|
|
if ($this->syslog === "true") {
|
2022-09-10 11:40:59 -06:00
|
|
|
openlog("fusionpbx", LOG_PID | LOG_PERROR, LOG_USER);
|
|
|
|
|
syslog(LOG_WARNING, "debug: cache: [flush: all, script: ".$_SERVER['SCRIPT_NAME'].", line: ".__line__."]");
|
|
|
|
|
closelog();
|
|
|
|
|
}
|
2022-06-01 22:28:12 -06:00
|
|
|
|
2025-02-25 20:21:41 -04:00
|
|
|
//check for apcu extension
|
|
|
|
|
if (function_exists('apcu_enabled') && apcu_enabled()) {
|
|
|
|
|
//flush everything
|
|
|
|
|
apcu_clear_cache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//remove the autoloader file cache
|
|
|
|
|
if (file_exists(sys_get_temp_dir() . '/' . auto_loader::FILE)) {
|
|
|
|
|
@unlink(sys_get_temp_dir() . '/' . auto_loader::FILE);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 13:21:55 -04:00
|
|
|
//cache method memcache
|
|
|
|
|
if ($this->method === "memcache") {
|
2017-07-22 13:18:03 -06:00
|
|
|
// connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if ($esl === false) {
|
2017-07-22 13:18:03 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-24 16:55:56 -05:00
|
|
|
|
2017-07-22 13:18:03 -06:00
|
|
|
//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";
|
2023-12-02 20:16:18 -04:00
|
|
|
event_socket::command($event);
|
2018-07-24 16:55:56 -05:00
|
|
|
|
2017-07-22 13:18:03 -06:00
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache flush";
|
2023-12-02 20:16:18 -04:00
|
|
|
$result = event_socket::api($command);
|
2018-07-24 16:55:56 -05:00
|
|
|
|
2016-04-25 20:30:23 -05:00
|
|
|
}
|
|
|
|
|
|
2024-02-09 13:21:55 -04:00
|
|
|
//cache method file
|
|
|
|
|
if ($this->method === "file") {
|
2021-10-14 00:41:56 -06:00
|
|
|
// connect to event socket
|
2023-12-02 20:16:18 -04:00
|
|
|
$esl = event_socket::create();
|
|
|
|
|
if ($esl === false) {
|
2021-10-14 00:41:56 -06:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-22 13:18:03 -06:00
|
|
|
//send a custom event
|
|
|
|
|
$event = "sendevent CUSTOM\n";
|
|
|
|
|
$event .= "Event-Name: CUSTOM\n";
|
|
|
|
|
$event .= "Event-Subclass: fusion::file\n";
|
2017-09-20 18:19:56 -04:00
|
|
|
$event .= "API-Command: cache\n";
|
2017-07-22 13:18:03 -06:00
|
|
|
$event .= "API-Command-Argument: flush\n";
|
2023-12-02 20:16:18 -04:00
|
|
|
event_socket::command($event);
|
2016-04-25 20:30:23 -05:00
|
|
|
|
2017-07-22 13:18:03 -06:00
|
|
|
//remove the cache
|
2024-02-09 13:21:55 -04:00
|
|
|
recursive_delete($this->location);
|
2019-12-04 13:49:02 -07:00
|
|
|
|
|
|
|
|
//set message
|
|
|
|
|
$result = '+OK cache flushed';
|
2017-07-22 13:18:03 -06:00
|
|
|
}
|
2016-04-25 20:30:23 -05:00
|
|
|
|
2019-10-15 13:54:55 -06:00
|
|
|
//return result
|
2016-04-25 20:30:23 -05:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 18:40:36 +01:00
|
|
|
?>
|