Package Data | |
---|---|
Maintainer Username: | stevenmaguire |
Maintainer Contact: | stevenmaguire@gmail.com (Steven Maguire) |
Package Create Date: | 2015-04-28 |
Package Last Update: | 2015-10-22 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-22 03:04:38 |
Package Statistics | |
---|---|
Total Downloads: | 1,757 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 17 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Seamlessly adding caching to Laravel Eloquent service objects.
Via Composer
$ composer require stevenmaguire/laravel-cache
Extend your service class with the EloquentCache class.
class UserRegistrar extends Stevenmaguire\Laravel\Services\EloquentCache
{
//
}
Implement the interface and use the trait.
class UserRegistrar implements Stevenmaguire\Laravel\Contracts\Cacheable
{
use \Stevenmaguire\Laravel\Services\EloquentCacheTrait;
}
Build queries using Eloquent and request cache object.
use App\User;
use Stevenmaguire\Laravel\Services\EloquentCache;
class UserRegistrar extends EloquentCache
{
public function __construct(User $user)
{
$this->user = $user;
}
public function getAllUsers()
{
$query = $this->user->query();
return $this->cache('all', $query);
}
public function getUserById($id)
{
$query = $this->user->where('id', $id);
return $this->cache('id('.$id.')', $query, 'first');
}
public function getRecent($skip = 0, $take = 100)
{
$query = $this->user->orderBy('created_at', 'desc')
->take($take)
->skip($skip);
return $this->cache('recent('.$skip.','.$take.')', $query);
}
}
The cache
method takes three parameters:
Builder
object for the Eloquent queryget
, first
, list
, paginate
etc; get
by defaultIf the method associated with the optional verb takes parameters, like paginate
, the parameters can be expressed as a comma separated list following the verb and a colon. If a parameter expects an array of literal values, these may be expressed as a pipe delimited sting.
/**
* Paginate users with all pagination parameters
*/
public function getAllUsers()
{
$query = $this->user->query();
return $this->cache('all', $query, 'paginate:15,id|email|name,sheet,2');
// $query->paginate(15, ['id', 'email', 'name'], 'sheet', 2);
}
The cache service will automatically index all of the unique keys used by your application. These keys will be used when the flushCache
method is called on each service implementing the base cache service.
For each of the services you implement using the EloquentCache
you can configure the following:
use Stevenmaguire\Laravel\Services\EloquentCache;
class UserRegistrar extends EloquentCache
{
protected $cacheForMinutes = 15;
//
}
use Stevenmaguire\Laravel\Services\EloquentCache;
class UserRegistrar extends EloquentCache
{
protected $enableCaching = false;
//
}
use Stevenmaguire\Laravel\Services\EloquentCache;
class UserRegistrar extends EloquentCache
{
protected $enableLogging = false;
//
}
use Stevenmaguire\Laravel\Services\EloquentCache;
class UserRegistrar extends EloquentCache
{
protected $cacheIndexKey = 'my-service-keys-index';
//
}
Each service object that implements caching via Stevenmaguire\Laravel\Services\EloquentCache
can flush its own cache, independently of other consuming services.
Your application can flush the cache for all keys within the service object serviceKey
group.
$userRegistrar->flushCache();
Your application can only flush the cache for keys within the service object serviceKey
group that match a particular regular expression pattern.
// Flush cache for all cached users with a single digit user id
$userRegistrar->flushCache('^id\([0-9]{1}\)$');
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->when('App\Handlers\Events\UserHandler')
->needs('Stevenmaguire\Laravel\Contracts\Cacheable')
->give('App\Services\UserRegistrar');
}
}
In this particular example, UserHandler
is responsible for flushing the user service cache when a specific event occurs. The UserHandler
takes a dependacy on the flushCache
method within the UserRegistrar
service.
$ ./vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email stevenmaguire@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.