weight) * @return array end_index => weight */ public static function getCachedByTierAndDirection(string $tier, int $direction, ?int $deptId = null): array { $inst = self::getCachedInstance($deptId); $byTierDirection = $inst['by_tier_direction'] ?? []; $list = $byTierDirection[$tier][$direction] ?? []; $result = []; foreach ($list as $row) { $endIndex = isset($row['end_index']) ? (int) $row['end_index'] : 0; $weight = isset($row['weight']) ? (int) $row['weight'] : 1; $result[$endIndex] = $weight; } return $result; } /** * 按渠道从数据库加载并写入缓存 */ public static function refreshCache(?int $deptId = null): void { $deptId = self::resolveDeptId($deptId); $query = (new self())->order('tier')->order('direction')->order('end_index'); ConfigScopeEditHelper::applyDeptIdWhere($query, $deptId); $list = $query->select()->toArray(); $byTierDirection = []; foreach ($list as $row) { $tier = isset($row['tier']) ? (string) $row['tier'] : ''; $direction = isset($row['direction']) ? (int) $row['direction'] : 0; if ($tier !== '') { if (!isset($byTierDirection[$tier])) { $byTierDirection[$tier] = [0 => [], 1 => []]; } if (!isset($byTierDirection[$tier][$direction])) { $byTierDirection[$tier][$direction] = []; } $byTierDirection[$tier][$direction][] = $row; } } $instance = [ 'list' => $list, 'by_tier_direction' => $byTierDirection, ]; self::$instance = $instance; Cache::set(self::cacheKeyForDept($deptId), $instance, self::CACHE_TTL); } private static function buildEmptyInstance(): array { return [ 'list' => [], 'by_tier_direction' => [], ]; } public static function clearRequestInstance(): void { self::$instance = null; self::$requestDeptId = null; } private static function refreshCacheForModel($model): void { $deptId = AdminScopeHelper::normalizeRecordDeptId( is_array($model) ? ($model['dept_id'] ?? null) : ($model->dept_id ?? null) ); self::refreshCache($deptId); } public static function onAfterInsert($model): void { self::refreshCacheForModel($model); } public static function onAfterUpdate($model): void { self::refreshCacheForModel($model); } public static function onAfterDelete($model): void { self::refreshCacheForModel($model); } }