前段時間作項目時候,想要在不改變方法簽名的狀況下,給 Model::find 方法作個緩存。並且想要作到即插即用。node
1.先看一下當咱們調用 find 方法時,框架幹了什麼?json
找到 Illuminate\Database\Eloquent\Model 的代碼,搜索 find,沒有該方法。看來是走了 __callStatic 這個魔術方法。該方法裏只有一行代碼:緩存
return (new static)->$method(...$parameters);
static 指的是調用該靜態方法的類(若是使用的是 UserModel::find(1),則 static 就表明 UserModel 類)。看來是實例化了一個對象,並調用了成員方法。架構
2.分析如何優雅地在中間插一腳框架
爲了可以在調用 find 時候,先走咱們的緩存,因此咱們須要覆蓋 __callStatic 方法,並檢測若是是 find 方法,則優先返回緩存中的數據。this
另外,爲了可以達到即插即用的效果,咱們使用繼承的方式,而是使用了 Trait。核心邏輯以下:spa
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'){ // 從緩存中獲取數據 $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]); // 設置緩存及過時時間 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}"; }
大致邏輯上面已經介紹過了:覆蓋 __callStatic 方法,判斷若是是調用 find ,則走緩存(無緩存,查詢後須要設置緩存)。另新增 findNoCache 方法。code
3.細節補充對象
當修改(或刪除)數據(調用 save 方法)時須要刪除已緩存的內容。blog
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 方法我暫時寫,內容相似 save 方法 如何使用。在須要使用 find 緩存的 Model 類裏,加上一行就夠了。 class User extends BaseModel { use MemoryCacheTrait; }
快去試試吧。
更多PHP內容請訪問: