在 Laravel 中缓存查询的更优雅或更有效的方法是什么
What is more elegant or more efficient way to cache queries in Laravel
这就是我目前缓存查询的方式,我认为当数据在更新后 table 秒内发生变化时很难使用模型事件来更新缓存,因为我需要更新多个缓存。
<?php namespace App\Repositories;
use App\Models\Book; //my model
use \Cache;
class BookEloquentRepository implements BookRepositoryInterface{
protected $cache_enabled = true;
protected $cache_key = "book";
public function __construct(Book $book){
$this->book = $book;
}
public function find($id)
{
$this->cache_key = $this->cache_key ."_find_{$id}";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->find($id);
Cache::forever($this->cache_key, $books);
return $books;
}
public function all()
{
$this->cache_key = $this->cache_key ."_all";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->all();
Cache::forever($this->cache_key, $books);
return $books;
}
public function allPublished()
{
$this->cache_key = $this->cache_key ."_allPublished";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->where('published', 1);
Cache::forever($this->cache_key, $books);
return $books;
}
}
这样做正确吗?我面临的挑战是如何在记录更改时更新缓存
我想知道是否有可能只为所有记录维护一个缓存并且能够在没有的情况下做这样的事情
访问数据库。
$books = Cache::get('books');
$book = $books->find(1);
$published = $books->where('published', 1)->get();
这样我只能更新一次缓存 'books' 当记录在 table 使用模型事件更新后更改时
$published = Cache::remember('published', $minutes, function() {
return Book::where('published', 1)->get();
});
这就是我目前缓存查询的方式,我认为当数据在更新后 table 秒内发生变化时很难使用模型事件来更新缓存,因为我需要更新多个缓存。
<?php namespace App\Repositories;
use App\Models\Book; //my model
use \Cache;
class BookEloquentRepository implements BookRepositoryInterface{
protected $cache_enabled = true;
protected $cache_key = "book";
public function __construct(Book $book){
$this->book = $book;
}
public function find($id)
{
$this->cache_key = $this->cache_key ."_find_{$id}";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->find($id);
Cache::forever($this->cache_key, $books);
return $books;
}
public function all()
{
$this->cache_key = $this->cache_key ."_all";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->all();
Cache::forever($this->cache_key, $books);
return $books;
}
public function allPublished()
{
$this->cache_key = $this->cache_key ."_allPublished";
if( $this->cache_enabled && Cache::has($this->cache_key) ) return Cache::get($this->cache_key);
$books = $this->book->where('published', 1);
Cache::forever($this->cache_key, $books);
return $books;
}
}
这样做正确吗?我面临的挑战是如何在记录更改时更新缓存
我想知道是否有可能只为所有记录维护一个缓存并且能够在没有的情况下做这样的事情 访问数据库。
$books = Cache::get('books');
$book = $books->find(1);
$published = $books->where('published', 1)->get();
这样我只能更新一次缓存 'books' 当记录在 table 使用模型事件更新后更改时
$published = Cache::remember('published', $minutes, function() {
return Book::where('published', 1)->get();
});