PERF: cache the results of User::hasPermssion()

This commit is contained in:
Toby Zerner 2015-07-27 11:17:21 +09:30
parent 372a159521
commit 5ae7bfefaa

View File

@ -60,6 +60,13 @@ class User extends Model
'notification_read_time' 'notification_read_time'
]; ];
/**
* An cache of permissions, and whether or not this user has them.
*
* @var array
*/
protected $hasPermission = [];
/** /**
* The hasher with which to hash passwords. * The hasher with which to hash passwords.
* *
@ -319,7 +326,11 @@ class User extends Model
$this->setRelation('permissions', $this->permissions()->get()); $this->setRelation('permissions', $this->permissions()->get());
} }
return (bool) $this->permissions->contains('permission', $permission); if (! isset($this->hasPermissions[$permission])) {
$this->hasPermission[$permission] = (bool) $this->permissions->contains('permission', $permission);
}
return $this->hasPermission[$permission];
} }
/** /**