PHPackages                             michaeljennings/broker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. michaeljennings/broker

ActiveLibrary[Caching](/categories/caching)

michaeljennings/broker
======================

A laravel package for caching keys against an object

v1.0.3(6y ago)07.5k1MITPHPCI failing

Since Apr 26Pushed 6y ago1 watchersCompare

[ Source](https://github.com/michaeljennings/broker)[ Packagist](https://packagist.org/packages/michaeljennings/broker)[ RSS](/packages/michaeljennings-broker/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (8)Versions (10)Used By (1)

Broker [![Build Status](https://camo.githubusercontent.com/932f7cb9f852d4ec352b839a14444b5c2dc12c513a3410d4b3eea07279a8d86d/68747470733a2f2f7472617669732d63692e6f72672f6d69636861656c6a656e6e696e67732f62726f6b65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/michaeljennings/broker) [![Coverage Status](https://camo.githubusercontent.com/9ba033e4510e7ef6b56ea1b35ec20fc3f85615f4e46e823dcf3108d764973d28/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d69636861656c6a656e6e696e67732f62726f6b65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/michaeljennings/broker?branch=master)
=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#broker--)

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);
```

Installation
------------

[](#installation)

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',
  ...
];
```

Cacheable Entities
------------------

[](#cacheable-entities)

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);
	}
}
```

### Cacheable Models

[](#cacheable-models)

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;
}
```

Usage
-----

[](#usage)

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');
```

### Retrieving Items From Cache

[](#retrieving-items-from-cache)

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');
```

#### Checking If An Item Exists

[](#checking-if-an-item-exists)

The `has` method will check if the key has been set for the cacheable entity.

```
broker()->has($cacheable, 'key');
```

#### Retrieve/Store

[](#retrievestore)

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();
});
```

### Storing Items In The Cache

[](#storing-items-in-the-cache)

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);
```

#### Storing Items Forever

[](#storing-items-forever)

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');
```

### Removing Items From The Cache

[](#removing-items-from-the-cache)

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);
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~164 days

Recently: every ~220 days

Total

9

Last Release

2359d ago

Major Versions

v0.1.4 → v1.0.02018-08-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/df807d0371b1b265ea1becedfa4001428f278e7632c6d175e2afb2921f5788a3?d=identicon)[michaeljennings](/maintainers/michaeljennings)

---

Top Contributors

[![michaeljennings](https://avatars.githubusercontent.com/u/5189701?v=4)](https://github.com/michaeljennings "michaeljennings (30 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/michaeljennings-broker/health.svg)

```
[![Health](https://phpackages.com/badges/michaeljennings-broker/health.svg)](https://phpackages.com/packages/michaeljennings-broker)
```

###  Alternatives

[laravel/pulse

Laravel Pulse is a real-time application performance monitoring tool and dashboard for your Laravel application.

1.7k12.1M99](/packages/laravel-pulse)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[mikebronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k127.1k1](/packages/mikebronner-laravel-model-caching)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
