キャッシュの有効期限+削除関数の追加

This commit is contained in:
2026-04-27 20:42:43 +09:00
parent 9c753e5d09
commit 9d6e9ccf9b

View File

@@ -6,6 +6,7 @@ use Std\Lib\Curl;
class Openprovider {
protected string $dataDir = ROOT."/data/cache/";
protected int $tokenDuration = 172800; // 48時間
protected int $dataDuration = 1800; // 30分
private string $user = '';
private string $pass = '';
@@ -93,7 +94,7 @@ class Openprovider {
public function listDomains(array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache('listdomains');
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache);
$curl = $this->setupCurl('/domains');
$res = $this->curlResult($curl);
@@ -206,8 +207,8 @@ class Openprovider {
*/
public function listCustomers(array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache('listcustomer');
if (!empty($cache) && (isset($cache['query']) && $cache['query'] === $query)) return \Result::Success('', $cache);
$cache = $this->getCache('listcustomers');
if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache);
foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
$uri = '/customers?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
@@ -215,7 +216,7 @@ class Openprovider {
$res = $this->curlResult($curl);
if (isset($res->data['data'])) {
$res->data['query'] = $query;
$this->setCache('listcustomer', $res->data);
$this->setCache('listcustomers', $res->data);
return \Result::Success('', $res->data);
}
@@ -233,7 +234,7 @@ class Openprovider {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cacheName = "getcustomer-{$handle}";
$cache = $this->getCache($cacheName);
if (!empty($cache) && (isset($cache['handle']) && $cache['handle'] === $handle)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache, $handle, 'handle')) return \Result::Success('', $cache);
$query = [ 'with_additional_data' => $withAdditionalData ? 'true' : 'false' ];
$uri = "/customers/{$handle}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
@@ -266,7 +267,7 @@ class Openprovider {
$cacheName = $name && $ext ? "domainprices-{$name}.{$ext}" : 'domainprices';
$cache = $this->getCache($cacheName);
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
$uri = "/domains/prices?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
$curl = $this->setupCurl($uri);
@@ -292,7 +293,7 @@ class Openprovider {
public function listTlds(array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache('listtlds');
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ];
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -318,7 +319,7 @@ class Openprovider {
public function getTld(string $tld, array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache("gettld-{$tld}");
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ];
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -359,7 +360,7 @@ class Openprovider {
public function listDnsZones(array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache('listdnszones');
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache);
if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ];
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -385,7 +386,7 @@ class Openprovider {
public function getDnsZone(string $domain, array $query = []): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache("getdnszone-{$domain}");
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
if (empty($query)) $query = [ 'limit' => 100, 'offset' => 0, 'order_by.name' => 'asc' ];
foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -412,7 +413,7 @@ class Openprovider {
public function listZoneRecords(string $domain): \Result {
if (!OPENPROVIDER_ENABLED) return \Result::error('エラーOpenProviderは無効です。');
$cache = $this->getCache("getzonerecords-{$domain}");
if (!empty($cache)) return \Result::Success('', $cache);
if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
$curl = $this->setupCurl("/dns/zones/{$domain}/records");
$res = $this->curlResult($curl);
@@ -502,6 +503,8 @@ class Openprovider {
if (!mkdir($this->dataDir, 0755))
return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。');
}
$data['lifespan'] = time();
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if (file_put_contents("{$this->dataDir}{$type}.json", $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
@@ -530,4 +533,32 @@ class Openprovider {
return $data;
}
/**
* キャッシュを破壊する
*
* @param string $name キャッシュのファイル名
* @return Result
*/
private function murderCache(string $name): \Result {
if (!file_exists("{$this->dataDir}{$name}.json")) return \Result::Error('キャッシュファイルが存在しない。');
if (!unlink("{$this->dataDir}{$name}.json")) return \Result::Error('キャッシュファイルの削除に失敗。');
return \Result::Success();
}
/**
* キャッシュ有効期限の確認
*
* @param array $cache
* @param mixed $data デフォルトnull
* @param string $dataPoint デフォルトquery
* @return bool
*/
private function isValidLifespan(array $cache, mixed $data = null, string $dataPoint = 'query'): bool {
if (NULL === $data) return (!empty($cache) && (isset($cache['lifespan']) && time() < ($cache['lifespan'] + $this->dataDuration)));
else return (!empty($cache)
&& (isset($cache[$dataPoint])
&& $cache[$dataPoint] === $data)
&& (isset($cache['lifespan']) && time() < ($cache['lifespan'] + $this->dataDuration)));
}
}