fusionpbx/resources/classes/cache.php

222 lines
5.7 KiB
PHP
Raw Normal View History

<?php
/**
* cache class provides an abstracted cache
*
* @method string set
* @method string get
* @method string delete
* @method string flush
*/
class cache {
/**
* Called when the object is created
*/
public function __construct() {
//place holder
}
/**
* Called when there are no references to a particular object
* unset the variables used in the class
*/
public function __destruct() {
foreach ($this as $key => $value) {
unset($this->$key);
}
}
/**
* 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 ($_SESSION['cache']['method']['text'] == "memcache") {
//connect to event socket
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === false) {
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_request($fp, 'api '.$command);
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//close event socket
fclose($fp);
}
2019-07-30 08:11:50 +02:00
//save to the file cache
if ($_SESSION['cache']['method']['text'] == "file") {
2021-10-26 08:30:30 +02:00
$result = file_put_contents($_SESSION['cache']['location']['text'] . "/" . $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);
2019-07-30 08:11:50 +02:00
//cache method memcache
if ($_SESSION['cache']['method']['text'] == "memcache") {
// connect to event socket
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === false) {
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_request($fp, 'api '.$command);
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//close event socket
fclose($fp);
}
2019-10-15 21:54:55 +02:00
2019-07-30 08:11:50 +02:00
//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);
}
}
2019-10-15 21:54:55 +02:00
//return result
return $result;
}
/**
* Delete a specific item from the cache
* @var string $key cache id
*/
public function delete($key) {
2017-07-22 21:18:03 +02:00
//cache method memcache
2017-09-02 17:56:30 +02:00
if ($_SESSION['cache']['method']['text'] == "memcache") {
2021-09-04 06:35:33 +02:00
//connect to event socket
2017-07-22 21:18:03 +02:00
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === 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_request($fp, $event);
//run the memcache
$command = "memcache delete ".$key;
$result = event_socket_request($fp, 'api '.$command);
//close event socket
fclose($fp);
}
2017-07-22 23:52:45 +02:00
//cache method file
if ($_SESSION['cache']['method']['text'] == "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
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === 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_request($fp, $event);
2018-07-24 23:55:56 +02:00
//remove the local files
2020-11-05 17:29:41 +01:00
foreach (glob($_SESSION['cache']['location']['text'] . "/" . $key) as $file) {
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() {
2017-07-22 21:18:03 +02:00
//cache method memcache
2017-07-22 23:52:45 +02:00
if ($_SESSION['cache']['method']['text'] == "memcache") {
2017-07-22 21:18:03 +02:00
// connect to event socket
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === false) {
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_request($fp, $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_request($fp, 'api '.$command);
2018-07-24 23:55:56 +02:00
2017-07-22 21:18:03 +02:00
//close event socket
fclose($fp);
}
2017-07-22 21:18:03 +02:00
//cache method file
2017-07-22 23:52:45 +02:00
if ($_SESSION['cache']['method']['text'] == "file") {
2021-10-14 08:41:56 +02:00
// connect to event socket
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
if ($fp === false) {
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_request($fp, $event);
2017-07-22 21:18:03 +02:00
//remove the cache
recursive_delete($_SESSION['cache']['location']['text']);
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;
}
}
?>