From 1b01378120296faf004aeebf1d1ec5a532af4d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E9=87=91=E5=8B=87?= Date: Fri, 25 Oct 2024 01:48:44 +0800 Subject: [PATCH] * [perf] Fix error. --- framework/model.class.php | 2 +- lib/base/cache/cache.class.php | 86 ++++++++++++++++++++++++++++++++++ lib/zredis/zredis.class.php | 12 +++-- module/bug/model.php | 28 ++++++----- module/setting/model.php | 7 ++- 5 files changed, 116 insertions(+), 19 deletions(-) diff --git a/framework/model.class.php b/framework/model.class.php index 626b0d1678..2557e069b6 100644 --- a/framework/model.class.php +++ b/framework/model.class.php @@ -413,7 +413,7 @@ class model extends baseModel * @access public * @return mixed */ - public function __call($method, $arguments) + public function __call(string $method, array $arguments) { $moduleName = $this->getModuleName(); $taoClass = $moduleName . 'Tao'; diff --git a/lib/base/cache/cache.class.php b/lib/base/cache/cache.class.php index cdfe40c897..05b2516766 100644 --- a/lib/base/cache/cache.class.php +++ b/lib/base/cache/cache.class.php @@ -100,6 +100,24 @@ class baseCache */ public $conditionIsTrue; + /** + * 待处理的数据。 + * The data to be processed. + * + * @var array + * @access public + */ + public $data; + + /** + * 待处理数据的column,作为缓存的key。 + * The column as key. + * + * @var string + * @access public + */ + public $dataColumn; + /** * 构造方法。 * The construct method. @@ -429,6 +447,74 @@ class baseCache return $this; } + /** + * 选择处理的数据和column最为缓存的key。 + * Load data and select column as key. + * + * @param array $data + * @param string $field + * @access public + * @return static|cache. + */ + public function use($data, $column) + { + $this->data = $data; + $this->dataColumn = $column; + + return $this; + } + + /** + * data追加字段。 + * Append field to data. + * + * @param string $tableName + * @param string $fields + * @access public + * @return static|cache. + */ + public function append($tableName, $fields) + { + $fields = explode(',', $fields); + + $alias = []; + foreach($fields as $field) + { + $fieldInfo = explode(' ', trim($field)); + if(count($fieldInfo) == 1) + { + $alias[$fieldInfo[0]] = $fieldInfo[0]; + } + elseif(count($fieldInfo) == 2) + { + $alias[$fieldInfo[0]] = $fieldInfo[1]; + } + else + { + $alias[$fieldInfo[0]] = $fieldInfo[2]; + } + } + + $cacheKeys = []; + $key = $this->dataColumn; + foreach($this->data as $row) + { + if(!in_array($row->$key, $cacheKeys)) $cacheKeys[] = $row->$key; + } + $objects = $this->app->redis->fetchAll($tableName, $cacheKeys); + foreach($this->data as $row) + { + $object = $objects[$row->$key]; + foreach($alias as $aliasKey => $aliasValue) + { + $row->$aliasValue = $object->$aliasKey; + } + } + + return $this; + } + + //-------------------- Fetch相关方法(Fetch related methods) -------------------// /** diff --git a/lib/zredis/zredis.class.php b/lib/zredis/zredis.class.php index a118e91b1b..70159dd58b 100644 --- a/lib/zredis/zredis.class.php +++ b/lib/zredis/zredis.class.php @@ -261,13 +261,17 @@ class zredis if(!$keyList) $keyList = $this->redis->smembers("set:{$code}List"); foreach($keyList as $key) $keys[] = "raw:{$code}:{$key}"; - $objects = $this->redis->mget($keys); - foreach($objects as $key => $object) + $objects = $this->redis->mget($keys); + $cacheKey = $this->config->redis->caches[$table]; + + $result = []; + foreach($objects as $object) { - if($object) $objects[$key] = json_decode($object); + $object = json_decode($object); + if($object) $result[$object->$cacheKey] = $object; } - return $objects; + return $result; } /** diff --git a/module/bug/model.php b/module/bug/model.php index 7bf7725dd0..38db25bef0 100644 --- a/module/bug/model.php +++ b/module/bug/model.php @@ -856,27 +856,29 @@ class bugModel extends model $query = preg_replace('/`(\w+)`/', 't1.`$1`', $query); if($moduleName == 'contributeBug') $bugsAssignedByMe = $this->loadModel('my')->getAssignedByMe($account, null, $orderBy, 'bug'); - return $this->dao->select("t1.*, t2.name AS productName, t2.shadow, IF(t1.`pri` = 0, {$this->config->maxPriValue}, t1.`pri`) AS priOrder, IF(t1.`severity` = 0, {$this->config->maxPriValue}, t1.`severity`) AS severityOrder")->from(TABLE_BUG)->alias('t1') - ->leftJoin(TABLE_PRODUCT)->alias('t2')->on('t1.product = t2.id') - ->where('t1.deleted')->eq(0) - ->andWhere('t2.deleted')->eq('0') + + $bugs = $this->dao->select("*, IF(`pri` = 0, {$this->config->maxPriValue}, `pri`) AS priOrder, IF(`severity` = 0, {$this->config->maxPriValue}, `severity`) AS severityOrder")->from(TABLE_BUG) + ->where('deleted')->eq(0) ->beginIF($type == 'bySearch')->andWhere($query)->fi() - ->beginIF($executionID)->andWhere('t1.execution')->eq($executionID)->fi() - ->beginIF($type != 'closedBy' and $this->app->moduleName == 'block')->andWhere('t1.status')->ne('closed')->fi() - ->beginIF($type != 'all' and $type != 'bySearch')->andWhere("t1.`$type`")->eq($account)->fi() - ->beginIF($type == 'bySearch' and $moduleName == 'workBug')->andWhere("t1.assignedTo")->eq($account)->fi() - ->beginIF($type == 'assignedTo' and $moduleName == 'workBug')->andWhere('t1.status')->ne('closed')->fi() + ->beginIF($executionID)->andWhere('execution')->eq($executionID)->fi() + ->beginIF($type != 'closedBy' and $this->app->moduleName == 'block')->andWhere('status')->ne('closed')->fi() + ->beginIF($type != 'all' and $type != 'bySearch')->andWhere("`$type`")->eq($account)->fi() + ->beginIF($type == 'bySearch' and $moduleName == 'workBug')->andWhere("assignedTo")->eq($account)->fi() + ->beginIF($type == 'assignedTo' and $moduleName == 'workBug')->andWhere('status')->ne('closed')->fi() ->beginIF($type == 'bySearch' and $moduleName == 'contributeBug') - ->andWhere('t1.openedBy', 1)->eq($account) - ->orWhere('t1.closedBy')->eq($account) - ->orWhere('t1.resolvedBy')->eq($account) - ->orWhere('t1.id')->in(!empty($bugsAssignedByMe) ? array_keys($bugsAssignedByMe) : array()) + ->andWhere('openedBy', 1)->eq($account) + ->orWhere('closedBy')->eq($account) + ->orWhere('resolvedBy')->eq($account) + ->orWhere('id')->in(!empty($bugsAssignedByMe) ? array_keys($bugsAssignedByMe) : array()) ->markRight(1) ->fi() ->orderBy($orderBy) ->beginIF($limit > 0)->limit($limit)->fi() ->page($pager) ->fetchAll(); + + $this->cache->cache->use($bugs, 'product')->append(TABLE_PRODUCT, 'name AS productName, shadow'); + return $bugs; } /** diff --git a/module/setting/model.php b/module/setting/model.php index a32c992d9b..a8bd3e6e02 100644 --- a/module/setting/model.php +++ b/module/setting/model.php @@ -244,7 +244,12 @@ class settingModel extends model */ public function getSysAndPersonalConfig(string $account = ''): array { - $records = $this->cache->setting->getSysAndPersonalConfig($account); + $owner = 'system,' . ($account ? $account : ''); + $records = $this->dao->select('*')->from(TABLE_CONFIG) + ->where('owner')->in($owner) + ->beginIF(!$this->app->upgrading)->andWhere('vision')->in(array('', $this->config->vision))->fi() + ->orderBy('id') + ->fetchAll('id'); if(!$records) return array(); $vision = $this->config->vision;