Skip to content

Commit

Permalink
Merge pull request #547 from pjmartorell/fix-cache-tags
Browse files Browse the repository at this point in the history
Bypass cache when file or database cache drivers are set
  • Loading branch information
andrewelkins committed Mar 23, 2016
2 parents 3528dee + 87d407e commit 6a0fd8c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
24 changes: 17 additions & 7 deletions src/Entrust/Traits/EntrustRoleTrait.php
Expand Up @@ -8,6 +8,7 @@
* @package Zizaco\Entrust
*/

use Illuminate\Cache\TaggableStore;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;

Expand All @@ -18,35 +19,44 @@ public function cachedPermissions()
{
$rolePrimaryKey = $this->primaryKey;
$cacheKey = 'entrust_permissions_for_role_'.$this->$rolePrimaryKey;
return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->perms()->get();
});
if(Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('entrust.permission_role_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->perms()->get();
});
}
else return $this->perms()->get();
}
public function save(array $options = [])
{ //both inserts and updates
if(!parent::save($options)){
return false;
}
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}
public function delete(array $options = [])
{ //soft or hard
if(!parent::delete($options)){
return false;
}
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}
public function restore()
{ //soft delete undo's
if(!parent::restore()){
return false;
}
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.permission_role_table'))->flush();
}
return true;
}

/**
* Many-to-Many relations with the user model.
*
Expand Down
24 changes: 17 additions & 7 deletions src/Entrust/Traits/EntrustUserTrait.php
Expand Up @@ -8,6 +8,7 @@
* @package Zizaco\Entrust
*/

use Illuminate\Cache\TaggableStore;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use InvalidArgumentException;
Expand All @@ -19,26 +20,35 @@ public function cachedRoles()
{
$userPrimaryKey = $this->primaryKey;
$cacheKey = 'entrust_roles_for_user_'.$this->$userPrimaryKey;
return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->roles()->get();
});
if(Cache::getStore() instanceof TaggableStore) {
return Cache::tags(Config::get('entrust.role_user_table'))->remember($cacheKey, Config::get('cache.ttl'), function () {
return $this->roles()->get();
});
}
else return $this->roles()->get();
}
public function save(array $options = [])
{ //both inserts and updates
parent::save($options);
Cache::tags(Config::get('entrust.role_user_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
}
}
public function delete(array $options = [])
{ //soft or hard
parent::delete($options);
Cache::tags(Config::get('entrust.role_user_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
}
}
public function restore()
{ //soft delete undo's
parent::restore();
Cache::tags(Config::get('entrust.role_user_table'))->flush();
if(Cache::getStore() instanceof TaggableStore) {
Cache::tags(Config::get('entrust.role_user_table'))->flush();
}
}

/**
* Many-to-Many relations with Role.
*
Expand Down
37 changes: 23 additions & 14 deletions tests/EntrustUserTest.php
Expand Up @@ -2,6 +2,7 @@

use Zizaco\Entrust\Contracts\EntrustUserInterface;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Cache\ArrayStore;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cache;
use Zizaco\Entrust\Permission;
Expand All @@ -11,23 +12,23 @@
class EntrustUserTest extends PHPUnit_Framework_TestCase
{
private $facadeMocks = array();

public function setUp()
{
parent::setUp();

$app = m::mock('app')->shouldReceive('instance')->getMock();

$this->facadeMocks['config'] = m::mock('config');
$this->facadeMocks['cache'] = m::mock('cache');

Config::setFacadeApplication($app);
Config::swap($this->facadeMocks['config']);

Cache::setFacadeApplication($app);
Cache::swap($this->facadeMocks['cache']);
}

public function tearDown()
{
m::close();
Expand Down Expand Up @@ -91,6 +92,7 @@ public function testHasRole()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(9)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(9)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(9)->andReturn($user->roles);
Cache::shouldReceive('getStore')->times(9)->andReturn(new ArrayStore);

/*
|------------------------------------------------------------
Expand Down Expand Up @@ -137,6 +139,7 @@ public function testCan()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(11)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(11)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(11)->andReturn($user->roles);
Cache::shouldReceive('getStore')->times(11)->andReturn(new ArrayStore);

/*
|------------------------------------------------------------
Expand Down Expand Up @@ -182,6 +185,7 @@ public function testCanWithPlaceholderSupport ()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(6)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(6)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(6)->andReturn($user->roles);
Cache::shouldReceive('getStore')->times(6)->andReturn(new ArrayStore);

/*
|------------------------------------------------------------
Expand All @@ -196,8 +200,8 @@ public function testCanWithPlaceholderSupport ()
$this->assertTrue($user->can(['admin.*']));
$this->assertFalse($user->can(['site.*']));
}


public function testAbilityShouldReturnBoolean()
{
/*
Expand Down Expand Up @@ -240,7 +244,8 @@ public function testAbilityShouldReturnBoolean()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles);

Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore);

$user->shouldReceive('hasRole')
->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false))
->andReturn(true);
Expand Down Expand Up @@ -363,7 +368,8 @@ public function testAbilityShouldReturnArray()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles);

Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore);

$user->shouldReceive('hasRole')
->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false))
->andReturn(true);
Expand Down Expand Up @@ -524,7 +530,8 @@ public function testAbilityShouldReturnBoth()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles);

Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore);

$user->shouldReceive('hasRole')
->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false))
->andReturn(true);
Expand Down Expand Up @@ -699,7 +706,8 @@ public function testAbilityShouldAcceptStrings()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(8)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(8)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(8)->andReturn($user->roles);

Cache::shouldReceive('getStore')->times(8)->andReturn(new ArrayStore);

$user->shouldReceive('hasRole')
->with(m::anyOf('UserRoleA', 'UserRoleB'), m::anyOf(true, false))
->andReturn(true);
Expand Down Expand Up @@ -774,7 +782,8 @@ public function testAbilityDefaultOptions()
Config::shouldReceive('get')->with('entrust.role_user_table')->times(32)->andReturn('role_user');
Config::shouldReceive('get')->with('cache.ttl')->times(32)->andReturn('1440');
Cache::shouldReceive('tags->remember')->times(32)->andReturn($user->roles);

Cache::shouldReceive('getStore')->times(32)->andReturn(new ArrayStore);

$user->shouldReceive('hasRole')
->with(m::anyOf($userRoleNameA, $userRoleNameB), m::anyOf(true, false))
->andReturn(true);
Expand Down Expand Up @@ -1124,7 +1133,7 @@ class HasRoleUser implements EntrustUserInterface
public $roles;
public $primaryKey;
public $id;

public function __construct() {
$this->primaryKey = 'id';
$this->id = 4;
Expand Down

0 comments on commit 6a0fd8c

Please sign in to comment.