2015-01-15 18:15:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* cache class provides an abstracted cache
|
2016-03-11 12:08:26 +00:00
|
|
|
*
|
2015-01-15 18:19:54 +00:00
|
|
|
* @method string set
|
|
|
|
|
* @method string get
|
|
|
|
|
* @method string delete
|
|
|
|
|
* @method string flush
|
2015-01-15 18:15:25 +00:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
*/
|
2015-01-15 19:24:54 +00:00
|
|
|
public function set($key, $value) {
|
2015-12-24 13:19:53 +03:00
|
|
|
// connect to event socket
|
2015-01-15 18:15:25 +00:00
|
|
|
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
|
2015-12-24 13:19:53 +03:00
|
|
|
if ($fp === false) {
|
2015-01-15 18:15:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-24 13:19:53 +03:00
|
|
|
|
|
|
|
|
//send a custom event
|
|
|
|
|
|
|
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache set ".$key." ".$value;
|
|
|
|
|
$result = event_socket_request($fp, 'api '.$command);
|
|
|
|
|
|
|
|
|
|
//close event socket
|
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
|
|
// return result
|
|
|
|
|
return $result;
|
2015-01-15 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific item from the cache
|
|
|
|
|
* @var string $key cache id
|
|
|
|
|
*/
|
2015-01-15 19:24:54 +00:00
|
|
|
public function get($key) {
|
2015-12-24 13:19:53 +03:00
|
|
|
// connect to event socket
|
2015-01-15 18:15:25 +00:00
|
|
|
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
|
2015-12-24 13:19:53 +03:00
|
|
|
if ($fp === false) {
|
2015-01-15 18:15:25 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-24 13:19:53 +03:00
|
|
|
|
|
|
|
|
//send a custom event
|
|
|
|
|
|
|
|
|
|
//run the memcache
|
|
|
|
|
$command = "memcache get ".$key;
|
|
|
|
|
$result = event_socket_request($fp, 'api '.$command);
|
|
|
|
|
|
|
|
|
|
//close event socket
|
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
|
|
// return result
|
|
|
|
|
return $result;
|
2015-01-15 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete a specific item from the cache
|
|
|
|
|
* @var string $key cache id
|
|
|
|
|
*/
|
2015-01-15 19:24:54 +00:00
|
|
|
public function delete($key) {
|
2015-12-24 13:19:53 +03:00
|
|
|
// connect to event socket
|
2015-01-16 03:52:32 +00:00
|
|
|
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
|
2015-12-24 13:19:53 +03:00
|
|
|
if ($fp === false) {
|
|
|
|
|
return false;
|
2015-01-16 03:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-24 13:19:53 +03:00
|
|
|
//send a custom event
|
|
|
|
|
$event = "sendevent CUSTOM\n";
|
|
|
|
|
$event .= "Event-Name: MEMCACHE\n";
|
|
|
|
|
$event .= "Event-Subclass: delete\n";
|
|
|
|
|
$event .= "API-Command: memcache\n";
|
|
|
|
|
$event .= "API-Command-Argument: delete ".$key."\n";
|
|
|
|
|
event_socket_request($fp, $event);
|
|
|
|
|
|
2015-01-15 18:15:25 +00:00
|
|
|
//run the memcache
|
2015-12-24 13:19:53 +03:00
|
|
|
$command = "memcache delete ".$key;
|
|
|
|
|
$result = event_socket_request($fp, 'api '.$command);
|
|
|
|
|
|
|
|
|
|
//close event socket
|
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
|
|
// return result
|
|
|
|
|
return $result;
|
2015-01-15 18:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete the entire cache
|
|
|
|
|
*/
|
2015-01-15 19:24:54 +00:00
|
|
|
public function flush() {
|
2015-12-24 13:19:53 +03:00
|
|
|
// connect to event socket
|
2015-01-16 08:25:54 +00:00
|
|
|
$fp = event_socket_create($_SESSION['event_socket_ip_address'], $_SESSION['event_socket_port'], $_SESSION['event_socket_password']);
|
2015-12-24 13:19:53 +03:00
|
|
|
if ($fp === false) {
|
|
|
|
|
return false;
|
2015-01-16 08:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
2015-12-24 13:19:53 +03:00
|
|
|
//send a custom event
|
|
|
|
|
$event = "sendevent CUSTOM\n";
|
|
|
|
|
$event .= "Event-Name: MEMCACHE\n";
|
|
|
|
|
$event .= "Event-Subclass: flush\n";
|
|
|
|
|
$event .= "API-Command: memcache\n";
|
|
|
|
|
$event .= "API-Command-Argument: flush\n";
|
|
|
|
|
event_socket_request($fp, $event);
|
|
|
|
|
|
2015-01-15 18:15:25 +00:00
|
|
|
//run the memcache
|
2015-12-24 13:19:53 +03:00
|
|
|
$command = "memcache flush";
|
|
|
|
|
$result = event_socket_request($fp, 'api '.$command);
|
|
|
|
|
|
|
|
|
|
//close event socket
|
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
|
|
// return result
|
|
|
|
|
return $result;
|
2015-01-15 18:15:25 +00:00
|
|
|
}
|
2015-01-15 19:22:39 +00:00
|
|
|
}
|
2015-01-15 18:15:25 +00:00
|
|
|
|
|
|
|
|
?>
|