欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在Laravel中使用Model層做數(shù)據(jù)緩存-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關如何在Laravel中使用Model層做數(shù)據(jù)緩存,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據(jù)這篇文章可以有所收獲。

十多年專注成都網(wǎng)站制作,成都企業(yè)網(wǎng)站建設,個人網(wǎng)站制作服務,為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設計流程、步驟,成功服務上千家企業(yè)。為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制高端網(wǎng)站建設服務,專注于成都企業(yè)網(wǎng)站建設,高端網(wǎng)頁制作,對混凝土攪拌罐車等多個方面,擁有豐富的網(wǎng)站維護經(jīng)驗。

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


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

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

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

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

通過使用模型訪問器,我們可以緩存基于最后一次文章更新的評論計數(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ā)”的關聯(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ù)范圍內有高的命中率。

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

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

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

articles/1-1515650910

這個鍵值是由表名、模型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();
    });
  }
}

然后是關聯(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;
  });
}

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

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

看完上述內容,你們對如何在Laravel中使用Model層做數(shù)據(jù)緩存有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享題目:如何在Laravel中使用Model層做數(shù)據(jù)緩存-創(chuàng)新互聯(lián)
鏈接分享:http://chinadenli.net/article26/dgjsjg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計網(wǎng)站維護、網(wǎng)站排名手機網(wǎng)站建設、云服務器電子商務

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設