在 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 分享