ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 文件 libraries/Yf/Cache.php 负责初始化并存放所有的Cache类。 ~~~ 调用方法 1.缓存页面 $Cache = Yf_Cache::create('default'); $site_index_key = sprintf('%s|%s|%s', Yf_Registry::get('server_id'), 'site_index', isset($_COOKIE['sub_site_id']) ? $_COOKIE['sub_site_id'] : 0); if (!$Cache->start($site_index_key)) { //其他页面代码。。。 $Cache->_id = $site_index_key; $Cache->end($site_index_key); } 2.缓存数据 $cache_group = 'default';//配置信息中的缓存key $Cache = Yf_Cache::create($cache_group); $key = '';//缓存key $data = '';//缓存数据 $Cache->save($data, $key);//保存缓存 $Cache->get($key,$cache_group); 底层实现 ~~~ public $_cacheKeyPrefix = 'c|m|'; public $_cacheName = 'default'; public $_tableName = 'test'; public $_tablePrimaryKey = 'test_id'; public $_cacheFlag = false; /** * 取得 * * @param array $id * * @return array $rows 信息 * @access protected */ protected function getCacheRow($id = null, &$need_cache_id_name) { fb($id); $rows = array(); if (is_array($id)) { if ($this->_cacheFlag) { $Yf_Cache = Yf_Cache::create($this->_cacheName); $cache_key = array(); $cache_key_index = array(); foreach ($id as $item) { $cache_key[] = $this->_cacheKeyPrefix . $item; $cache_key_index[$this->_cacheKeyPrefix . $item] = $item; } $rows_cache = array(); //fix file cache $Yf_Registry = Yf_Registry::getInstance(); if (!isset($Yf_Registry['config_cache'][$this->_cacheName])) { $this->_cacheName = 'default'; } if (1 == $Yf_Registry['config_cache'][$this->_cacheName]['cacheType']) { foreach ($cache_key as $key_id) { $tmp_data = $Yf_Cache->get($key_id); if (false === $tmp_data) { } else { $rows_cache[$key_id] = $tmp_data; } } //end fix file cache } else { $rows_cache = $Yf_Cache->get($cache_key); } foreach ($cache_key as $val) { if (!isset($rows_cache[$val])) { array_push($need_cache_id_name, $cache_key_index[$val]); } else { $rows = $rows + (array)$rows_cache[$val]; } } } } //fb($rows, "getCacheRow(" . encode_json($id) . ", " . encode_json($need_cache_id_name) . ")"); return $rows; } /** * 添加缓存,为多条记录 * * @param array $rows_db * * @return array $rows 信息 * @access protected */ protected function setCacheRow($rows_db = null, $expire = null) { if ($this->_cacheFlag) { if (false !== $rows_db) { $Yf_Cache = Yf_Cache::create($this->_cacheName); foreach ($rows_db as $key => $val) { //fix file cache $Yf_Registry = Yf_Registry::getInstance(); if (!isset($Yf_Registry['config_cache'][$this->_cacheName])) { $this->_cacheName = 'default'; } if (1 == $Yf_Registry['config_cache'][$this->_cacheName]['cacheType']) { $Yf_Cache->save(array($key => $val), $this->_cacheKeyPrefix . $key); } else { $Yf_Cache->save(array($key => $val), $this->_cacheKeyPrefix . $key, null, 0, $expire); } //end fix file cache /* $Yf_Cache->save(array($key=>$val), $this->_cacheKeyPrefix . $key, null, 0, $expire); //$this->setCache(array($key=>$val), $this->_cacheKeyPrefix . $key); */ } } } } /** * 取得单个缓存 * * @param int $id id * * @return array $rows 信息 * @access protected */ protected function getCache($id = null) { $rows = array(); if ($this->_cacheFlag) { $Yf_Cache = Yf_Cache::create($this->_cacheName); if ($id) { if (is_array($id)) { $cache_key = array(); foreach ($id as $k => $v) { $cache_key[] = $this->_cacheKeyPrefix . $v; } } else { $cache_key = $this->_cacheKeyPrefix . $id; } } else { $cache_key = $this->_cacheKeyPrefix . 'all'; } $rows = $Yf_Cache->get($cache_key); } fb($cache_key, 'getCache'); fb($rows); return $rows; } /** * 添加缓存,为单条记录 * * @param array $rows_db * * @return array $rows 信息 * @access protected */ protected function setCache($rows_db, $key = null, $expire = null) { if ($this->_cacheFlag) { if (false !== $rows_db) { $Yf_Cache = Yf_Cache::create($this->_cacheName); //fix file cache $Yf_Registry = Yf_Registry::getInstance(); if (!isset($Yf_Registry['config_cache'][$this->_cacheName])) { $this->_cacheName = 'default'; } if (1 == $Yf_Registry['config_cache'][$this->_cacheName]['cacheType']) { if ($key) { return $Yf_Cache->save($rows_db, $this->_cacheKeyPrefix . $key); } else { return $Yf_Cache->save($rows_db, null); } } else { if ($key) { return $Yf_Cache->save($rows_db, $this->_cacheKeyPrefix . $key, null, 0, $expire); } else { return $Yf_Cache->save($rows_db, null, null, 0, $expire); } } //end fix file cache } } } ~~~