Skip to content

Commit

Permalink
Fixed caching code previously commited
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmerainfo committed Sep 6, 2017
1 parent fa265f1 commit e5a176f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 36 deletions.
33 changes: 12 additions & 21 deletions src/Ntrust/Traits/NtrustRoleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@

trait NtrustRoleTrait
{
private static $permissions;

//Big block of caching functionality.
public function cachedPermissions()
{
$rolePrimaryKey = $this->primaryKey;
$cacheKey = 'ntrust_permissions_for_role_'.$this->$rolePrimaryKey;
if (self::$permissions) {
return self::$permissions;
} else if (Cache::getStore() instanceof TaggableStore) {
self::$permissions = Cache::tags(Config::get('ntrust.profiles.' . self::$roleProfile . '.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl', 1440), function () {
return $this->perms()->get();
});
return self::$permissions;
} else {
self::$permissions = $this->perms()->get();
return self::$permissions;
}

if (Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('ntrust.profiles.' . self::$roleProfile . '.permission_role_table'))
->remember($cacheKey, Config::get('cache.ttl', 1440), function () {
return $this->perms()->get();
});
}
else
return $this->perms()->get();
}

/**
Expand Down Expand Up @@ -62,19 +58,16 @@ public function perms()
*/
protected static function bootNtrustRoleTrait()
{
static::saved(function()
{
static::saved(function() {
self::clearCache();
});

static::deleted(function($role)
{
static::deleted(function($role) {
self::clearCache($role);
});

if(method_exists(self::class, 'restored')) {
static::restored(function($role)
{
static::restored(function($role) {
self::clearCache($role);
});
}
Expand Down Expand Up @@ -218,8 +211,6 @@ public static function clearCache($role = null)
$role->perms()->sync([]);
}
}

self::$permissions = null;
}
}

24 changes: 9 additions & 15 deletions src/Ntrust/Traits/NtrustUserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@

trait NtrustUserTrait
{
private static $roles;

//Big block of caching functionality.
public function cachedRoles()
{
$userPrimaryKey = $this->primaryKey;
$cacheKey = 'ntrust_roles_for_' . self::$roleProfile . '_'.$this->$userPrimaryKey;
if (self::$roles) {
return self::$roles;
} else if (Cache::getStore() instanceof TaggableStore) {
self::$roles = Cache::tags(Config::get('ntrust.profiles.' . self::$roleProfile . '.role_user_table'))->remember($cacheKey, Config::get('cache.ttl', 1440), function () {
return $this->roles()->get();
});
return self::$roles;
} else {
self::$roles = $this->roles()->get();
return self::$roles;
}

if (Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('ntrust.profiles.' . self::$roleProfile . '.role_user_table'))
->remember($cacheKey, Config::get('cache.ttl', 1440), function () {
return $this->roles()->get();
});
}
else
return $this->roles()->get();
}

/**
Expand Down Expand Up @@ -289,8 +285,6 @@ public static function clearCache($user = null)
if ($user)
$user->roles()->sync([]);
}

self::$roles = null;
}

}

0 comments on commit e5a176f

Please sign in to comment.