2019-05-03 12:56:07 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
class MemcachedCache implements CacheInterface
|
|
|
|
{
|
|
|
|
private $scope;
|
|
|
|
private $key;
|
|
|
|
private $conn;
|
|
|
|
private $expiration = 0;
|
|
|
|
private $time = false;
|
|
|
|
private $data = null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!extension_loaded('memcached')) {
|
|
|
|
returnServerError('"memcached" extension not loaded. Please check "php.ini"');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-08-02 16:03:54 +03:00
|
|
|
$section = 'MemcachedCache';
|
|
|
|
$host = Configuration::getConfig($section, 'host');
|
|
|
|
$port = Configuration::getConfig($section, 'port');
|
2019-05-03 12:56:07 +03:00
|
|
|
if (empty($host) && empty($port)) {
|
2022-08-02 16:03:54 +03:00
|
|
|
returnServerError('Configuration for ' . $section . ' missing. Please check your ' . FILE_CONFIG);
|
2019-05-03 12:56:07 +03:00
|
|
|
} elseif (empty($host)) {
|
2022-08-02 16:03:54 +03:00
|
|
|
returnServerError('"host" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
2019-05-03 12:56:07 +03:00
|
|
|
} elseif (empty($port)) {
|
2022-08-02 16:03:54 +03:00
|
|
|
returnServerError('"port" param is not set for ' . $section . '. Please check your ' . FILE_CONFIG);
|
2019-05-03 12:56:07 +03:00
|
|
|
} elseif (!ctype_digit($port)) {
|
2022-08-02 16:03:54 +03:00
|
|
|
returnServerError('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
2019-05-03 12:56:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
$port = intval($port);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
if ($port < 1 || $port > 65535) {
|
2022-08-02 16:03:54 +03:00
|
|
|
returnServerError('"port" param is invalid for ' . $section . '. Please check your ' . FILE_CONFIG);
|
2019-05-03 12:56:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
$conn = new Memcached();
|
|
|
|
$conn->addServer($host, $port) or returnServerError('Could not connect to memcached server');
|
|
|
|
$this->conn = $conn;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
public function loadData()
|
|
|
|
{
|
|
|
|
if ($this->data) {
|
|
|
|
return $this->data;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2019-05-03 12:56:07 +03:00
|
|
|
$result = $this->conn->get($this->getCacheKey());
|
|
|
|
if ($result === false) {
|
2020-06-08 12:27:19 +03:00
|
|
|
return null;
|
2019-05-03 12:56:07 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
$this->time = $result['time'];
|
|
|
|
$this->data = $result['data'];
|
|
|
|
return $result['data'];
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
public function saveData($datas)
|
|
|
|
{
|
|
|
|
$time = time();
|
|
|
|
$object_to_save = [
|
|
|
|
'data' => $datas,
|
|
|
|
'time' => $time,
|
|
|
|
];
|
|
|
|
$result = $this->conn->set($this->getCacheKey(), $object_to_save, $this->expiration);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
if ($result === false) {
|
|
|
|
returnServerError('Cannot write the cache to memcached server');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
$this->time = $time;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
public function getTime()
|
|
|
|
{
|
|
|
|
if ($this->time === false) {
|
|
|
|
$this->loadData();
|
|
|
|
}
|
|
|
|
return $this->time;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
public function purgeCache($duration)
|
|
|
|
{
|
|
|
|
// Note: does not purges cache right now
|
|
|
|
// Just sets cache expiration and leave cache purging for memcached itself
|
|
|
|
$this->expiration = $duration;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
/**
|
|
|
|
* Set scope
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setScope($scope)
|
|
|
|
{
|
|
|
|
$this->scope = $scope;
|
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
/**
|
|
|
|
* Set key
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setKey($key)
|
|
|
|
{
|
|
|
|
if (!empty($key) && is_array($key)) {
|
|
|
|
$key = array_map('strtolower', $key);
|
|
|
|
}
|
|
|
|
$key = json_encode($key);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
if (!is_string($key)) {
|
|
|
|
throw new \Exception('The given key is invalid!');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
$this->key = $key;
|
|
|
|
return $this;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
private function getCacheKey()
|
|
|
|
{
|
|
|
|
if (is_null($this->key)) {
|
|
|
|
returnServerError('Call "setKey" first!');
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2019-05-03 12:56:07 +03:00
|
|
|
return 'rss_bridge_cache_' . hash('md5', $this->scope . $this->key . 'A');
|
|
|
|
}
|
|
|
|
}
|