Package Data | |
---|---|
Maintainer Username: | michaeljennings |
Maintainer Contact: | michael.jennings91@gmail.com (Michael Jennings) |
Package Create Date: | 2016-04-26 |
Package Last Update: | 2019-12-03 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-11 15:22:50 |
Package Statistics | |
---|---|
Total Downloads: | 5,334 |
Monthly Downloads: | 137 |
Daily Downloads: | 3 |
Total Stars: | 0 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
A laravel package that allows you to cache items against an object. For example you may want to cache a user's navigation items against the user model.
broker()->put($user, 'navigation', $navigationItems, 60);
To install through composer simply run the following command:
composer require michaeljennings/broker
Or add the following to your composer.json
file:
{
"require": {
"michaeljennings/broker": "^1.0"
}
}
And then run composer install
.
For Laravel 5.5 and upwards, the service provider and facade will be loaded automatically. For older versions of Laravel, you will need to add the broker service provider into your providers array in config/app.php
.
'providers' => [
...
'Michaeljennings\Broker\BrokerServiceProvider'
...
];
The package also comes with a facade, to use it add it to your aliases array in config/app.php.
'aliases' => [
...
'Broker' => 'Michaeljennings\Broker\Facades\Broker',
...
];
To make a class cacheable you need to implement the Michaeljennings\Broker\Contracts\Cacheable
interface and implement the getCacheKey
method. This must return a unique key for this class.
class Dummy implements \Michaeljennings\Broker\Contracts\Cacheable
{
public function getCacheKey()
{
return get_class($this);
}
}
If you want to make an eloquent model cacheable you can use the Michaeljennings\Broker\Traits\Cacheable
trait. This automatically uses the table name as the cache key.
use Illuminate\Database\Eloquent\Model;
class Dummy extends Model implements \Michaeljennings\Broker\Contracts\Cacheable
{
use \Michaeljennings\Broker\Traits\Cacheable;
}
Broker can be accessed either by its facade, its helper method, or through its IOC binding.
// Facade
Michaeljennings\Broker\Facades\Broker::get($cachable, 'key');
// Helper
broker()->get($cachable, 'key');
// IOC Binding
app(Michaeljennings\Broker\Contracts\Broker)->get($cachable, 'key');
The get
method is used to retrieve an item for the cacheable entity. If the item does not exist null
will be returned.
broker()->get($cacheable, 'key');
The has
method will check if the key has been set for the cacheable entity.
broker()->has($cacheable, 'key');
Occasionally you may want to retrieve an item, but also set the value if it is not set. You can do this using the remember
method.
broker()->remember($cacheable, 'key', function() {
return DB::table('users')->get();
});
The put
method will add an item to the cache for the cacheable entity. By default items will be stored for 60 minutes, but you can specify the amount minutes.
broker()->put($cacheable, 'key', 'value');
broker()->put($cacheable, 'key', 'value', $minutes);
The forever
method will store items in the cache indefinitely. These items will need to be removed manually with the forget
method.
broker()->forever($cacheable, 'key');
The forget
method will remove a specific item from the cache.
broker()->forget($cacheable, 'key');
Or you may remove all of the items for a cacheable entity with the flush
method.
broker()->flush($cacheable);
Occasionally changes in your application may require you to flush the cache for every entity of a cacheable type, for example you need to clear the cache for every user, but want the rest of the cache to remain.
To this you can use the flushAll
method and pass it the class you want to flush.
broker()->flushAll(App\User::class);