drapor / cache-repository
简单的 Laravel Eloquent 缓存仓库
1.0.6
2017-03-03 17:37 UTC
Requires
- php: >=5.4.0
- drapor/networking: dev-master
- illuminate/config: >=5.0
- illuminate/routing: >=5.0
- illuminate/support: >=5.0
This package is not auto-updated.
Last update: 2024-09-28 18:42:15 UTC
README
这个 Laravel 包提供了一种流畅的方式来缓存查询,同时加快你的Eloquent工作流程。
设置
安装 composer 包
composer require "drapor/cache-repository"将服务提供者添加到你的 app.php 中
'Drapor\CacheRepository\CacheRepositoryServiceProvider'发布配置
` php artisan vendor:publish `使用
步骤 1 : 修改我们的模型
<?php namespace app\Models;
use Drapor\CacheRepository\Eloquent\BaseModel;
class User extends BaseModel
{ ... }
?>
步骤 2: 创建一个仓库类
<?php namespace app\Repositories;
use Drapor\CacheRepository\CacheRepository;
class UserRepository extends CacheRepository
{
public function __construct(User $user)
{
parent::__construct($user, 'user');
//Create a Relation instance and pass the second argument as a boolean
//indicating wether or not it should be removed from the cache when the User object is updated.
$task = new Relation('task',true);
//Only call $this->setRelations() if you wish to eager load these relations before every call. This method accepts both
//instances of Relation and strings.
$this->setRelations([
'group', $task
]);
}
}
现在你可以在需要使用它的任何地方注入仓库。由于 Laravel 的魔法,这可以以多种方式完成,但我们将保持简单,并使用一个常见的示例。
步骤 3 : 注入仓库
<?php namespace app/controllers;
use Repositories/UserRepository;
use Request;
class UserController extends Controller
{
protected $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
public function index()
{
$limit = Request::input('limit');
$users = $this->repository
->orderBy('name','ASC')
->paginate($limit);
return response()->json($users);
}
public function getSadUsers()
{
$limit = Request::input('limit');
//Paginate accepts a second argument for search parameters.
//This should be an array of arrays, each with at least a key and a value.
$params = [
[
'key' => 'sad',
'value' => 'true'
],
[
'key' => 'happiness',
'operator' => '<',
'value' => '2',
'keyword' => 'AND'
]
];
//Alternatively you can call Argument::extract(Request::input(),'search')
//and any search information will automatically be formatted
$users = $this->repository
->with('equity','orders')
->paginate($limit,$params);
return response()->json($users->toJson());
}
public function find($id)
{
$user = $this->repository->with('tasks')->find($id);
return response()->json($user->toJson());
}
public function update($id)
{
//The Repository class will automatically clean out any input
//that isn't fillable by our model.
$user = $this->repository->update($id,Request::input());
return response()->json($user->toJson());
}
}
我应该知道什么?
- 可以通过调用来在任何时候禁用任何查询的缓存
$user = $this->repository->noCache()->find($id);或使用PlainRepository类。 - 目前对
paginate()方法的调用不会被缓存。如果你想要缓存一个集合,你应该直接从你的仓库使用whereCached()方法。 - 你可以在调用任何方法之前调用
setCacheLifeTime()来改变模型缓存的时长。
计划功能列表
- 每个实体的缓存 JSON 序列化。
$books = $this->respository ->with('books') ->whereCached('name','harry-potter')-> ->transformTo(BookTransformer::class); - 对更复杂的查询、连接、原始查询等的缓存支持
- 全局广播常规模型事件。创建、删除、恢复等。
- 为分页响应提供流畅的缓存,即在集合中的元素被修改时更新,而不是每次都修改整个集合。
许可证
do-whatever-you-want-with-it 属性