33namespace tauthz \adapter ;
44
55use tauthz \model \Rule ;
6+ use tauthz \cache \CacheHandlerContract ;
67use Casbin \Model \Model ;
78use Casbin \Persist \Adapter ;
89use Casbin \Persist \AdapterHelper ;
1112use Casbin \Persist \FilteredAdapter ;
1213use Casbin \Persist \Adapters \Filter ;
1314use Casbin \Exceptions \InvalidFilterTypeException ;
15+ use tauthz \traits \Configurable ;
1416use think \facade \Db ;
1517
1618/**
2022 */
2123class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, FilteredAdapter
2224{
23- use AdapterHelper;
25+ use AdapterHelper, Configurable ;
2426
2527 /**
2628 * @var bool
@@ -34,6 +36,13 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
3436 */
3537 protected $ model ;
3638
39+ /**
40+ * Cache Handler.
41+ *
42+ * @var CacheHandlerContract
43+ */
44+ protected $ cacheHandler ;
45+
3746 /**
3847 * the DatabaseAdapter constructor.
3948 *
@@ -42,6 +51,9 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
4251 public function __construct (Rule $ model )
4352 {
4453 $ this ->model = $ model ;
54+
55+ $ cacheHandlerClass = $ this ->config ('cache.handler ' , \tauthz \cache \CacheHandler::class);
56+ $ this ->cacheHandler = new $ cacheHandlerClass ();
4557 }
4658
4759 /**
@@ -78,7 +90,7 @@ public function savePolicyLine($ptype, array $rule)
7890 foreach ($ rule as $ key => $ value ) {
7991 $ col ['v ' .strval ($ key ).'' ] = $ value ;
8092 }
81- $ this ->model -> cache ( ' tauthz ' )->insert ($ col );
93+ $ this ->cacheHandler -> cachePolicies ( $ this -> model )->insert ($ col );
8294 }
8395
8496 /**
@@ -88,7 +100,7 @@ public function savePolicyLine($ptype, array $rule)
88100 */
89101 public function loadPolicy (Model $ model ): void
90102 {
91- $ rows = $ this ->model -> cache ( ' tauthz ' )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])->select ()->toArray ();
103+ $ rows = $ this ->cacheHandler -> cachePolicies ( $ this -> model )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])->select ()->toArray ();
92104 foreach ($ rows as $ row ) {
93105 $ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
94106 }
@@ -148,7 +160,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
148160 $ cols [$ i ++] = $ temp ;
149161 $ temp = [];
150162 }
151- $ this ->model -> cache ( ' tauthz ' )->insertAll ($ cols );
163+ $ this ->cacheHandler -> cachePolicies ( $ this -> model )->insertAll ($ cols );
152164 }
153165
154166 /**
@@ -169,7 +181,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
169181 }
170182
171183 foreach ($ instance ->select () as $ model ) {
172- if ($ model -> cache ( ' tauthz ' )->delete ()) {
184+ if ($ this -> cacheHandler -> cachePolicies ( $ model )->delete ()) {
173185 ++$ count ;
174186 }
175187 }
@@ -218,7 +230,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
218230 $ item = $ model ->hidden (['id ' , 'ptype ' ])->toArray ();
219231 $ item = $ this ->filterRule ($ item );
220232 $ removedRules [] = $ item ;
221- if ($ model -> cache ( ' tauthz ' )->delete ()) {
233+ if ($ this -> cacheHandler -> cachePolicies ( $ model )->delete ()) {
222234 ++$ count ;
223235 }
224236 }
0 commit comments