final - speedup code for api
This commit is contained in:
@@ -37,6 +37,10 @@ class UrbackupApiClient
|
||||
|
||||
private int $lastlogid = 0;
|
||||
|
||||
private ?array $cached_status = null;
|
||||
|
||||
private array $cached_settings = [];
|
||||
|
||||
private string $server_version = '';
|
||||
|
||||
private bool $is_version_2_4_or_higher = false;
|
||||
@@ -247,21 +251,29 @@ class UrbackupApiClient
|
||||
*/
|
||||
public function getStatus(): array
|
||||
{
|
||||
if ($this->cached_status !== null) {
|
||||
return $this->cached_status;
|
||||
}
|
||||
|
||||
$data = $this->apiAction('status');
|
||||
|
||||
if (isset($data['status']) && is_array($data['status'])) {
|
||||
return array_values($data['status']);
|
||||
$this->cached_status = array_values($data['status']);
|
||||
return $this->cached_status;
|
||||
}
|
||||
|
||||
if (isset($data['clients']) && is_array($data['clients'])) {
|
||||
return array_values($data['clients']);
|
||||
$this->cached_status = array_values($data['clients']);
|
||||
return $this->cached_status;
|
||||
}
|
||||
|
||||
if (array_is_list($data)) {
|
||||
return $data;
|
||||
$this->cached_status = $data;
|
||||
return $this->cached_status;
|
||||
}
|
||||
|
||||
return [];
|
||||
$this->cached_status = [];
|
||||
return $this->cached_status;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,16 +329,22 @@ class UrbackupApiClient
|
||||
return [];
|
||||
}
|
||||
|
||||
if (isset($this->cached_settings[$client_id])) {
|
||||
return $this->cached_settings[$client_id];
|
||||
}
|
||||
|
||||
$data = $this->apiAction('settings', [
|
||||
'sa' => 'clientsettings',
|
||||
't_clientid' => $client_id,
|
||||
]);
|
||||
|
||||
if (isset($data['settings']) && is_array($data['settings'])) {
|
||||
return $data['settings'];
|
||||
$this->cached_settings[$client_id] = $data['settings'];
|
||||
return $this->cached_settings[$client_id];
|
||||
}
|
||||
|
||||
return $data;
|
||||
$this->cached_settings[$client_id] = $data;
|
||||
return $this->cached_settings[$client_id];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,6 +356,11 @@ class UrbackupApiClient
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function clearSettingsCache(int $client_id): void
|
||||
{
|
||||
unset($this->cached_settings[$client_id]);
|
||||
}
|
||||
|
||||
public function changeClientSetting(string $client_name, string $key, mixed $value): bool
|
||||
{
|
||||
$client_id = $this->getClientIdByName($client_name);
|
||||
@@ -353,6 +376,7 @@ class UrbackupApiClient
|
||||
$key => (string) $value,
|
||||
]);
|
||||
|
||||
$this->clearSettingsCache($client_id);
|
||||
return $this->responseIsSuccess($data);
|
||||
}
|
||||
|
||||
@@ -371,6 +395,7 @@ class UrbackupApiClient
|
||||
$key => $value,
|
||||
]);
|
||||
|
||||
$this->clearSettingsCache($client_id);
|
||||
return $this->responseIsSuccess($data);
|
||||
}
|
||||
|
||||
@@ -393,6 +418,7 @@ class UrbackupApiClient
|
||||
|
||||
$data = $this->apiAction('settings', $params);
|
||||
|
||||
$this->clearSettingsCache($client_id);
|
||||
return $this->responseIsSuccess($data);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user