fusionpbx/resources/classes/cache.php

252 lines
5.7 KiB
PHP
Raw Normal View History

<?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) {
2021-10-26 08:30:30 +02:00
//change the delimiter
$key = str_replace(":", ".", $key);
2019-07-30 08:11:50 +02:00
//save to memcache
if ($this->method === "memcache") {
2019-07-30 08:11:50 +02:00
//connect to event socket
$esl = event_socket::create();
if ($esl === false) {
2019-07-30 08:11:50 +02:00
return false;
}
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//run the memcache
$command = "memcache set ".$key." ".$value;
$result = event_socket::api($command);
2019-10-15 21:54:55 +02:00
}
2019-07-30 08:11:50 +02:00
//save to the file cache
if ($this->method === "file") {
$result = file_put_contents($this->location . "/" . $key, $value);
2019-07-30 08:11:50 +02:00
}
2019-10-15 21:54:55 +02:00
//return result
return $result;
}
/**
* Get a specific item from the cache
* @var string $key cache id
*/
public function get($key) {
2019-11-19 02:01:10 +01:00
2021-10-26 08:30:30 +02:00
//change the delimiter
$key = str_replace(":", ".", $key);
//cache method memcache
if ($this->method === "memcache") {
2019-07-30 08:11:50 +02:00
// connect to event socket
$esl = event_socket::create();
if (!$esl->is_connected()) {
2019-07-30 08:11:50 +02:00
return false;
}
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//send a custom event
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//run the memcache
$command = "memcache get ".$key;
$result = event_socket::api($command);
2019-10-15 21:54:55 +02:00
}
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//get the file cache
if ($this->method === "file") {
if (file_exists($this->location . "/" . $key)) {
$result = file_get_contents($this->location . "/" . $key);
2019-07-30 08:11:50 +02:00
}
}
2019-10-15 21:54:55 +02:00
//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") {
2021-09-04 06:35:33 +02:00
//connect to event socket
$esl = event_socket::create();
if ($esl === false) {
2017-07-22 21:18:03 +02: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";
event_socket::command($event);
2017-07-22 21:18:03 +02:00
//run the memcache
$command = "memcache delete ".$key;
$result = event_socket::api($command);
2017-07-22 21:18:03 +02:00
}
2017-07-22 23:52:45 +02:00
//cache method file
if ($this->method === "file") {
2018-08-11 01:41:02 +02:00
//change the delimiter
$key = str_replace(":", ".", $key);
2019-11-19 02:01:10 +01:00
//connect to event socket
$esl = event_socket::create();
if ($esl === false) {
return false;
}
2018-07-24 23:55:56 +02: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";
event_socket::command($event);
2018-07-24 23:55:56 +02:00
//remove the local files
foreach (glob($this->location . "/" . $key) as $file) {
2020-11-05 17:29:41 +01:00
if (file_exists($file)) {
unlink($file);
}
if (file_exists($file)) {
unlink($file . ".tmp");
}
}
2017-07-22 21:18:03 +02:00
}
}
/**
* 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();
}
//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);
}
//cache method memcache
if ($this->method === "memcache") {
2017-07-22 21:18:03 +02:00
// connect to event socket
$esl = event_socket::create();
if ($esl === false) {
2017-07-22 21:18:03 +02:00
return false;
}
2018-07-24 23:55:56 +02:00
2017-07-22 21:18:03 +02: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";
event_socket::command($event);
2018-07-24 23:55:56 +02:00
2017-07-22 21:18:03 +02:00
//run the memcache
$command = "memcache flush";
$result = event_socket::api($command);
2018-07-24 23:55:56 +02:00
}
//cache method file
if ($this->method === "file") {
2021-10-14 08:41:56 +02:00
// connect to event socket
$esl = event_socket::create();
if ($esl === false) {
2021-10-14 08:41:56 +02:00
return false;
}
2017-07-22 21:18:03 +02: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";
2017-07-22 21:18:03 +02:00
$event .= "API-Command-Argument: flush\n";
event_socket::command($event);
2017-07-22 21:18:03 +02:00
//remove the cache
recursive_delete($this->location);
2019-12-04 21:49:02 +01:00
//set message
$result = '+OK cache flushed';
2017-07-22 21:18:03 +02:00
}
2019-10-15 21:54:55 +02:00
//return result
return $result;
}
}
?>