diff --git a/cache/Cache.php b/cache/Cache.php index f4d4211..2e15c5d 100644 --- a/cache/Cache.php +++ b/cache/Cache.php @@ -5,20 +5,36 @@ class Cache { public $path = __DIR__ . '/'; public $filename; - public function __construct(string $filename = 'temp.json') { - if (!file_exists($this->path . $filename)) file_put_contents($this->path . $filename, json_encode([])); + public function __construct(string $filename, int $days = 30) { + if (!file_exists($this->path . $filename)) file_put_contents($this->path . $filename, json_encode([ + 'isRemove' => ($days <= 0) ? false : true, + 'deleteTime' => strtotime("+ {$days} minutes"), + 'temp' => [] + ])); $this->filename = $filename; } public function set($value): void { $array = $this->get(); - array_push($array, $value); + $array['temp'][] = $value; file_put_contents($this->path . $this->filename, json_encode($array)); + + $this->checkDeleteTime(); } public function get(): array { return json_decode(file_get_contents($this->path . $this->filename), 1); } + private function checkDeleteTime(): void { + $array = $this->get(); + if ($array['isRemove'] == true) { + if (time() >= $array['deleteTime']) { + unlink($this->path . $this->filename); + echo "\n=> Временный файл удалён!\n"; + } + } + } + } \ No newline at end of file diff --git a/cache/temp_wallIDS.json b/cache/temp_wallIDS.json index 223e155..231a36e 100644 --- a/cache/temp_wallIDS.json +++ b/cache/temp_wallIDS.json @@ -1 +1 @@ -[110848,110840,110834,110832,110831,110828,110827,110825,110817,110815,110853,110866,110858,110857,110877] \ No newline at end of file +{"isRemove":false,"deleteTime":1612882798,"temp":[110917,110916,110915,110913,110908,110903,110902,110899,110896,110893]} \ No newline at end of file diff --git a/src/Account.php b/src/Account.php index c4551b0..3ae3957 100644 --- a/src/Account.php +++ b/src/Account.php @@ -8,9 +8,9 @@ public function __construct(User $user) { $this->user = $user; } - /*public function getId() { + public function getId() { return $this->user->VkApiRequest()->api('account.getProfileInfo', [])['id']; - }*/ + } public function ban(int $owner_id = 1) { return $this->user->VkApiRequest()->api('account.ban', ['owner_id' => $owner_id]); diff --git a/src/Wall.php b/src/Wall.php index 9a6b840..45069c9 100644 --- a/src/Wall.php +++ b/src/Wall.php @@ -7,7 +7,7 @@ class Wall { public function __construct(User $user) { $this->user = $user; - $this->cache = new Cache('temp_wallIDS.json'); + $this->cache = new Cache('temp_wallIDS.json', 30); } public function listen($owner_id, $call, int $sleep) { @@ -21,7 +21,7 @@ public function listen($owner_id, $call, int $sleep) { foreach ($items as $key => $item) { - if (!in_array($item['id'], $this->cache->get())) { + if (!in_array($item['id'], $this->cache->get()['temp'])) { $this->cache->set($item['id']); $call($item); }