function do_history($chars, $max = 20) { require_once 'lib/php-file-cache/fcache.inc.php'; $fcache = new FCache(); $history = $fcache->get('hash_history'); if (!$history) { $history = array(); } if (array_key_exists($chars, $history)) { return array_keys($history); } if (count($history) > $max) { $key_del = array_rand($history, 1); unset($history[$key_del]); } $history[$chars] = ''; $fcache->add('hash_history', $history); return array_keys($history); }
public static function updateSystemCache() { $setting_file = WEB_ROOT_DIR . self::$cache_file; // FFile::rmDir(WEB_ROOT_DIR . 'data/system/'); $t = new FTable('setting'); $settings = $t->select(); $setting_write = array(); foreach ($settings as $row) { $setting_write[$row['setting_key']] = $row['setting_value']; } FFile::save($setting_file, "<?php\n" . 'return ' . var_export($setting_write, true) . ';'); FCache::flush(); }
/** * 获得表结构 * @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; }
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; }
/** * S * @access public * @param string $key * @param mixed $var * @param integer $expire * @return mixed */ public static function S($key, $var = null, $expire = 3600) { if (empty($key)) { return null; } if (!is_object(self::$_cache)) { self::$_cache = new static(); } $num = func_num_args(); if ($num == 1) { return self::$_cache->get($key); } elseif ($num > 1) { return self::$_cache->set($key, $var, $expire); } }
} return false; } private function _safe_filename($key) { if ($this->_is_valid_key($key)) { return md5($key); } return 'unvalid_cache_key'; } private function _get_cache_file($key) { return $this->cache_path . $this->_safe_filename($key) . $this->cache_extension; } } $cache = new FCache(); if (strlen($cache->get(urlencode($query))) > 0) { echo $cache->get(urlencode($query)) . '_'; } else { // 下拉框提示模式 I 第 1 位查询扩展作为主标题 $sugip = array('115.239.211.11', '115.239.211.12', '180.97.33.72', '180.97.33.73'); shuffle($sugip); // 1. 初始化 $ch = curl_init(); // 2. 设置选项,包括 URL curl_setopt($ch, CURLOPT_URL, 'http://' . $sugip[0] . '/su?action=opensearch&ie=UTF-8&wd=' . $query); curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
/** * 获得表结构 * @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; }
<?php require_once 'fcache.inc.php'; //example $cache = new FCache(); $storeData = array('time' => time(), 'str' => 'test', 'int' => 1321); $cache->add('select * from table;', $storeData); $cache->add('select * from table;', $storeData); $cache->add('select * from table;', $storeData); $cache->add('select * from table;', $storeData); print_r($storeData = $cache->get('select * from table;')); $cache->delete('select * from table;'); print_r($cache->get('select * from table;') ? 'exist' : 'has no cache'); $cache->add('select * from table1;', 123); $cache->add('select * from table2;', 234); $cache->add('select * from table3;', 345); $cache->flush(); print_r($cache->get('select * from table3;') ? 'exist' : 'has no cache');
/** * 获取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; }