Skip to content

Commit cc73dea

Browse files
donjan-dengDonjan
andauthored
perf: Optimize the cache to improve performance
* Improve performance * Trigger event * add DatabaseAdapterForCacheTest.php Co-authored-by: Donjan <[email protected]>
1 parent 66a8576 commit cc73dea

File tree

2 files changed

+159
-18
lines changed

2 files changed

+159
-18
lines changed

src/Adapters/DatabaseAdapter.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
135135
$temp = [];
136136
}
137137
$this->eloquent->insert($cols);
138+
Rule::fireModelEvent('saved');
138139
}
139140

140141
/**
@@ -146,19 +147,14 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
146147
*/
147148
public function removePolicy(string $sec, string $ptype, array $rule): void
148149
{
149-
$count = 0;
150-
151150
$instance = $this->eloquent->where('p_type', $ptype);
152151

153152
foreach ($rule as $key => $value) {
154153
$instance->where('v'.strval($key), $value);
155154
}
156155

157-
foreach ($instance->get() as $model) {
158-
if ($model->delete()) {
159-
++$count;
160-
}
161-
}
156+
$instance->delete();
157+
Rule::fireModelEvent('deleted');
162158
}
163159

164160
/**
@@ -171,7 +167,6 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
171167
*/
172168
public function removePolicies(string $sec, string $ptype, array $rules): void
173169
{
174-
$count = 0;
175170
$instance = $this->eloquent->where('p_type', $ptype);
176171
foreach($rules as $rule)
177172
{
@@ -184,8 +179,8 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
184179
foreach($keys as $key){
185180
$instance->whereIn($key, $con[$key]);
186181
}
187-
$num = $instance->delete();
188-
$count += $num;
182+
$instance->delete();
183+
Rule::fireModelEvent('deleted');
189184
}
190185

191186
/**
@@ -199,22 +194,18 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
199194
*/
200195
public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex, string ...$fieldValues): void
201196
{
202-
$count = 0;
203-
204197
$instance = $this->eloquent->where('p_type', $ptype);
198+
205199
foreach (range(0, 5) as $value) {
206200
if ($fieldIndex <= $value && $value < $fieldIndex + count($fieldValues)) {
207201
if ('' != $fieldValues[$value - $fieldIndex]) {
208202
$instance->where('v'.strval($value), $fieldValues[$value - $fieldIndex]);
209203
}
210204
}
211205
}
212-
213-
foreach ($instance->get() as $model) {
214-
if ($model->delete()) {
215-
++$count;
216-
}
217-
}
206+
207+
$instance->delete();
208+
Rule::fireModelEvent('deleted');
218209
}
219210

220211
/**
@@ -238,6 +229,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
238229
$update['v' . $k] = $v;
239230
}
240231
$instance->update($update);
232+
Rule::fireModelEvent('saved');
241233
}
242234

243235
/**

tests/DatabaseAdapterForCacheTest.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
namespace Lauthz\Tests;
4+
5+
use Enforcer;
6+
use Lauthz\Models\Rule;
7+
use Illuminate\Foundation\Testing\DatabaseMigrations;
8+
use Casbin\Persist\Adapters\Filter;
9+
use Casbin\Exceptions\InvalidFilterTypeException;
10+
11+
class DatabaseAdapterForCacheTest extends TestCase
12+
{
13+
14+
use DatabaseMigrations;
15+
16+
public function testAddPolicy()
17+
{
18+
$this->enableCache();
19+
$this->assertFalse(Enforcer::enforce('eve', 'data3', 'read'));
20+
Enforcer::addPermissionForUser('eve', 'data3', 'read');
21+
$this->refreshPolicies();
22+
$this->assertTrue(Enforcer::enforce('eve', 'data3', 'read'));
23+
}
24+
25+
public function testAddPolicies()
26+
{
27+
$this->enableCache();
28+
$policies = [
29+
['u1', 'd1', 'read'],
30+
['u2', 'd2', 'read'],
31+
['u3', 'd3', 'read'],
32+
];
33+
$this->refreshPolicies();
34+
Rule::truncate();
35+
Enforcer::addPolicies($policies);
36+
$this->refreshPolicies();
37+
$this->assertEquals($policies, Enforcer::getPolicy());
38+
}
39+
40+
public function testSavePolicy()
41+
{
42+
$this->enableCache();
43+
$this->assertFalse(Enforcer::enforce('alice', 'data4', 'read'));
44+
45+
$model = Enforcer::getModel();
46+
$model->clearPolicy();
47+
$model->addPolicy('p', 'p', ['alice', 'data4', 'read']);
48+
49+
$adapter = Enforcer::getAdapter();
50+
$adapter->savePolicy($model);
51+
$this->refreshPolicies();
52+
$this->assertTrue(Enforcer::enforce('alice', 'data4', 'read'));
53+
}
54+
55+
public function testRemovePolicy()
56+
{
57+
$this->enableCache();
58+
$this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
59+
60+
Enforcer::addPermissionForUser('alice', 'data5', 'read');
61+
$this->refreshPolicies();
62+
$this->assertTrue(Enforcer::enforce('alice', 'data5', 'read'));
63+
64+
Enforcer::deletePermissionForUser('alice', 'data5', 'read');
65+
$this->refreshPolicies();
66+
$this->assertFalse(Enforcer::enforce('alice', 'data5', 'read'));
67+
}
68+
69+
public function testRemovePolicies()
70+
{
71+
$this->enableCache();
72+
$this->assertEquals([
73+
['alice', 'data1', 'read'],
74+
['bob', 'data2', 'write'],
75+
['data2_admin', 'data2', 'read'],
76+
['data2_admin', 'data2', 'write'],
77+
], Enforcer::getPolicy());
78+
79+
Enforcer::removePolicies([
80+
['data2_admin', 'data2', 'read'],
81+
['data2_admin', 'data2', 'write'],
82+
]);
83+
$this->refreshPolicies();
84+
$this->assertEquals([
85+
['alice', 'data1', 'read'],
86+
['bob', 'data2', 'write']
87+
], Enforcer::getPolicy());
88+
}
89+
90+
public function testRemoveFilteredPolicy()
91+
{
92+
$this->enableCache();
93+
$this->assertTrue(Enforcer::enforce('alice', 'data1', 'read'));
94+
Enforcer::removeFilteredPolicy(1, 'data1');
95+
$this->refreshPolicies();
96+
$this->assertFalse(Enforcer::enforce('alice', 'data1', 'read'));
97+
$this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
98+
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'read'));
99+
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
100+
Enforcer::removeFilteredPolicy(1, 'data2', 'read');
101+
$this->refreshPolicies();
102+
$this->assertTrue(Enforcer::enforce('bob', 'data2', 'write'));
103+
$this->assertFalse(Enforcer::enforce('alice', 'data2', 'read'));
104+
$this->assertTrue(Enforcer::enforce('alice', 'data2', 'write'));
105+
Enforcer::removeFilteredPolicy(2, 'write');
106+
$this->refreshPolicies();
107+
$this->assertFalse(Enforcer::enforce('bob', 'data2', 'write'));
108+
$this->assertFalse(Enforcer::enforce('alice', 'data2', 'write'));
109+
}
110+
111+
public function testUpdatePolicy()
112+
{
113+
$this->enableCache();
114+
$this->assertEquals([
115+
['alice', 'data1', 'read'],
116+
['bob', 'data2', 'write'],
117+
['data2_admin', 'data2', 'read'],
118+
['data2_admin', 'data2', 'write'],
119+
], Enforcer::getPolicy());
120+
121+
Enforcer::updatePolicy(
122+
['alice', 'data1', 'read'],
123+
['alice', 'data1', 'write']
124+
);
125+
126+
Enforcer::updatePolicy(
127+
['bob', 'data2', 'write'],
128+
['bob', 'data2', 'read']
129+
);
130+
$this->refreshPolicies();
131+
$this->assertEquals([
132+
['alice', 'data1', 'write'],
133+
['bob', 'data2', 'read'],
134+
['data2_admin', 'data2', 'read'],
135+
['data2_admin', 'data2', 'write'],
136+
], Enforcer::getPolicy());
137+
}
138+
139+
protected function refreshPolicies()
140+
{
141+
Enforcer::loadPolicy();
142+
}
143+
144+
protected function enableCache()
145+
{
146+
$this->app['config']->set('lauthz.basic.cache.enabled', true);
147+
}
148+
149+
}

0 commit comments

Comments
 (0)