我是 php 和 Laravel 的初学者。我在我的项目 Laravel 7 中使用。我的项目中有带有缓存的存储库模式。
页面服务提供者:
public function register()
{
$this->app->bind(PageRepositoryInterface::class, function ($app) {
return new CachingPageRepository(
new PageRepository
);
});
}
public function provides()
{
return [
PageRepositoryInterface::class,
];
}
缓存库:
abstract class CachingBaseRepository implements RepositoryInterface
{
use ScopeActiveTrait;
protected $model;
public function all()
{
return Cache::remember($this->model.'.all', $minutes = 10, function () {
return $this->model->get();
});
}
public function allEnables()
{
return Cache::remember($this->model.'.enables', $minutes = 10, function () {
return $this->model->active()->get();
});
}
public function list(string $orderByColumn, string $orderBy = 'desc', array $with = [])
{
return Cache::remember($this->model.'.list', $minutes = 10, function () use($with, $orderByColumn, $orderBy) {
return $this->model->with($with)
->orderBy($orderByColumn, $orderBy)
->get();
});
}
public function listWithPaginate(string $orderByColumn, string $orderBy = 'desc', array $with = [], int $perPage = 10)
{
return Cache::remember($this->model.'.listWithPaginate', $minutes = 10, function () use($with, $orderByColumn, $orderBy, $perPage) {
return $this->model->with($with)
->orderBy($orderByColumn, $orderBy)
->paginate($perPage)->appends(request()->query());
});
}
public function create(array $data): int
{
return $this->model->create($data)->id;
// delete cache: all, enables, list, listWithPaginate
}
qq_遁去的一_1