Caching

Building a large menu on each request (with counters in badges, for example) can be expensive. The package can cache the built sidebar.

'cache' => [
    'method' => 'user-based',
    'duration' => 1440, // minutes
],
Method Behaviour
null Build the sidebar on each request. Default.
static One cached sidebar for everybody. Use it only when the menu is the same for every visitor.
user-based One cached sidebar per authenticated user. Needs a cache store with tags (Redis, Memcached).

duration is in minutes. Leave it as null to cache forever (until flushed).

Making a sidebar cacheable

Implement ShouldCache and use CacheableTrait (the installer does it for you):

use Wikigods\Sidebar\ShouldCache;
use Wikigods\Sidebar\Sidebar;
use Wikigods\Sidebar\Traits\CacheableTrait;

class AdminSidebar implements Sidebar, ShouldCache
{
    use CacheableTrait;

    // ...
}

The trait stores the menu property. List other properties in protected $cacheables = ['menu', ...]; when your sidebar needs them.

Warning

Don't use static when items, badges or authorization depend on the current user: every visitor would see the menu built for the first one.

Flushing the cache

Dispatch an event that implements ShouldFlushCache when data shown in the menu changes, and register the package listener:

use Illuminate\Support\Facades\Event;
use Wikigods\Sidebar\Domain\Events\FlushesSidebarCache;
use Wikigods\Sidebar\Domain\Events\ShouldFlushCache;

// AppServiceProvider::boot()
Event::listen(ShouldFlushCache::class, FlushesSidebarCache::class);
class RoleUpdated implements ShouldFlushCache
{
    // ...
}

event(new RoleUpdated($role));

php artisan cache:clear also works.