讨论数量:
实战教程2中有说,是使用Observer来实现,当数据更新的话
Cache::forget($link->cache_key);
当获取数据时,优先从cache中读取,如果没有则读取数据中的数据并且存入cache
public function getAllCached()
{
// 尝试从缓存中取出 cache_key 对应的数据。如果能取到,便直接返回数据。
// 否则运行匿名函数中的代码来取出 links 表中所有的数据,返回的同时做了缓存。
return Cache::remember($this->cache_key, $this->cache_expire_in_seconds, function () {
return $this->all();
});
}
推荐文章: