Skip to content

Commit

Permalink
добавлено удаление по времени
Browse files Browse the repository at this point in the history
  • Loading branch information
FunnyRain committed Feb 9, 2021
1 parent c487110 commit c654680
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
22 changes: 19 additions & 3 deletions cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
}

}
2 changes: 1 addition & 1 deletion cache/temp_wallIDS.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[110848,110840,110834,110832,110831,110828,110827,110825,110817,110815,110853,110866,110858,110857,110877]
{"isRemove":false,"deleteTime":1612882798,"temp":[110917,110916,110915,110913,110908,110903,110902,110899,110896,110893]}
4 changes: 2 additions & 2 deletions src/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
4 changes: 2 additions & 2 deletions src/Wall.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit c654680

Please sign in to comment.