drapor / cache-repository
Simple to use Caching Repository for Laravel Eloquent
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-11-09 19:21:08 UTC
README
This laravel package gives a fluid way to cache your queries while speeding up your Eloquent workflow.
Setup:
Install the composer package
composer require "drapor/cache-repository"Add the service Provider to your app.php
'Drapor\CacheRepository\CacheRepositoryServiceProvider'Publish the config
` php artisan vendor:publish `Usage
Step 1 : Modify our Model
<?php namespace app\Models;
use Drapor\CacheRepository\Eloquent\BaseModel;
class User extends BaseModel
{ ... }
?>
Step 2: Create a Repository Class
<?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
]);
}
}
Now You can Inject the Repository wherever you need to use it. This can be done in quite a few ways due to Laravel's magic, but we're going to keep it simple and use a common example.
Step 3 : Inject The Repo
<?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());
}
}
What should I know?
-
Cache can be disabled for any query at any time by calling
$user = $this->repository->noCache()->find($id);
or by using thePlainRepository
class instead. -
Calls to the
paginate()
method are not cached at this time. If you wish to cache a collection, you should use thewhereCached()
method directly from your Repository -
You can alter the time a model is cached by calling
setCacheLifeTime()
before any method
Planned Feature List
-
Cached json serialization of each entity.
$books = $this->respository ->with('books') ->whereCached('name','harry-potter')-> ->transformTo(BookTransformer::class);
- Caching support for more complex queries, joins, raws, etc
- Global broadcasting of regular model events. Create, delete, restore, etc.
- Fluid cache for pagination responses, that is updated when an element of the collection is modified and does not modify the whole collection each time.
License
The do-whatever-you-want-with-it attributation