Skip to content

Commit 12c4eb0

Browse files
author
Scott A Connerly
committed
* Cache what roles a user has
* Cache what permissions a role has
1 parent 2f97b7c commit 12c4eb0

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/Entrust/Traits/EntrustRoleTrait.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,36 @@
88
* @package Zizaco\Entrust
99
*/
1010

11+
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Facades\Config;
1213

1314
trait EntrustRoleTrait
1415
{
16+
//Big block of caching functionality.
17+
public function cachedPermissions()
18+
{
19+
$rolePrimaryKey = $this->primaryKey;
20+
$cacheKey = 'entrust_permissions_for_role_'.$this->$rolePrimaryKey;
21+
return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
22+
return $this->perms()->get();
23+
});
24+
}
25+
public function save(array $options = [])
26+
{ //both inserts and updates
27+
parent::save($options);
28+
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
29+
}
30+
public function delete(array $options = [])
31+
{ //soft or hard
32+
parent::delete($options);
33+
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
34+
}
35+
public function restore()
36+
{ //soft delete undo's
37+
parent::restore();
38+
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
39+
}
40+
1541
/**
1642
* Many-to-Many relations with the user model.
1743
*
@@ -81,7 +107,7 @@ public function hasPermission($name, $requireAll = false)
81107
// Return the value of $requireAll;
82108
return $requireAll;
83109
} else {
84-
foreach ($this->perms as $permission) {
110+
foreach ($this->cachedPermissions() as $permission) {
85111
if ($permission->name == $name) {
86112
return true;
87113
}

src/Entrust/Traits/EntrustUserTrait.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,37 @@
88
* @package Zizaco\Entrust
99
*/
1010

11+
use Illuminate\Support\Facades\Cache;
1112
use Illuminate\Support\Facades\Config;
1213
use InvalidArgumentException;
1314

1415
trait EntrustUserTrait
1516
{
17+
//Big block of caching functionality.
18+
public function cachedRoles()
19+
{
20+
$userPrimaryKey = $this->primaryKey;
21+
$cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey;
22+
return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
23+
return $this->roles()->get();
24+
});
25+
}
26+
public function save(array $options = [])
27+
{ //both inserts and updates
28+
parent::save($options);
29+
Cache::tags(Config::get('entrust.role_user_table'))->flush();
30+
}
31+
public function delete(array $options = [])
32+
{ //soft or hard
33+
parent::delete($options);
34+
Cache::tags(Config::get('entrust.role_user_table'))->flush();
35+
}
36+
public function restore()
37+
{ //soft delete undo's
38+
parent::restore();
39+
Cache::tags(Config::get('entrust.role_user_table'))->flush();
40+
}
41+
1642
/**
1743
* Many-to-Many relations with Role.
1844
*
@@ -69,7 +95,7 @@ public function hasRole($name, $requireAll = false)
6995
// Return the value of $requireAll;
7096
return $requireAll;
7197
} else {
72-
foreach ($this->roles as $role) {
98+
foreach ($this->cachedRoles() as $role) {
7399
if ($role->name == $name) {
74100
return true;
75101
}
@@ -105,9 +131,9 @@ public function can($permission, $requireAll = false)
105131
// Return the value of $requireAll;
106132
return $requireAll;
107133
} else {
108-
foreach ($this->roles as $role) {
134+
foreach ($this->cachedRoles() as $role) {
109135
// Validate against the Permission table
110-
foreach ($role->perms as $perm) {
136+
foreach ($role->cachedPermissions() as $perm) {
111137
if (str_is( $permission, $perm->name) ) {
112138
return true;
113139
}

0 commit comments

Comments
 (0)