您在此之前可能就已經(jīng)緩存過模型數(shù)據(jù),但是我將向您展示一個使用動態(tài)記錄模型的更精細的Laravel模型緩存技術(shù),這是我一開始在 RailsCasts學習到的技術(shù)。
使用模型的唯一緩存鍵,您可以緩存模型(或關(guān)聯(lián)模型)更新時自動更新(以及緩存失效)的模型上的屬性和關(guān)聯(lián),一個好處是訪問緩存的數(shù)據(jù)比在控制器中緩存的數(shù)據(jù)更具可復用性,因為它在模型上而不是在單個控制器方法中。
這是這個技術(shù)的要點:
假設你有很多個 Comment
的 Article
模型,給定下面的Laravel blade 模板,你就可以像下面這樣訪問 /article/:id
路由時得到評論的數(shù)量:
h3>$article->comments->count() {{ str_plural('Comment', $article->comments->count())/h3>
您可以在控制器中緩存評論的計數(shù),但是當您有多個需要緩存的一次性查詢和數(shù)據(jù)時,控制器會變得非常臃腫難看。使用控制器,訪問緩存的數(shù)據(jù)也不是很方便。
我們可以構(gòu)建一個模板,它僅在文章更新時訪問數(shù)據(jù)庫,并且訪問該模型的所有代碼都可以獲取緩存值:
h3>$article->cached_comments_count {{ str_plural('Comment', $article->cached_comments_count)/h3>
通過使用模型訪問器,我們可以緩存基于最后一次文章更新的評論計數(shù)值。
因此,在評論新增或刪除時我們該怎么更新文章的 updated_at
列值呢?
先進入 touch 方法看看。
模型的觸發(fā)
可以通過使用模型的 touch()
方法來更新文章的 updated_at
列值:
$ php artisan tinker
>>> $article = \App\Article::first();
=> App\Article {#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",
}
>>> $article->updated_at->timestamp
=> 1515649867
>>> $article->touch();
=> true
>>> $article->updated_at->timestamp
=> 1515650910
我們可以用更新的 timestamp 值使緩存失效。不過在新增或刪除一個評論時,我們怎么觸發(fā)修改文章的 updated_at
字段呢?
碰巧 Eloquent 模型中有一個屬性就叫 $touches
。下面是我們的評論模型的大概樣子:
?php
namespace App;
use App\Article;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $guarded = [];
protected $touches = ['article'];
public function article()
{
return $this->belongsTo(Article::class);
}
}
這里的 $touches
屬性是個數(shù)組,包含了在評論的創(chuàng)建、保存和刪除時會引起“觸發(fā)”的關(guān)聯(lián)信息。
緩存的屬性
我們先回到 $article->cached_comments_count
訪問器。該方法的實現(xiàn)可能象 App\Article
模型中的樣子:
public function getCachedCommentsCountAttribute()
{
return Cache::remember($this->cacheKey() . ':comments_count', 15, function () {
return $this->comments->count();
});
}
我們使用唯一鍵值的 cacheKey()
方法緩存模型 15 分鐘,然后簡單地在閉包方法中返回評論計數(shù)值。
注意,我們也用到了 Cache::rememberForever()
方法,靠著緩存機制的垃圾回收策略以刪除過期的鍵值。我設置了一個定時器,以便在每隔 15 分鐘的緩存刷新間隔里,緩存可在該時間的多數(shù)范圍內(nèi)有最高的命中率。
cacheKey()
方法要用到模型的唯一鍵值,并且在模型更新時對應緩存失效。下面是我的 cacheKey
實現(xiàn)代碼:
public function cacheKey()
{
return sprintf(
"%s/%s-%s",
$this->getTable(),
$this->getKey(),
$this->updated_at->timestamp
);
}
模型的 cacheKey()
方法示例輸出結(jié)果可能返回下面的字串信息:
這個鍵值是由表名、模型id值及當前 updated_at
的 timestamp 值組成。一旦我們觸發(fā)這個模型,timestamp 值就會更新,并且我們的模型緩存就會相應地失效。
以下是 Article
模型的完整代碼:
?php
namespace App;
use App\Comment;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public function cacheKey()
{
return sprintf(
"%s/%s-%s",
$this->getTable(),
$this->getKey(),
$this->updated_at->timestamp
);
}
public function comments()
{
return $this->hasMany(Comment::class);
}
public function getCachedCommentsCountAttribute()
{
return Cache::remember($this->cacheKey() . ':comments_count', 15, function () {
return $this->comments->count();
});
}
}
然后是關(guān)聯(lián)的 Comment
模型:
?php
namespace App;
use App\Article;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $guarded = [];
protected $touches = ['article'];
public function article()
{
return $this->belongsTo(Article::class);
}
}
接下來做什么?
我已經(jīng)向你展示了如何緩存一個簡單的評論計數(shù),但是如何緩存所有的評論呢?
public function getCachedCommentsAttribute()
{
return Cache::remember($this->cacheKey() . ':comments', 15, function () {
return $this->comments;
});
}
你也可以選擇將評論轉(zhuǎn)換為數(shù)組替代序列化模型,只允許在前端對數(shù)據(jù)進行簡單的數(shù)組訪問:
public function getCachedCommentsAttribute()
{
return Cache::remember($this->cacheKey() . ':comments', 15, function () {
return $this->comments->toArray();
});
}
最后, 我在 Article
模型中定義了cacheKey()
方法,但是你可能想要通過一個名為 ProvidesModelCacheKey
的trait來定義這個方法以便你可以在復合模型中使用或者在一個基礎模型中定義所有模型擴展的方法。 你甚至可能想要為實現(xiàn)cacheKey()
方法的模型使用使用契約(接口)。
我希望你已經(jīng)發(fā)現(xiàn)這個簡單的技術(shù)是十分有用的!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 清除laravel緩存命令代碼實例
- 在 Laravel 6 中緩存數(shù)據(jù)庫查詢結(jié)果的方法
- laravel清除視圖緩存的代碼
- laravel使用Redis實現(xiàn)網(wǎng)站緩存讀取的方法詳解
- laravel框架的緩存操作代碼實例