在 Laravel 中如何在 Model 層進行數據緩存?

在 Laravel 中如何在 Model 層進行數據緩存?

您在此之前可能就已經緩存過模型數據,但是我將向您展示一個使用動態記錄模型的更精細的 laravel 模型緩存技術,這是我一開始在 RailsCasts 學習到的技術。

使用模型的唯一緩存鍵,您可以緩存模型(或關聯模型)更新時自動更新(以及緩存失效)的模型上的屬性和關聯,一個好處是訪問緩存的數據比在控制器中緩存的數據更具可復用性,因為它在模型上而不是在單個控制器方法中。

這是這個技術的要點:

假設你有很多個 Comment 的 Article 模型,給定下面的 Laravel blade 模板,你就可以像下面這樣訪問 /article/:id 路由時得到評論的數量:

<h3>$article-&gt;comments-&gt;count()?{{?str_plural('Comment',?$article-&gt;comments-&gt;count())</h3>

您可以在控制器中緩存評論的計數,但是當您有多個需要緩存的一次性查詢和數據時,控制器會變得非常臃腫難看。使用控制器,訪問緩存的數據也不是很方便。

我們可以構建一個模板,它僅在文章更新時訪問數據庫,并且訪問該模型的所有代碼都可以獲取緩存值:

<h3>$article-&gt;cached_comments_count?{{?str_plural('Comment',?$article-&gt;cached_comments_count)</h3>

通過使用模型訪問器,我們可以緩存基于最后一次文章更新的評論計數值。

因此,在評論新增或刪除時我們該怎么更新文章的 updated_at 列值呢?

先進入 touch 方法看看。

模型的觸發

可以通過使用模型的 touch() 方法來更新文章的 updated_at 列值:

$ php artisan tinker

&gt;&gt;&gt;?$article?=?AppArticle::first(); =&gt;?AppArticle?{#746 ?????id:?1, ?????title:?"Hello?World", ?????body:?"The?Body", ?????created_at:?"2018-01-11?05:16:51", ?????updated_at:?"2018-01-11?05:51:07", ???} &gt;&gt;&gt;?$article-&gt;updated_at-&gt;timestamp =&gt;?1515649867 &gt;&gt;&gt;?$article-&gt;touch(); =&gt;?true &gt;&gt;&gt;?$article-&gt;updated_at-&gt;timestamp =&gt;?1515650910

我們可以用更新的 timestamp 值使緩存失效。不過在新增或刪除一個評論時,我們怎么觸發修改文章的 updated_at 字段呢?

碰巧 Eloquent 模型中有一個屬性就叫 $touches 。下面是我們的評論模型的大概樣子:

<?php namespace App; use AppArticle; use IlluminateDatabaseEloquentModel; class Comment extends Model {     protected $guarded = [];     protected $touches = [&#39;article&#39;];     public function article()     {         return $this->belongsTo(Article::class); ????} }

這里的 $touches 屬性是個數組,包含了在評論的創建、保存和刪除時會引起 “觸發” 的關聯信息。

緩存的屬性

我們先回到 $article->cached_comments_count 訪問器。該方法的實現可能象 AppArticle 模型中的樣子:

public?function?getCachedCommentsCountAttribute() { ????return?Cache::remember($this-&gt;cacheKey()?.?':comments_count',?15,?function?()?{ ????????return?$this-&gt;comments-&gt;count(); ????}); }

我們使用唯一鍵值的 cacheKey() 方法緩存模型 15 分鐘,然后簡單地在閉包方法中返回評論計數值。

注意,我們也用到了 Cache::rememberForever() 方法,靠著緩存機制的垃圾回收策略以刪除過期的鍵值。我設置了一個定時器,以便在每隔 15 分鐘的緩存刷新間隔里,緩存可在該時間的多數范圍內有最高的命中率。

cacheKey() 方法要用到模型的唯一鍵值,并且在模型更新時對應緩存失效。下面是我的 cacheKey 實現代碼:

public?function?cacheKey() { ????return?sprintf( ????????"%s/%s-%s", ????????$this-&gt;getTable(), ????????$this-&gt;getKey(), ????????$this-&gt;updated_at-&gt;timestamp ????); }

模型的 cacheKey() 方法示例輸出結果可能返回下面的字串信息:

articles/1-1515650910

這個鍵值是由表名、模型 id 值及當前 updated_at 的 timestamp 值組成。一旦我們觸發這個模型,timestamp 值就會更新,并且我們的模型緩存就會相應地失效。

以下是 Article 模型的完整代碼:

<?php namespace App; use AppComment; use IlluminateSupportFacadesCache; use IlluminateDatabaseEloquentModel; class Article extends Model {     public function cacheKey()     {         return sprintf(             "%s/%s-%s",             $this->getTable(), ????????????$this-&gt;getKey(), ????????????$this-&gt;updated_at-&gt;timestamp ????????); ????} ????public?function?comments() ????{ ????????return?$this-&gt;hasMany(Comment::class); ????} ????public?function?getCachedCommentsCountAttribute() ????{ ????????return?Cache::remember($this-&gt;cacheKey()?.?':comments_count',?15,?function?()?{ ????????????return?$this-&gt;comments-&gt;count(); ????????}); ????} }

然后是關聯的 Comment 模型:

<?php namespace App; use AppArticle; use IlluminateDatabaseEloquentModel; class Comment extends Model {     protected $guarded = [];     protected $touches = [&#39;article&#39;];     public function article()     {         return $this->belongsTo(Article::class); ????} }

接下來做什么?

我已經向你展示了如何緩存一個簡單的評論計數,但是如何緩存所有的評論呢?

public?function?getCachedCommentsAttribute() { ????return?Cache::remember($this-&gt;cacheKey()?.?':comments',?15,?function?()?{ ????????return?$this-&gt;comments; ????}); }

你也可以選擇將評論轉換為數組替代序列化模型,只允許在前端對數據進行簡單的數組訪問:

public?function?getCachedCommentsAttribute() { ????return?Cache::remember($this-&gt;cacheKey()?.?':comments',?15,?function?()?{ ????????return?$this-&gt;comments-&gt;toArray(); ????}); }

最后, 我在 Article 模型中定義了 cacheKey() 方法,但是你可能想要通過一個名為 ProvidesModelCacheKey 的 trait 來定義這個方法以便你可以在復合模型中使用或者在一個基礎模型中定義所有模型擴展的方法。 你甚至可能想要為實現 cacheKey() 方法的模型使用使用契約 (接口)。

推薦教程:《PHP教程》《PHP教程

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享