|
| 1 | +# Copyright 2026 The casbin Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from casbin.async_enforcer import AsyncEnforcer |
| 16 | +from casbin.cached_enforcer import CacheSupport |
| 17 | + |
| 18 | + |
| 19 | +class AsyncCachedEnforcer(CacheSupport, AsyncEnforcer): |
| 20 | + """AsyncEnforcer that caches enforce() decisions. |
| 21 | +
|
| 22 | + Only policy management is asynchronous upstream; enforce()/enforce_ex() stay |
| 23 | + synchronous, and so does the caching around them. The methods overridden here are |
| 24 | + exactly the mutating ones AsyncEnforcer defines as coroutines -- the synchronous ones |
| 25 | + are handled by CacheSupport, and redeclaring them as coroutines here would turn calls |
| 26 | + like ``e.clear_policy()`` into un-awaited no-ops. |
| 27 | +
|
| 28 | + See CachedEnforcer for what the cache does and does not notice. |
| 29 | + """ |
| 30 | + |
| 31 | + async def load_policy(self): |
| 32 | + try: |
| 33 | + return await super().load_policy() |
| 34 | + finally: |
| 35 | + self.invalidate_cache() |
| 36 | + |
| 37 | + async def load_filtered_policy(self, filter): |
| 38 | + try: |
| 39 | + return await super().load_filtered_policy(filter) |
| 40 | + finally: |
| 41 | + self.invalidate_cache() |
| 42 | + |
| 43 | + async def load_increment_filtered_policy(self, filter): |
| 44 | + try: |
| 45 | + return await super().load_increment_filtered_policy(filter) |
| 46 | + finally: |
| 47 | + self.invalidate_cache() |
| 48 | + |
| 49 | + async def _add_policy(self, sec, ptype, rule): |
| 50 | + try: |
| 51 | + return await super()._add_policy(sec, ptype, rule) |
| 52 | + finally: |
| 53 | + self.invalidate_cache() |
| 54 | + |
| 55 | + async def _add_policies(self, sec, ptype, rules): |
| 56 | + try: |
| 57 | + return await super()._add_policies(sec, ptype, rules) |
| 58 | + finally: |
| 59 | + self.invalidate_cache() |
| 60 | + |
| 61 | + async def _update_policy(self, sec, ptype, old_rule, new_rule): |
| 62 | + try: |
| 63 | + return await super()._update_policy(sec, ptype, old_rule, new_rule) |
| 64 | + finally: |
| 65 | + self.invalidate_cache() |
| 66 | + |
| 67 | + async def _update_policies(self, sec, ptype, old_rules, new_rules): |
| 68 | + try: |
| 69 | + return await super()._update_policies(sec, ptype, old_rules, new_rules) |
| 70 | + finally: |
| 71 | + self.invalidate_cache() |
| 72 | + |
| 73 | + async def _update_filtered_policies(self, sec, ptype, new_rules, field_index, *field_values): |
| 74 | + try: |
| 75 | + return await super()._update_filtered_policies(sec, ptype, new_rules, field_index, *field_values) |
| 76 | + finally: |
| 77 | + self.invalidate_cache() |
| 78 | + |
| 79 | + async def _remove_policy(self, sec, ptype, rule): |
| 80 | + try: |
| 81 | + return await super()._remove_policy(sec, ptype, rule) |
| 82 | + finally: |
| 83 | + self.invalidate_cache() |
| 84 | + |
| 85 | + async def _remove_policies(self, sec, ptype, rules): |
| 86 | + try: |
| 87 | + return await super()._remove_policies(sec, ptype, rules) |
| 88 | + finally: |
| 89 | + self.invalidate_cache() |
| 90 | + |
| 91 | + async def _remove_filtered_policy(self, sec, ptype, field_index, *field_values): |
| 92 | + try: |
| 93 | + return await super()._remove_filtered_policy(sec, ptype, field_index, *field_values) |
| 94 | + finally: |
| 95 | + self.invalidate_cache() |
| 96 | + |
| 97 | + async def _remove_filtered_policy_returns_effects(self, sec, ptype, field_index, *field_values): |
| 98 | + try: |
| 99 | + return await super()._remove_filtered_policy_returns_effects(sec, ptype, field_index, *field_values) |
| 100 | + finally: |
| 101 | + self.invalidate_cache() |
0 commit comments