Files
data-collection-terminal/management-panel/app/Services/EtcdService.php
2024-08-27 13:35:26 +08:00

55 lines
1.4 KiB
PHP

<?php
namespace App\Services;
use GuzzleHttp\Client;
class EtcdService
{
protected $client;
protected $baseUri = 'http://127.0.0.1:2379/v3/'; // etcd 默认地址
public function __construct()
{
$this->client = new Client(['base_uri' => $this->baseUri]);
}
public function put($key, $value)
{
$this->client->post('kv/put', [
'json' => [
'key' => base64_encode($key),
'value' => base64_encode($value),
],
]);
}
public function get($key)
{
$response = $this->client->post('kv/range', [
'json' => [
'key' => base64_encode($key),
],
]);
return json_decode($response->getBody()->getContents(), true);
}
public function deleteByPrefix($prefix)
{
// Encode the prefix
$encodedPrefix = base64_encode($prefix);
// Calculate the range end by incrementing the last character
$lastChar = $prefix[strlen($prefix) - 1];
$rangeEnd = $prefix . chr(ord($lastChar) + 1);
$encodedRangeEnd = base64_encode($rangeEnd);
// Send the delete request
$this->client->post('kv/deletion_range', [
'json' => [
'key' => $encodedPrefix,
'range_end' => $encodedRangeEnd,
],
]);
}
}