Procházet zdrojové kódy

Optimize the removal of expired bans

Visman před 4 roky
rodič
revize
89ab4f5531

+ 6 - 2
app/Models/BanList/Check.php

@@ -15,10 +15,14 @@ class Check extends Method
      */
     public function check(User $user): bool
     {
+        $now = \time();
+
         // удаление просроченных банов
-        if (! empty($this->model->banList)) { // ???? зачем при каждом запуске проверять просроченность?
+        if (
+            $this->model->firstExpire > 0
+            && $this->model->firstExpire < $now
+        ) {
             $ids = [];
-            $now = \time();
 
             foreach ($this->model->banList as $id => $row) {
                 if (

+ 14 - 4
app/Models/BanList/Load.php

@@ -19,6 +19,7 @@ class Load extends Method
         $emailList = [];
         $ipList    = [];
         $banList   = [];
+        $first     = 0;
 
         $query = 'SELECT b.id, b.username, b.ip, b.email, b.message, b.expire
             FROM ::bans AS b';
@@ -74,13 +75,22 @@ class Load extends Method
                 'message'  => $message,
                 'expire'   => $expire,
             ];
+
+            if (
+                null !== $expire
+                && $expire > 0
+                && $expire < $first
+            ) {
+                $first = $expire;
+            }
         }
 
         return [
-            'banList'   => $banList,
-            'userList'  => $userList,
-            'emailList' => $emailList,
-            'ipList'    => $ipList,
+            'banList'     => $banList,
+            'userList'    => $userList,
+            'emailList'   => $emailList,
+            'ipList'      => $ipList,
+            'firstExpire' => $first,
         ];
     }
 }

+ 6 - 5
app/Models/BanList/Model.php

@@ -17,7 +17,7 @@ class Model extends ParentModel
     {
         $list = $this->c->Cache->get('banlist');
 
-        if (! isset($list['banList'], $list['userList'], $list['emailList'], $list['ipList'])) {
+        if (! isset($list['banList'], $list['userList'], $list['emailList'], $list['ipList'], $list['firstExpire'])) {
             $list = $this->load();
 
             if (true !== $this->c->Cache->set('banlist', $list)) {
@@ -25,10 +25,11 @@ class Model extends ParentModel
             }
         }
 
-        $this->banList   = $list['banList'];
-        $this->userList  = $list['userList'];
-        $this->emailList = $list['emailList'];
-        $this->ipList    = $list['ipList'];
+        $this->banList     = $list['banList'];
+        $this->userList    = $list['userList'];
+        $this->emailList   = $list['emailList'];
+        $this->ipList      = $list['ipList'];
+        $this->firstExpire = $list['firstExpire'];
 
         return $this;
     }