userInfo = request()->userInfo; // 生成缓存key $cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId']; // 尝试从缓存获取设备信息 $device = Cache::get($cacheKey); // 如果缓存不存在,则从数据库获取 if (!$device) { $device = Db::name('device_user') ->alias('du') ->join(['s2_device' => 'd'], 'd.id = du.deviceId','left') ->join(['s2_wechat_account' => 'wa'], 'd.id = wa.currentDeviceId','left') ->where([ 'du.userId' => $this->userInfo['id'], 'du.companyId' => $this->userInfo['companyId'] ]) ->field('d.*,wa.id as wechatAccountId,wa.wechatId,wa.alias') ->find(); // 将设备信息存入缓存 if ($device) { Cache::set($cacheKey, $device, $this->cacheExpire); } } $this->device = $device; } /** * 清除设备信息缓存 */ protected function clearDeviceCache() { $cacheKey = 'device_info_' . $this->userInfo['id'] . '_' . $this->userInfo['companyId']; Cache::rm($cacheKey); } }