在 Laravel 的 Model 層作數據緩存

文章轉發自專業的Laravel開發者社區,原始連接:learnku.com/laravel/t/7…php

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

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

這是這個技術的要點:數據庫

假設你有不少個 Comment 的 Article 模型,給定下面的Laravel blade 模板,你就能夠像下面這樣訪問 /article/:id 路由時獲得評論的數量:數組

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

複製代碼

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

咱們能夠構建一個模板,它僅在文章更新時訪問數據庫,而且訪問該模型的全部代碼均可以獲取緩存值:bash

<h3>$article->cached_comments_count {{ str_plural('Comment', $article->cached_comments_count)</h3>

複製代碼

經過使用模型訪問器,咱們能夠緩存基於最後一次文章更新的評論計數值。閉包

所以,在評論新增或刪除時咱們該怎麼更新文章的 updated_at 列值呢?ide

先進入 touch 方法看看。學習

模型的觸發

能夠經過使用模型的 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 值使緩存失效。不過在新增或刪除一個評論時,咱們怎麼觸發修改文章的 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 屬性是個數組,包含了在評論的建立、保存和刪除時會引發「觸發」的關聯信息。

緩存的屬性

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

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

複製代碼

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

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

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

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

複製代碼

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

articles/1-1515650910

複製代碼

這個鍵值是由表名、模型id值及當前 updated_at 的 timestamp 值組成。一旦咱們觸發這個模型,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();
        });
    }
}

複製代碼

而後是關聯的 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);
    }
}

複製代碼

接下來作什麼?

我已經向你展現瞭如何緩存一個簡單的評論計數,可是如何緩存全部的評論呢?

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

複製代碼

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

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

複製代碼

最後,  我在 Article 模型中定義了cacheKey()方法,可是你可能想要經過一個名爲 ProvidesModelCacheKey的trait來定義這個方法以便你能夠在複合模型中使用或者在一個基礎模型中定義全部模型擴展的方法。 你甚至可能想要爲實現cacheKey() 方法的模型使用使用契約(接口)。

我但願你已經發現這個簡單的技術是十分有用的!

相關文章
相關標籤/搜索