キャッシュの有効期限+削除関数の追加
諏訪子 <suwako@076.moe>、2026-04-27 20:42
コミット 9d6e9ccf9b9a69e05200da58fa05dceabbdc42ff
・親 9c753e5
・ファイルを見る
1個のファイル、+42 -11
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | class Openprovider { |
| 7 | 7 | protected string $dataDir = ROOT."/data/cache/"; |
| 8 | 8 | protected int $tokenDuration = 172800; // 48時間 |
| 9 | + protected int $dataDuration = 1800; // 30分 | |
| 9 | 10 | |
| 10 | 11 | private string $user = ''; |
| 11 | 12 | private string $pass = ''; |
| @@ -93,7 +94,7 @@ | ||
| 93 | 94 | public function listDomains(array $query = []): \Result { |
| 94 | 95 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 95 | 96 | $cache = $this->getCache('listdomains'); |
| 96 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 97 | + if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache); | |
| 97 | 98 | |
| 98 | 99 | $curl = $this->setupCurl('/domains'); |
| 99 | 100 | $res = $this->curlResult($curl); |
| @@ -206,8 +207,8 @@ | ||
| 206 | 207 | */ |
| 207 | 208 | public function listCustomers(array $query = []): \Result { |
| 208 | 209 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 209 | - $cache = $this->getCache('listcustomer'); | |
| 210 | - if (!empty($cache) && (isset($cache['query']) && $cache['query'] === $query)) return \Result::Success('', $cache); | |
| 210 | + $cache = $this->getCache('listcustomers'); | |
| 211 | + if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache); | |
| 211 | 212 | |
| 212 | 213 | foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; } |
| 213 | 214 | $uri = '/customers?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986); |
| @@ -215,7 +216,7 @@ | ||
| 215 | 216 | $res = $this->curlResult($curl); |
| 216 | 217 | if (isset($res->data['data'])) { |
| 217 | 218 | $res->data['query'] = $query; |
| 218 | - $this->setCache('listcustomer', $res->data); | |
| 219 | + $this->setCache('listcustomers', $res->data); | |
| 219 | 220 | return \Result::Success('', $res->data); |
| 220 | 221 | } |
| 221 | 222 | |
| @@ -233,7 +234,7 @@ | ||
| 233 | 234 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 234 | 235 | $cacheName = "getcustomer-{$handle}"; |
| 235 | 236 | $cache = $this->getCache($cacheName); |
| 236 | - if (!empty($cache) && (isset($cache['handle']) && $cache['handle'] === $handle)) return \Result::Success('', $cache); | |
| 237 | + if ($this->isValidLifespan($cache, $handle, 'handle')) return \Result::Success('', $cache); | |
| 237 | 238 | |
| 238 | 239 | $query = [ 'with_additional_data' => $withAdditionalData ? 'true' : 'false' ]; |
| 239 | 240 | $uri = "/customers/{$handle}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986); |
| @@ -266,7 +267,7 @@ | ||
| 266 | 267 | $cacheName = $name && $ext ? "domainprices-{$name}.{$ext}" : 'domainprices'; |
| 267 | 268 | |
| 268 | 269 | $cache = $this->getCache($cacheName); |
| 269 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 270 | + if ($this->isValidLifespan($cache)) return \Result::Success('', $cache); | |
| 270 | 271 | |
| 271 | 272 | $uri = "/domains/prices?".http_build_query($query, '', '&', PHP_QUERY_RFC3986); |
| 272 | 273 | $curl = $this->setupCurl($uri); |
| @@ -292,7 +293,7 @@ | ||
| 292 | 293 | public function listTlds(array $query = []): \Result { |
| 293 | 294 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 294 | 295 | $cache = $this->getCache('listtlds'); |
| 295 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 296 | + if ($this->isValidLifespan($cache)) return \Result::Success('', $cache); | |
| 296 | 297 | |
| 297 | 298 | if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ]; |
| 298 | 299 | foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0'; |
| @@ -318,7 +319,7 @@ | ||
| 318 | 319 | public function getTld(string $tld, array $query = []): \Result { |
| 319 | 320 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 320 | 321 | $cache = $this->getCache("gettld-{$tld}"); |
| 321 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 322 | + if ($this->isValidLifespan($cache)) return \Result::Success('', $cache); | |
| 322 | 323 | |
| 323 | 324 | if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ]; |
| 324 | 325 | foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0'; |
| @@ -359,7 +360,7 @@ | ||
| 359 | 360 | public function listDnsZones(array $query = []): \Result { |
| 360 | 361 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 361 | 362 | $cache = $this->getCache('listdnszones'); |
| 362 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 363 | + if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache); | |
| 363 | 364 | |
| 364 | 365 | if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ]; |
| 365 | 366 | foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0'; |
| @@ -385,7 +386,7 @@ | ||
| 385 | 386 | public function getDnsZone(string $domain, array $query = []): \Result { |
| 386 | 387 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 387 | 388 | $cache = $this->getCache("getdnszone-{$domain}"); |
| 388 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 389 | + if ($this->isValidLifespan($cache)) return \Result::Success('', $cache); | |
| 389 | 390 | |
| 390 | 391 | if (empty($query)) $query = [ 'limit' => 100, 'offset' => 0, 'order_by.name' => 'asc' ]; |
| 391 | 392 | foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0'; |
| @@ -412,7 +413,7 @@ | ||
| 412 | 413 | public function listZoneRecords(string $domain): \Result { |
| 413 | 414 | if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。'); |
| 414 | 415 | $cache = $this->getCache("getzonerecords-{$domain}"); |
| 415 | - if (!empty($cache)) return \Result::Success('', $cache); | |
| 416 | + if ($this->isValidLifespan($cache)) return \Result::Success('', $cache); | |
| 416 | 417 | |
| 417 | 418 | $curl = $this->setupCurl("/dns/zones/{$domain}/records"); |
| 418 | 419 | $res = $this->curlResult($curl); |
| @@ -502,6 +503,8 @@ | ||
| 502 | 503 | if (!mkdir($this->dataDir, 0755)) |
| 503 | 504 | return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。'); |
| 504 | 505 | } |
| 506 | + | |
| 507 | + $data['lifespan'] = time(); | |
| 505 | 508 | $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 506 | 509 | if (file_put_contents("{$this->dataDir}{$type}.json", $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。'); |
| 507 | 510 | |
| @@ -529,5 +532,33 @@ | ||
| 529 | 532 | } |
| 530 | 533 | |
| 531 | 534 | return $data; |
| 535 | + } | |
| 536 | + | |
| 537 | + /** | |
| 538 | + * キャッシュを破壊する | |
| 539 | + * | |
| 540 | + * @param string $name キャッシュのファイル名 | |
| 541 | + * @return Result | |
| 542 | + */ | |
| 543 | + private function murderCache(string $name): \Result { | |
| 544 | + if (!file_exists("{$this->dataDir}{$name}.json")) return \Result::Error('キャッシュファイルが存在しない。'); | |
| 545 | + if (!unlink("{$this->dataDir}{$name}.json")) return \Result::Error('キャッシュファイルの削除に失敗。'); | |
| 546 | + return \Result::Success(); | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 550 | + * キャッシュ有効期限の確認 | |
| 551 | + * | |
| 552 | + * @param array $cache | |
| 553 | + * @param mixed $data デフォルト=null | |
| 554 | + * @param string $dataPoint デフォルト=query | |
| 555 | + * @return bool | |
| 556 | + */ | |
| 557 | + private function isValidLifespan(array $cache, mixed $data = null, string $dataPoint = 'query'): bool { | |
| 558 | + if (NULL === $data) return (!empty($cache) && (isset($cache['lifespan']) && time() < ($cache['lifespan'] + $this->dataDuration))); | |
| 559 | + else return (!empty($cache) | |
| 560 | + && (isset($cache[$dataPoint]) | |
| 561 | + && $cache[$dataPoint] === $data) | |
| 562 | + && (isset($cache['lifespan']) && time() < ($cache['lifespan'] + $this->dataDuration))); | |
| 532 | 563 | } |
| 533 | 564 | } |