[TOC]
* * * * *
# 1 緩存
>>緩存,使用外部存儲機制存儲框架的運行信息
>相對于配置,緩存可用跨請求使用。
>配置的修改與存儲只在當前請求中發揮作用,而緩存的修改與存儲則會影響到下個請求。
## 1-1 緩存初始化
### Cache::init()
>>初始化配置中指定類型的緩存
~~~
public static function init(array $options = [])
{
if (is_null(self::$handler)) {
// 自動初始化緩存
if (!empty($options)) {
self::connect($options);
} elseif ('complex' == Config::get('cache.type')) {
self::connect(Config::get('cache.default'));
} else {
self::connect(Config::get('cache'));
}
}
}
~~~
## 1-2 緩存存儲連接
### Cache::connect()
>>創建配置中指定類型的緩存存儲。
~~~
public static function connect(array $options = [], $name = false)
{
$type = !empty($options['type']) ? $options['type'] : 'File';
if (false === $name) {
$name = md5(serialize($options));
}
if (true === $name || !isset(self::$instance[$name])) {
$class = false !== strpos($type, '\\') ? $type : '\\think\\cache\\driver\\' . ucwords($type);
// 記錄初始化信息
App::$debug && Log::record('[ CACHE ] INIT ' . $type, 'info');
if (true === $name) {
return new $class($options);
} else {
self::$instance[$name] = new $class($options);
}
}
self::$handler = self::$instance[$name];
return self::$handler;
}
~~~
>>由上可知緩存機制默認使用File存儲,其多種存儲驅動實現在(\cache\driver\)目錄下
## 1-3切換緩存存儲類型
>>可以用來切換緩存的存儲類型
### Cache::store()
~~~
public static function store($name)
{
if ('complex' == Config::get('cache.type')) {
self::connect(Config::get('cache.' . $name), strtolower($name));
}
return self::$handler;
}
~~~
# 2 緩存操作
>>緩存的操作包括,存儲數據到緩存存儲器中,讀取緩存存儲器中的數據
### Cache::get()
>>讀取緩存中數據
~~~
public static function get($name, $default = false)
{
self::init();
self::$readTimes++;
return self::$handler->get($name, $default);
}
~~~
### Cache::set()
>>存儲數據到緩存
~~~
public static function set($name, $value, $expire = null)
{
self::init();
self::$writeTimes++;
return self::$handler->set($name, $value, $expire);
}
~~~
### Cache::has()
>>檢查緩存數據
~~~
public static function has($name)
{
self::init();
self::$readTimes++;
return self::$handler->has($name);
}
~~~
### Cache::inc()
>>緩存自增
~~~
public static function inc($name, $step = 1)
{
self::init();
self::$writeTimes++;
return self::$handler->inc($name, $step);
}
~~~
### Cache::dec()
>>緩存自減
~~~
public static function dec($name, $step = 1)
{
self::init();
self::$writeTimes++;
return self::$handler->dec($name, $step);
}
~~~
### Cache::rm()
>>刪除緩存
~~~
public static function rm($name)
{
self::init();
self::$writeTimes++;
return self::$handler->rm($name);
}
~~~
### Cache::clear()
>>清空緩存
~~~
public static function clear($tag = null)
{
self::init();
self::$writeTimes++;
return self::$handler->clear($tag);
}
~~~
### Cache::tag()
>>帶標簽緩存
~~~
public static function tag($name, $keys = null, $overlay = false)
{
self::init();
return self::$handler->tag($name, $keys, $overlay);
}
~~~
# 3 內置緩存驅動
>>tp5內置了多種緩存驅動實現不同形式的存儲方式。如File,Redis,Memacahce.
>這里簡單分析File類緩存驅動
>其他類型緩存接口相似
## 3-1 緩存驅動抽象類(Driver)
### $driver->getCacheKey()
>> 獲取緩存標識
~~~
protected function getCacheKey($name)
{
return $this->options['prefix'] . $name;
}
~~~
### $driver->pull()
>>讀取緩存并刪除
~~~
public function pull($name)
{
$result = $this->get($name, false);
if ($result) {
$this->rm($name);
return $result;
} else {
return null;
}
}
~~~
### $driver->tag()
>>帶標簽緩存
~~~
public function tag($name, $keys = null, $overlay = false)
{
if (is_null($keys)) {
$this->tag = $name;
} else {
$key = 'tag_' . md5($name);
if (is_string($keys)) {
$keys = explode(',', $keys);
}
$keys = array_map([$this, 'getCacheKey'], $keys);
if ($overlay) {
$value = $keys;
} else {
$value = array_unique(array_merge($this->getTagItem($name), $keys));
}
$this->set($key, implode(',', $value));
}
return $this;
}
~~~
### $driver->setTagItem()
>>設置緩存標簽
~~~
protected function setTagItem($name)
{
if ($this->tag) {
$key = 'tag_' . md5($this->tag);
$this->tag = null;
if ($this->has($key)) {
$value = $this->get($key);
$value .= ',' . $name;
} else {
$value = $name;
}
$this->set($key, $value);
}
}
~~~
### $driver->getTagItem()
>>獲取緩存標簽
~~~
protected function getTagItem($tag)
{
$key = 'tag_' . md5($tag);
$value = $this->get($key);
if ($value) {
return explode(',', $value);
} else {
return [];
}
}
~~~
## 3-2 緩存驅動實現類(File)
### $file->__construct()
>>文件存儲緩存構造函數
### $file->init()
>>文件緩存初始化
### $file->getCacheKey()
>>獲取變量的存儲文件名
### $file->has()
>>判斷緩存是否存在
### $file->get()
>> 讀取緩存
### $file->set()
>> 寫入緩存
### $file->inc()
>>自增緩存
### $file->dec()
>>自減緩存
### $file->rm()
>>刪除緩存
### $file->clear()
>>清空緩存
### $file->unlink()
>>文件刪除。
- 框架簡介
- 簡介
- 框架目錄
- 根目錄
- 應用目錄
- 核心目錄
- 擴展目錄
- 其他目錄
- 框架流程
- 啟動流程
- 請求流程
- 響應流程
- 框架結構
- 應用組織
- 網絡請求
- 路由組織
- 數據驗證
- 數據模型(M)
- 數據庫連接(Connection)
- 數據庫(Db)
- 查詢構造(Builder)
- 數據庫查詢(Query)
- 模型(Model)
- 模板視圖(V)
- 視圖(View)
- 模板引擎(Think)
- 模板標簽庫(TagLib)
- 控制器(C)
- 網絡響應
- 配置與緩存
- 配置操作
- 緩存操作
- cookie與session
- Cookie操作
- Session操作
- 自動加載
- 鉤子注冊
- 文件上傳
- 分頁控制
- 控制臺
- 自動構建
- 日志異常調試
- 異常處理
- 代碼調試
- 日志記錄
- 框架使用
- 1 環境搭建(Server)
- 2 網絡請求(Request)
- 3 請求路由(Route)
- 4 響應輸出(Response)
- 5 業務處理(Controller)
- 6 數據存取(Model)
- 7 Web界面(View)