Skip to content

Commit 34c657d

Browse files
Here's an update on the README to make the usage examples clearer:
I've updated the README.md to provide more distinct and clearer examples for initializing the DBAL adapter: - I clarified basic usage without Redis, showcasing backward compatibility by omitting Redis configuration. - I improved the section on Redis caching usage, ensuring the example for initialization with Redis is clear. - I renamed sections for better discoverability of these usage patterns.
1 parent 52deda5 commit 34c657d

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

README.md

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,45 @@ Via [Composer](https://getcomposer.org/).
2626
composer require casbin/dbal-adapter
2727
```
2828

29-
### Usage
29+
### Basic Usage (Without Redis Caching)
3030

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.
3234

35+
**Example:**
36+
37+
```php
3338
require_once './vendor/autoload.php';
3439

3540
use Casbin\Enforcer;
3641
use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
42+
use Doctrine\DBAL\DriverManager; // Required if creating a new connection object
3743

38-
$config = [
39-
// Either 'driver' with one of the following values:
40-
// pdo_mysql,pdo_sqlite,pdo_pgsql,pdo_oci (unstable),pdo_sqlsrv
41-
// mysqli,sqlanywhere,sqlsrv,ibm_db2 (unstable),drizzle_pdo_mysql
44+
// Option 1: Using DBAL connection parameters array
45+
$dbConnectionParams = [
46+
// Supported drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, pdo_sqlsrv,
47+
// mysqli, sqlanywhere, sqlsrv, ibm_db2, drizzle_pdo_mysql
4248
'driver' => 'pdo_mysql',
4349
'host' => '127.0.0.1',
44-
'dbname' => 'test',
50+
'dbname' => 'casbin_db', // Your database name
4551
'user' => 'root',
4652
'password' => '',
47-
'port' => '3306',
53+
'port' => '3306', // Optional, defaults to driver's standard port
54+
// 'policy_table_name' => 'casbin_rules', // Optional, defaults to 'casbin_rule'
4855
];
4956

50-
$adapter = DatabaseAdapter::newAdapter($config);
57+
// Initialize the Adapter with the DBAL parameters array (without Redis)
58+
$adapter = DatabaseAdapter::newAdapter($dbConnectionParams);
59+
// Alternatively, using the constructor:
60+
// $adapter = new DatabaseAdapter($dbConnectionParams);
61+
62+
// Option 2: Using an existing Doctrine DBAL Connection instance
63+
// $dbalConnection = DriverManager::getConnection($dbConnectionParams);
64+
// $adapter = DatabaseAdapter::newAdapter($dbalConnection);
65+
// Or using the constructor:
66+
// $adapter = new DatabaseAdapter($dbalConnection);
67+
5168

5269
$e = new Enforcer('path/to/model.conf', $adapter);
5370

@@ -62,15 +79,13 @@ if ($e->enforce($sub, $obj, $act) === true) {
6279
}
6380
```
6481

65-
### Redis Caching
82+
### Usage with Redis Caching
6683

6784
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.
6885

69-
#### Configuration
70-
71-
To enable Redis caching, pass a Redis configuration array as the second argument to the `Adapter::newAdapter()` method or the `Adapter` constructor.
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).
7287

73-
Available Redis configuration options:
88+
**Redis Configuration Options:**
7489

7590
* `host` (string): Hostname or IP address of the Redis server. Default: `'127.0.0.1'`.
7691
* `port` (int): Port number of the Redis server. Default: `6379`.
@@ -86,29 +101,36 @@ require_once './vendor/autoload.php';
86101

87102
use Casbin\Enforcer;
88103
use CasbinAdapter\DBAL\Adapter as DatabaseAdapter;
104+
use Doctrine\DBAL\DriverManager; // Required if creating a new connection object
89105

90-
// Database configuration (as before)
91-
$dbConfig = [
106+
// Database connection parameters (can be an array or a Connection object)
107+
$dbConnectionParams = [
92108
'driver' => 'pdo_mysql',
93109
'host' => '127.0.0.1',
94-
'dbname' => 'test',
110+
'dbname' => 'casbin_db',
95111
'user' => 'root',
96112
'password' => '',
97113
'port' => '3306',
98114
];
115+
// Example with DBAL connection object:
116+
// $dbalConnection = DriverManager::getConnection($dbConnectionParams);
99117

100118
// Redis configuration
101119
$redisConfig = [
102120
'host' => '127.0.0.1', // Optional, defaults to '127.0.0.1'
103121
'port' => 6379, // Optional, defaults to 6379
104122
'password' => null, // Optional, defaults to null
105123
'database' => 0, // Optional, defaults to 0
106-
'ttl' => 7200, // Optional, Cache policies for 2 hours
107-
'prefix' => 'myapp_casbin:' // Optional, Custom prefix
124+
'ttl' => 7200, // Optional, Cache policies for 2 hours (default is 3600)
125+
'prefix' => 'myapp_casbin:' // Optional, Custom prefix (default is 'casbin_policies:')
108126
];
109127

110-
// Initialize adapter with both DB and Redis configurations
111-
$adapter = DatabaseAdapter::newAdapter($dbConfig, $redisConfig);
128+
// Initialize adapter with DB parameters array and Redis configuration
129+
$adapter = DatabaseAdapter::newAdapter($dbConnectionParams, $redisConfig);
130+
// Or, using a DBAL Connection object:
131+
// $adapter = DatabaseAdapter::newAdapter($dbalConnection, $redisConfig);
132+
// Alternatively, using the constructor:
133+
// $adapter = new DatabaseAdapter($dbConnectionParams, $redisConfig);
112134

113135
$e = new Enforcer('path/to/model.conf', $adapter);
114136

0 commit comments

Comments
 (0)