From 5ae7bfefaaba53aab3c64280eeebba937721f8b1 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 27 Jul 2015 11:17:21 +0930 Subject: [PATCH] PERF: cache the results of User::hasPermssion() --- framework/core/src/Core/Users/User.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Core/Users/User.php b/framework/core/src/Core/Users/User.php index 6b68ab8d5..dc062dabf 100755 --- a/framework/core/src/Core/Users/User.php +++ b/framework/core/src/Core/Users/User.php @@ -60,6 +60,13 @@ class User extends Model '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. * @@ -319,7 +326,11 @@ class User extends Model $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]; } /**