Use the settings class in the cache class (#6891)

This commit is contained in:
frytimo 2024-02-09 13:21:55 -04:00 committed by GitHub
parent 6890c21e92
commit a17b63e616
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 49 additions and 27 deletions

View File

@ -1,20 +1,42 @@
<?php
/**
* cache class provides an abstracted cache
*
* @method string set
* @method string get
* @method string delete
* @method string flush
* Provides an abstracted cache
*/
class cache {
private $settings;
private $syslog;
private $location;
private $method;
/**
* Called when the object is created
*/
public function __construct() {
//place holder
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);
}
/**
@ -28,7 +50,7 @@ class cache {
$key = str_replace(":", ".", $key);
//save to memcache
if ($_SESSION['cache']['method']['text'] == "memcache") {
if ($this->method === "memcache") {
//connect to event socket
$esl = event_socket::create();
if ($esl === false) {
@ -42,8 +64,8 @@ class cache {
}
//save to the file cache
if ($_SESSION['cache']['method']['text'] == "file") {
$result = file_put_contents($_SESSION['cache']['location']['text'] . "/" . $key, $value);
if ($this->method === "file") {
$result = file_put_contents($this->location . "/" . $key, $value);
}
//return result
@ -60,7 +82,7 @@ class cache {
$key = str_replace(":", ".", $key);
//cache method memcache
if ($_SESSION['cache']['method']['text'] == "memcache") {
if ($this->method === "memcache") {
// connect to event socket
$esl = event_socket::create();
if (!$esl->is_connected()) {
@ -76,9 +98,9 @@ class cache {
}
//get the file cache
if ($_SESSION['cache']['method']['text'] == "file") {
if (file_exists($_SESSION['cache']['location']['text'] . "/" . $key)) {
$result = file_get_contents($_SESSION['cache']['location']['text'] . "/" . $key);
if ($this->method === "file") {
if (file_exists($this->location . "/" . $key)) {
$result = file_get_contents($this->location . "/" . $key);
}
}
@ -93,14 +115,14 @@ class cache {
public function delete($key) {
//debug information
if (isset($_SESSION['cache']['syslog']['boolean']) && $_SESSION['cache']['syslog']['boolean'] == "true") {
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 (!empty($_SESSION['cache']['method']['text']) && $_SESSION['cache']['method']['text'] == "memcache") {
if ($this->method === "memcache") {
//connect to event socket
$esl = event_socket::create();
if ($esl === false) {
@ -122,7 +144,7 @@ class cache {
}
//cache method file
if (!empty($_SESSION['cache']['method']['text']) && $_SESSION['cache']['method']['text'] == "file") {
if ($this->method === "file") {
//change the delimiter
$key = str_replace(":", ".", $key);
@ -141,7 +163,7 @@ class cache {
event_socket::command($event);
//remove the local files
foreach (glob($_SESSION['cache']['location']['text'] . "/" . $key) as $file) {
foreach (glob($this->location . "/" . $key) as $file) {
if (file_exists($file)) {
unlink($file);
}
@ -159,14 +181,14 @@ class cache {
public function flush() {
//debug information
if (isset($_SESSION['cache']['syslog']['boolean']) && $_SESSION['cache']['syslog']['boolean'] == "true") {
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 ($_SESSION['cache']['method']['text'] == "memcache") {
if ($this->method === "memcache") {
// connect to event socket
$esl = event_socket::create();
if ($esl === false) {
@ -188,7 +210,7 @@ class cache {
}
//cache method file
if ($_SESSION['cache']['method']['text'] == "file") {
if ($this->method === "file") {
// connect to event socket
$esl = event_socket::create();
if ($esl === false) {
@ -204,7 +226,7 @@ class cache {
event_socket::command($event);
//remove the cache
recursive_delete($_SESSION['cache']['location']['text']);
recursive_delete($this->location);
//set message
$result = '+OK cache flushed';