Download the PHP package casbin/dbal-adapter without Composer
On this page you can find all versions of the php package casbin/dbal-adapter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dbal-adapter
Doctrine DBAL Adapter for Casbin
Doctrine DBAL Adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
The following database vendors are currently supported:
- MySQL
- Oracle
- Microsoft SQL Server
- PostgreSQL
- SAP Sybase SQL Anywhere
- SQLite
- Drizzle
Installation
Via Composer.
Basic Usage (Without Redis Caching)
This section describes how to use the adapter with a direct database connection, without leveraging Redis for caching.
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.
Example:
Usage with Redis Caching
To improve performance and reduce database load, the adapter supports caching policy data using Redis. When enabled, Casbin policies will be fetched from Redis if available, falling back to the database if the cache is empty.
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).
Redis Configuration Options:
host
(string): Hostname or IP address of the Redis server. Default:'127.0.0.1'
.port
(int): Port number of the Redis server. Default:6379
.password
(string, nullable): Password for Redis authentication. Default:null
.database
(int): Redis database index. Default:0
.ttl
(int): Cache Time-To-Live in seconds. Policies stored in Redis will expire after this duration. Default:3600
(1 hour).prefix
(string): Prefix for all Redis keys created by this adapter. Default:'casbin_policies:'
.
Example:
Cache Preheating
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.
Example:
Cache Invalidation
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
).
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.
Getting Help
License
This project is licensed under the Apache 2.0 license.
All versions of dbal-adapter with dependencies
casbin/casbin Version ^4.0
doctrine/dbal Version ^3.9|^4.0
predis/predis Version ^2.0