You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+106-9Lines changed: 106 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,28 +26,45 @@ Via [Composer](https://getcomposer.org/).
26
26
composer require casbin/dbal-adapter
27
27
```
28
28
29
-
### Usage
29
+
### Basic Usage (Without Redis Caching)
30
30
31
-
```php
31
+
This section describes how to use the adapter with a direct database connection, without leveraging Redis for caching.
32
+
33
+
You can initialize the adapter by passing either a Doctrine DBAL connection parameter array or an existing `Doctrine\DBAL\Connection` instance to the `Adapter::newAdapter()` method or the `Adapter` constructor.
34
+
35
+
**Example:**
32
36
37
+
```php
33
38
require_once './vendor/autoload.php';
34
39
35
40
use Casbin\Enforcer;
36
41
use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
42
+
use Doctrine\DBAL\DriverManager; // Required if creating a new connection object
37
43
38
-
$config = [
39
-
// Either 'driver' with one of the following values:
To improve performance and reduce database load, the adapter supports caching policy data using [Redis](https://redis.io/). When enabled, Casbin policies will be fetched from Redis if available, falling back to the database if the cache is empty.
85
+
86
+
To enable Redis caching, provide a Redis configuration array as the second argument when initializing the adapter. The first argument remains your Doctrine DBAL connection (either a parameters array or a `Connection` object).
87
+
88
+
**Redis Configuration Options:**
89
+
90
+
*`host` (string): Hostname or IP address of the Redis server. Default: `'127.0.0.1'`.
91
+
*`port` (int): Port number of the Redis server. Default: `6379`.
92
+
*`password` (string, nullable): Password for Redis authentication. Default: `null`.
// $adapter = new DatabaseAdapter($dbConnectionParams, $redisConfig);
134
+
135
+
$e = new Enforcer('path/to/model.conf', $adapter);
136
+
137
+
// ... rest of your Casbin usage
138
+
```
139
+
140
+
#### Cache Preheating
141
+
142
+
The adapter provides a `preheatCache()` method to proactively load all policies from the database and store them in the Redis cache. This can be useful during application startup or as part of a scheduled task to ensure the cache is warm, reducing latency on initial policy checks.
// Cache preheating failed (e.g., Redis not available or DB error)
152
+
echo "Casbin policy cache preheating failed.\n";
153
+
}
154
+
```
155
+
156
+
#### Cache Invalidation
157
+
158
+
The cache is designed to be automatically invalidated when policy-modifying methods are called on the adapter (e.g., `addPolicy()`, `removePolicy()`, `savePolicy()`, etc.). Currently, this primarily clears the cache key for all policies (`{$prefix}all_policies`).
159
+
160
+
**Important Note:** The automatic invalidation for *filtered policies* (policies loaded via `loadFilteredPolicy()`) is limited. Due to the way `predis/predis` client works and to avoid using performance-detrimental commands like `KEYS *` in production environments, the adapter does not automatically delete cache entries for specific filters by pattern. If you rely heavily on `loadFilteredPolicy` and make frequent policy changes, consider a lower TTL for your Redis cache or implement a more sophisticated cache invalidation strategy for filtered results outside of this adapter if needed. The main `{$prefix}all_policies` cache is cleared on any policy change, which means subsequent calls to `loadPolicy()` will refresh from the database and update this general cache.
0 commit comments