/** * 获得表结构 * @return array|null * @throws Exception */ public function getTableInfo() { $tableInfo = FCache::get($this->_table); if ($tableInfo) { return $tableInfo; } $sql = "desc {$this->config['table_pre']}{$this->table_raw}"; try { $this->connect('r'); $stmt = $this->_dbh->prepare($sql); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $tableInfo = array('pri' => null, 'fields' => null); foreach ($rows as $row) { if ($row['Key'] == 'PRI') { $tableInfo['pri'] = $row['Field']; } $tableInfo['fields'][$row['Field']] = $row; } FCache::set($this->_table, $tableInfo, 8640000); } catch (PDOException $e) { FLogger::write("获取表信息失败: " . $this->_table . "\t{$sql}\t" . $e->getMessage(), 'error'); throw new Exception("获取表信息失败。"); } return $tableInfo; }
/** * 获得表结构 * @return array|null * @throws Exception */ private function getTableInfo() { $tableInfo = FCache::get($this->_table); if ($tableInfo) { return $tableInfo; } try { $sql = "desc {$this->_table}"; $this->connect(FDB::$DB_READ); $stmt = $this->_dbh->prepare($sql); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $tableInfo = array('pri' => null, 'fields' => null); foreach ($rows as $row) { if ($row['Key'] == 'PRI') { $tableInfo['pri'] = $row['Field']; } } $tableInfo['fields'] = $rows; FCache::set($this->_table, $tableInfo, 8640000); } catch (PDOException $e) { FLogger::write("获取表信息失败: " . $this->_table . "\t" . $e->getMessage()); throw new Exception("获取表信息失败。"); } return $tableInfo; }
public static function fetchFirstCached($sql, $cache_time = 3600) { $cache_key = "sql-fetchFirst_{$sql}"; $cache_content = FCache::get($cache_key); if ($cache_content) { return $cache_content; } $cache_content = self::fetchFirst($sql); FCache::set($cache_key, $cache_content, $cache_time); return $cache_content; }
/** * 获取access_token */ public function checkAuth() { // 从缓存中获取access_token $cacheKey = 'weixin_access_token'; $access_token = FCache::get($cacheKey); if ($access_token) { $this->access_token = $access_token; return true; } // 请求微信服务器获取access_token $result = $this->https_request(self::API_URL_PREFIX . self::AUTH_URL . 'appid=' . $this->appid . '&secret=' . $this->appsecret); if ($result) { $jsonArr = json_decode($result, true); if (!$jsonArr || isset($jsonArr['errcode']) && $jsonArr['errcode'] > 0) { $this->error($jsonArr); } else { $this->access_token = $jsonArr['access_token']; $expire = isset($jsonArr['expires_in']) ? intval($jsonArr['expires_in']) - 100 : 3600; // 将access_token保存到缓存中 FCache::set($cacheKey, $this->access_token, $expire); return true; } } return false; }