Ver Fonte

Update Forums

Move the work with the cache to the manager.
Visman há 4 anos atrás
pai
commit
614f6c87ae
2 ficheiros alterados com 23 adições e 22 exclusões
  1. 21 11
      app/Models/Forum/Manager.php
  2. 2 11
      app/Models/Forum/Refresh.php

+ 21 - 11
app/Models/Forum/Manager.php

@@ -27,6 +27,7 @@ class Manager extends ManagerModel
 
     /**
      * Инициализация списка разделов
+     * Обновляет кеш разделов
      */
     public function init(Group $group = null): Manager
     {
@@ -37,25 +38,34 @@ class Manager extends ManagerModel
         }
 
         $mark = $this->c->Cache->get('forums_mark');
+
         if (empty($mark)) {
-            if (true !== $this->c->Cache->set('forums_mark', \time())) {
+            $mark = \time();
+
+            if (true !== $this->c->Cache->set('forums_mark', $mark)) {
                 throw new RuntimeException('Unable to write value to cache - forums_mark');
             }
 
-            $list = $this->refresh($group);
+            $result = [];
         } else {
-            $result = $this->c->Cache->get('forums_' . $gid);
-            if (
-                empty($result['time'])
-                || $result['time'] < $mark
-            ) {
-                $list = $this->refresh($group);
-            } else {
-                $list = $result['list'];
+            $result = $this->c->Cache->get("forums_{$gid}");
+        }
+
+        if (
+            ! isset($result['time'], $result['list'])
+            || $result['time'] < $mark
+        ) {
+            $result = [
+                'time' => $mark,
+                'list' => $this->refresh($group),
+            ];
+
+            if (true !== $this->c->Cache->set("forums_{$gid}", $result)) {
+                throw new RuntimeException('Unable to write value to cache - forums_' . $gid);
             }
         }
 
-        $this->forumList = $list;
+        $this->forumList = $result['list'];
 
         return $this;
     }

+ 2 - 11
app/Models/Forum/Refresh.php

@@ -6,7 +6,6 @@ namespace ForkBB\Models\Forum;
 
 use ForkBB\Models\Action;
 use ForkBB\Models\Group\Model as Group;
-use RuntimeException;
 
 class Refresh extends Action
 {
@@ -17,7 +16,6 @@ class Refresh extends Action
 
     /**
      * Возвращает список доступных разделов для группы
-     * Обновляет кеш
      */
     public function refresh(Group $group = null): array
     {
@@ -29,6 +27,8 @@ class Refresh extends Action
             $read = $group->g_read_board;
         }
 
+        $this->list = [];
+
         if ('1' == $read) {
             $list  = [];
             $vars  = [
@@ -53,15 +53,6 @@ class Refresh extends Action
             }
         }
 
-        $result = $this->c->Cache->set('forums_' . $gid, [
-            'time' => \time(),
-            'list' => $this->list,
-        ]);
-
-        if (true !== $result) {
-            throw new RuntimeException('Unable to write value to cache - forums_' . $gid);
-        }
-
         return $this->list;
     }