Laravel ORM對Model::find方法進行緩存示例詳解
前言
前段時間做項目時候,想要在不改變方法簽名的情況下,給 Model::find 方法做個緩存。而且想要做到即插即用。下面話不多說了,來一起看看詳細的介紹哦。
1.先看一下當(dāng)我們調(diào)用 find 方法時,框架干了什么?
找到 Illuminate\Database\Eloquent\Model 的代碼,搜索 find,沒有該方法??磥硎亲吡?__callStatic 這個魔術(shù)方法。該方法里只有一行代碼:
return (new static)->$method(...$parameters);
static 指的是調(diào)用該靜態(tài)方法的類(如果使用的是 UserModel::find(1),則 static 就代表 UserModel 類)??磥硎菍嵗艘粋€對象,并調(diào)用了成員方法。
2.分析如何優(yōu)雅地在中間插一腳
為了能夠在調(diào)用 find 時候,先走我們的緩存,所以我們需要覆蓋 __callStatic 方法,并檢測如果是 find 方法,則優(yōu)先返回緩存中的數(shù)據(jù)。
另外,為了能夠達到即插即用的效果,我們使用繼承的方式,而是使用了 Trait。核心邏輯如下:
public static function create($data = null){
if ($data == null){
return null;
}
$instance = new static;
foreach ($data as $key => $value){
$instance[$key] = $value;
}
return $instance;
}
/**
* 如果方法是 find($id, $nocache)
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
if ($method == 'find'){
// 從緩存中獲取數(shù)據(jù)
$obj = static::create(json_decode(Redis::get(static::getCacheKey($parameters[0])), true));
if (null == $obj){
$obj = (new static)->$method(...$parameters);
if (null == $obj){
return null;
} else {
$key = static::getCacheKey($parameters[0]);
// 設(shè)置緩存及過期時間
Redis::set($key, $obj);
Redis::expire($key, static::$expire_time);
return $obj;
}
} else {
$obj->exists = true;
return $obj;
}
} else if($method == 'findNoCache'){
$method = 'find';
return (new static)->$method(...$parameters);
}
return (new static)->$method(...$parameters);
}
private static function getCacheKey($id){
$name = str_replace('\\', ':', __CLASS__);
return "{$name}:{$id}";
}
大體邏輯上面已經(jīng)介紹過了:覆蓋 __callStatic 方法,判斷如果是調(diào)用 find ,則走緩存(無緩存,查詢后需要設(shè)置緩存)。另新增 findNoCache 方法。
3.細節(jié)補充
當(dāng)修改(或刪除)數(shù)據(jù)(調(diào)用 save 方法)時需要刪除已緩存的內(nèi)容。
private static function clearCache($id){
Redis::del(self::getCacheKey($id));
}
/**
* when save, should clear cache
* @param array $options
*/
public function save(array $options = []){
static::clearCache($this[$this->primaryKey]);
return parent::save($options);
}
// delete 方法我暫時寫,內(nèi)容類似 save 方法
如何使用。在需要使用 find 緩存的 Model 類里,加上一行就夠了。
class User extends BaseModel
{
use MemoryCacheTrait;
}
快去試試吧。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
php版微信公眾平臺之微信網(wǎng)頁登陸授權(quán)示例
這篇文章主要介紹了php版微信公眾平臺之微信網(wǎng)頁登陸授權(quán)的方法,結(jié)合實例形式分析了微信網(wǎng)頁登陸授權(quán)的接口調(diào)用及參數(shù)含義,需要的朋友可以參考下2016-09-09
ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法分析
這篇文章主要介紹了ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法,結(jié)合實例形式分析了thinkPHP5.0數(shù)據(jù)庫的配置與動態(tài)連接相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
thinkphp5 模型實例化獲得數(shù)據(jù)對象的教程
今天小編就為大家分享一篇thinkphp5 模型實例化獲得數(shù)據(jù)對象的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
PHP array_reduce()函數(shù)的應(yīng)用解析
這篇文章主要介紹了PHP array_reduce()的應(yīng)用,本文通過代碼舉例給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10

