From f8a4788ae260c9898a37c3c3636c854767e1d53f Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@enhancesoft.com>
Date: Mon, 22 Oct 2018 19:02:26 +0000
Subject: [PATCH] Queue: Cached Queue Counts

Clear cached queue counts on queue update and via auto-cron
---
 include/class.queue.php | 12 ++++++++----
 scp/autocron.php        |  2 +-
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/include/class.queue.php b/include/class.queue.php
index 3d2c8f365..e3212c502 100644
--- a/include/class.queue.php
+++ b/include/class.queue.php
@@ -1349,11 +1349,10 @@ class CustomQueue extends VerySimpleModel {
         $nopath = !isset($this->path);
         $path_changed = isset($this->dirty['parent_id']);
 
-        if ($this->dirty) {
+        if ($this->dirty)
             $this->updated = SqlFunction::NOW();
-            // Refetch the queue counts
-            SavedQueue::clearCounts();
-        }
+
+        $clearCounts = ($this->dirty || $this->__new__);
         if (!($rv = parent::save($refetch || $this->dirty)))
             return $rv;
 
@@ -1372,6 +1371,11 @@ class CustomQueue extends VerySimpleModel {
             };
             $move_children($this);
         }
+
+        // Refetch the queue counts
+        if ($clearCounts)
+            SavedQueue::clearCounts();
+
         return $this->columns->saveAll()
             && $this->exports->saveAll()
             && $this->sorts->saveAll();
diff --git a/scp/autocron.php b/scp/autocron.php
index 5f5da4fb2..cc124455f 100644
--- a/scp/autocron.php
+++ b/scp/autocron.php
@@ -47,7 +47,7 @@ require_once(INCLUDE_DIR.'class.cron.php');
 
 // Run tickets count every 3rd run or so... force new count by skipping cached
 // results
-if (mt_rand(1, 12) == 3)
+if ((mt_rand(1, 12) % 3) == 0)
     SavedQueue::counts($thisstaff, array(), false);
 
 // Clear staff obj to avoid false credit internal notes & auto-assignment
-- 
GitLab