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

諏訪子 <suwako@076.moe>、2026-04-27 20:42
コミット 9d6e9ccf9b9a69e05200da58fa05dceabbdc42ff ・親 9c753e5 ・ファイルを見る

1個のファイル、+42 -11

@@ -6,6 +6,7 @@
66 class Openprovider {
77 protected string $dataDir = ROOT."/data/cache/";
88 protected int $tokenDuration = 172800; // 48時間
9+ protected int $dataDuration = 1800; // 30分
910
1011 private string $user = '';
1112 private string $pass = '';
@@ -93,7 +94,7 @@
9394 public function listDomains(array $query = []): \Result {
9495 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
9596 $cache = $this->getCache('listdomains');
96- if (!empty($cache)) return \Result::Success('', $cache);
97+ if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache);
9798
9899 $curl = $this->setupCurl('/domains');
99100 $res = $this->curlResult($curl);
@@ -206,8 +207,8 @@
206207 */
207208 public function listCustomers(array $query = []): \Result {
208209 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);
211212
212213 foreach ($query as $k => $v) { if (is_bool($v)) $query[$k] = $v ? '1' : '0'; }
213214 $uri = '/customers?'.http_build_query($query, '', '&', PHP_QUERY_RFC3986);
@@ -215,7 +216,7 @@
215216 $res = $this->curlResult($curl);
216217 if (isset($res->data['data'])) {
217218 $res->data['query'] = $query;
218- $this->setCache('listcustomer', $res->data);
219+ $this->setCache('listcustomers', $res->data);
219220 return \Result::Success('', $res->data);
220221 }
221222
@@ -233,7 +234,7 @@
233234 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
234235 $cacheName = "getcustomer-{$handle}";
235236 $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);
237238
238239 $query = [ 'with_additional_data' => $withAdditionalData ? 'true' : 'false' ];
239240 $uri = "/customers/{$handle}?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
@@ -266,7 +267,7 @@
266267 $cacheName = $name && $ext ? "domainprices-{$name}.{$ext}" : 'domainprices';
267268
268269 $cache = $this->getCache($cacheName);
269- if (!empty($cache)) return \Result::Success('', $cache);
270+ if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
270271
271272 $uri = "/domains/prices?".http_build_query($query, '', '&', PHP_QUERY_RFC3986);
272273 $curl = $this->setupCurl($uri);
@@ -292,7 +293,7 @@
292293 public function listTlds(array $query = []): \Result {
293294 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
294295 $cache = $this->getCache('listtlds');
295- if (!empty($cache)) return \Result::Success('', $cache);
296+ if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
296297
297298 if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0, 'order' => 'ASC' ];
298299 foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -318,7 +319,7 @@
318319 public function getTld(string $tld, array $query = []): \Result {
319320 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
320321 $cache = $this->getCache("gettld-{$tld}");
321- if (!empty($cache)) return \Result::Success('', $cache);
322+ if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
322323
323324 if (empty($query)) $query = [ 'limit' => 10, 'offset' => 0 ];
324325 foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -359,7 +360,7 @@
359360 public function listDnsZones(array $query = []): \Result {
360361 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
361362 $cache = $this->getCache('listdnszones');
362- if (!empty($cache)) return \Result::Success('', $cache);
363+ if ($this->isValidLifespan($cache, $query)) return \Result::Success('', $cache);
363364
364365 if (empty($query)) $query = [ 'limit' => 25, 'offset' => 0, 'order_by.name' => 'asc' ];
365366 foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -385,7 +386,7 @@
385386 public function getDnsZone(string $domain, array $query = []): \Result {
386387 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
387388 $cache = $this->getCache("getdnszone-{$domain}");
388- if (!empty($cache)) return \Result::Success('', $cache);
389+ if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
389390
390391 if (empty($query)) $query = [ 'limit' => 100, 'offset' => 0, 'order_by.name' => 'asc' ];
391392 foreach ($query as $k => $v) if (is_bool($v)) $query[$k] = $v ? '1' : '0';
@@ -412,7 +413,7 @@
412413 public function listZoneRecords(string $domain): \Result {
413414 if (!OPENPROVIDER_ENABLED) return \Result::error('エラー:OpenProviderは無効です。');
414415 $cache = $this->getCache("getzonerecords-{$domain}");
415- if (!empty($cache)) return \Result::Success('', $cache);
416+ if ($this->isValidLifespan($cache)) return \Result::Success('', $cache);
416417
417418 $curl = $this->setupCurl("/dns/zones/{$domain}/records");
418419 $res = $this->curlResult($curl);
@@ -502,6 +503,8 @@
502503 if (!mkdir($this->dataDir, 0755))
503504 return \Result::Error('エラー:ユーザーのアイコンディレクトリの作成に失敗。');
504505 }
506+
507+ $data['lifespan'] = time();
505508 $json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
506509 if (file_put_contents("{$this->dataDir}{$type}.json", $json) === false) return \Result::Error('エラー:ユーザーデータの保存に失敗。');
507510
@@ -529,5 +532,33 @@
529532 }
530533
531534 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)));
532563 }
533564 }