diff options
author | Andrew Dolgov <[email protected]> | 2022-11-24 18:50:48 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2022-11-24 18:50:48 +0300 |
commit | 94e90cbc8ff9b0ee2a7f4490830c5b10817a308e (patch) | |
tree | c9dd5ad2ec002b3b4e0aaa123e1572c5b5c4abed /init.php | |
parent | 594f6cab1a2f467cf180b382bd76aff2b8591036 (diff) |
implement lifecycle policy handling, code cleanup
Diffstat (limited to 'init.php')
-rw-r--r-- | init.php | 91 |
1 files changed, 43 insertions, 48 deletions
@@ -6,9 +6,9 @@ use \Aws\Exception\AwsException; class Cache_S3 extends Plugin implements Cache_Adapter { - /** @var S3Client $s3 */ - private $s3; - private $dir; + /** @phpstan-ignore-next-line */ + private S3Client $s3; + private string $dir; const CACHE_S3_ENDPOINT = "CACHE_S3_ENDPOINT"; const CACHE_S3_BUCKET = "CACHE_S3_BUCKET"; @@ -30,6 +30,7 @@ class Cache_S3 extends Plugin implements Cache_Adapter { Config::add(self::CACHE_S3_ACCESS_KEY, "", Config::T_STRING); Config::add(self::CACHE_S3_SECRET_KEY, "", Config::T_STRING); + /** @phpstan-ignore-next-line */ $this->s3 = new Aws\S3\S3Client([ 'version' => 'latest', 'region' => Config::get(self::CACHE_S3_REGION), @@ -41,6 +42,7 @@ class Cache_S3 extends Plugin implements Cache_Adapter { ], ]); + /** @phpstan-ignore-next-line */ $this->s3->registerStreamWrapper(); } @@ -50,22 +52,6 @@ class Cache_S3 extends Plugin implements Cache_Adapter { public function set_dir(string $dir): void { $this->dir = $dir; - - /* $this->s3->putBucketLifecycleConfiguration([ - 'Bucket' => Config::get(self::CACHE_S3_BUCKET), - 'LifecycleConfiguration' => [ - 'Rules' => [ - [ - 'Expiration' => [ - 'Days' => (int)Config::CACHE_MAX_DAYS, - 'ExpiredObjectDeleteMarker' => false, - ], - 'Prefix' => $this->dir, - 'Status' => 'Enabled' - ], - ], - ] - ]); */ } public function get_dir(): string { @@ -81,8 +67,7 @@ class Cache_S3 extends Plugin implements Cache_Adapter { } public function exists(string $filename): bool { - return $this->s3->doesObjectExist(Config::get(self::CACHE_S3_BUCKET), - $this->dir . '/' . $filename); + return file_exists($this->get_full_path($filename)); } public function get_size(string $filename) { @@ -90,35 +75,11 @@ class Cache_S3 extends Plugin implements Cache_Adapter { } public function put(string $filename, $data) { - try { - $this->s3->putObject([ - 'Bucket' => Config::get(self::CACHE_S3_BUCKET), - 'Key' => $this->dir . '/' . $filename, - 'Body' => $data - ]); - - // couldn't find something like 'uploaded bytes' in \Aws\Result - return strlen($data); - - } catch (AwsException $e) { - user_error($e, E_USER_WARNING); - return false; - } + return file_put_contents($this->get_full_path($filename), $data); } public function get(string $filename): ?string { - try { - $res = $this->s3->getObject([ - 'Bucket' => Config::get(self::CACHE_S3_BUCKET), - 'Key' => $this->dir . '/' . $filename - ]); - - return $res['Body']->__toString(); - } catch (AwsException $e) { - user_error($e, E_USER_WARNING); - - return null; - } + return file_get_contents($this->get_full_path($filename)); } public function get_full_path(string $filename): string { @@ -144,7 +105,41 @@ class Cache_S3 extends Plugin implements Cache_Adapter { } public function expire_all(): void { - // TODO + $dh = opendir('s3://' . Config::get(self::CACHE_S3_BUCKET)); + $rules = []; + + if ($dh) { + while (($file = readdir($dh)) !== false) { + if (is_dir($this->get_full_path($file))) { + array_push($rules, + [ + 'ID' => $file, + 'Expiration' => [ + 'Days' => (int) Config::get(Config::CACHE_MAX_DAYS), + ], + 'Filter' => [ + 'Prefix' => $file + ], + 'Status' => 'Enabled' + ] + ); + } + } + } + + try { + /** @phpstan-ignore-next-line */ + $this->s3->putBucketLifecycleConfiguration([ + 'Bucket' => Config::get(self::CACHE_S3_BUCKET), + 'LifecycleConfiguration' => [ + 'Rules' => $rules + ] + ]); + + Debug::log("Set lifecycle policy on " . count($rules) . " prefixe(s)."); + } catch (Exception $e) { + user_error($e, E_USER_WARNING); + } } function api_version() { |