ulex / cached-repositories
Cached Repositories - A simple Laravel package
Installs: 679
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.0
README
Documentation, Installation, and Usage Instructions
First, install the package via Composer:
composer require ulex/cached-repositories
Service Provider
For Laravel
You should publish the RepositoriesServiceProvider:
php artisan vendor:publish --provider="Ulex\CachedRepositories\RepositoriesServiceProvider" --tag=config
Optional: The service provider will automatically get registered. Or you may manually add the service provider in your config/app.php file: Laravel
'providers' => [ // ... Ulex\CachedRepositories\RepositoriesServiceProvider::class, ];
For Lumen
In your bootstrap/app.php
:
- Register the provider
$app->register(Ulex\CachedRepositories\RepositoriesServiceProvider::class);
- Register config
$app->configure('cached-repositories');
Config
If config file cached-repositories.php
was not published copy it to config folder with:
cp vendor/ulex/cached-repositories/config/cached-repositories.php config/cached-repositories.php
Create Repository, Interface, Decorator for a Model
Run the following php artisan command where the argument is your Model name (example Post):
php artisan make:repository Post --all
Expected Result:
Repository created successfully.
Interface created successfully.
Decorator created successfully.
Add Model in `models` array in config/cached-repositories.php
The following folders will be created in your app/Repositories
folder (if they don't exist):
Decorators Eloquent Interfaces
As seen in the result remember to add the Model in config/cached-repositories.php
:
... 'models' => [ 'User' => App\Models\User::class, 'Post' => App\Models\Post::class, ] ...
How to use
This package provides an abstract structure that uses the Repository design pattern with caching decorators for you application.
Once installed you can create Repositories for your models that cache the data from your queries. EloquentRepository is provided and ready to use. Follow the same principle for any data resource you have on your application.
# Example when injecting to a controller public function __construct(UserRepositoryInterface $userRepository) { $this->userRepository = $userRepository; } ... public function get($name) { //retrieve from db and then cache the result $user = $this->userRepository->getBy('name', $userName); //retrieve straight from db, don't cache $user = $this->userRepository->fromDb()->getBy('name', $userName); }
Extending a model's CachingDecorator
For GET functions use remember
function the same way as in the abstract CachingDecorator. This will ensure that this function is cached properly.
UserCachingDecorator.php
public function getUserInfo($user_id) { return $this->remember(__FUNCTION__, func_get_args()); }
Note: Remember to add the cache invalidation of the new function by extending flushGetKeys in the model's CachingDecorator.
public function flushGetKeys($model, $attributes = null) { $user_id = $model->user_id; $key = $this->key('getUserInfo', compact('user_id')); parent::flushGetKeys($model, $attributes); }
UserRepository.php
Add the query in the model's repository
public function getUserInfo($user_id) { return $this->model->query() ->where('user_id', '=', $user_id) ->whereNotNull('something')->get(); }
Contributing
This package is mostly based on Jeffrey Way's awesome Laracasts lessons when using the repository design pattern on Laravel From Scratch series.
License
The MIT License (MIT). Please see License File for more information.